Custom validator in gridview

imstac73
imstac73 used Ask the Experts™
on
I have a gridview in which I have a textbox and a dropdown list.  I have added a custom validator on the template of the dropdown list.  I need a custom validator that displays an error message if the textbox value <> 0 and the selected index of the dropdown list is 0 (meaning that they have not selected a valid value in the dropdown list).  I have added the validator and wrote some js code to accomplish this but it is not working.  It is displaying the error even if the user has selected a valid dropdown list value. Attached is part of my gridview code for those two controls and my js code. Please let me know if you need the entire gridview code and I can send it in a .txt file.
Partial gridview code:
<asp:TemplateField HeaderText="OTH Hrs" SortExpression="INOTHR" ConvertEmptyStringToNull="False">
                    <EditItemTemplate>
                        <asp:TextBox ID="gridtxtOthHrs" runat="server" CssClass="table3_gridfield" Text='<%# Bind("INOTHR") %>' style="text-align: center" Width="45px" CausesValidation="True"></asp:TextBox><asp:RequiredFieldValidator ID="rgvGridOthHrs" runat="server" ControlToValidate="gridtxtOthHrs"
                            Display="Dynamic" ErrorMessage="Value needed; if none, enter 0" SetFocusOnError="True"
                            ValidationGroup="GridValidate" Width="184px"></asp:RequiredFieldValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="gridlblOthHrs" runat="server" Text='<%# Bind("INOTHR") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle CssClass="table3_labels" />
                    <ItemStyle CssClass="table3_gridfield" HorizontalAlign="Right" Wrap="True" VerticalAlign="Middle" />
                    <ControlStyle Width="90%" />
                    <FooterTemplate>
                        <asp:Label ID="gridlblOthHrsTotal" runat="server" Text="Label"></asp:Label>
                    </FooterTemplate>
                    <FooterStyle CssClass="gridview_totals" Font-Bold="True" HorizontalAlign="Right"
                        VerticalAlign="Middle" />
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Other Hours Type">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DD_OthHourTypeedit" runat="server" DataSourceID="OtherHoursType"
                        DataTextField="Desc" DataValueField="OthHoursCode" style="text-align: right" SelectedValue='<%# BIND("INOTTY") %>' AppendDataBoundItems="True" CssClass="table3_gridfield" >
                       </asp:DropDownList><br />
                        <asp:CustomValidator ID="gridcvothhrstype" runat="server" ClientValidationFunction="validategrdothhrstype"
                            ControlToValidate="gridtxtOthHrs" Display="Dynamic" ErrorMessage="Please Select an Other Hours Type"
                            ValidationGroup="GridValidate" OnServerValidate="gridcvothhrstype_ServerValidate" ></asp:CustomValidator>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="gridlblOtherHrsType" runat="server" Text='<%# Bind("OtherHrstype") %>'></asp:Label>
                    </ItemTemplate>
                    <HeaderStyle CssClass="table3_labels" Font-Underline="True" />
                    <ItemStyle Wrap="True" HorizontalAlign="Center" CssClass="table3_gridfield" />
                    <ControlStyle Width="100%" />
                </asp:TemplateField>
 
JS Code:
<script language="javascript" type="text/javascript">
    function validategrdothhrstype(source,arguments)
            {
                if ((
                    document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("gridtxtOthHrs").value == 0) && (document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("DD_OthHourTypeedit").selectedindex = 0)
                ) 
                
                {
                    arguments.IsValid = true                          
                }
                else
               
                {
                    arguments.IsValid = false;
                    //document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("DD_OthHrsTypeEdit").focus ();
                    
 
                   }
            }
        </script>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

You have written = instead of == at the end of this line:

document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("gridtxtOthHrs").value == 0) && (document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("DD_OthHourTypeedit").selectedindex = 0)

Should be

document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("gridtxtOthHrs").value == 0) && (document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("DD_OthHourTypeedit").selectedindex == 0)

Hope this helps,

Chris.

Author

Commented:
It's still not working.  When I click the Edit button and change the value in the textbox and hit the Tab button it is still displaying the error even though there is a value selected.
Try some debugging alerts in you javascript logic code eg: <see code>

This will help narrow down where the logic is going wrong.

Chris.
alert (string( document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("gridtxtOthHrs").value ));
 
alert (string( document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("DD_OthHourTypeedit").selectedindex ));

Open in new window

Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Author

Commented:
Any particular place in the code that I put this?

Author

Commented:
Okay, so I'm looking over my code again and I think I'm missing some logic in my code.  I only want the error to show if the texbox <> 0 and the selected index of the dropdown list = 0.  I don't want the error to show if the textbox =0 or if the textbox <>0 and the selected index of the dropdown is <>0.  
Lets start again then.

Check the alerts as you go to make sure the results are what you expect.

Is the problem that you are testing the textbox for 0 (int) rather than '0' (string)?

If it still doesn't work the attached code will be much easier to debug for you.

Chris.


function validategrdothhrstype(source,arguments)
{
  var textBoxValue = document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("gridtxtOthHrs").value;
  alert(textBoxValue);
  var ddListIndex = document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("DD_OthHourTypeedit").selectedindex;
  alert(string(ddListIndex));
 
  if ( (textBoxValue != '0') && (ddListIndex == 0) )
  {
    alert("is not valid");
    arguments.IsValid = false;
  }
  else
  {
    alert(("is valid");
    arguments.IsValid = true;
  }
}

Open in new window

Author

Commented:
I got an Object is expected error.
Can you tell what bit threw up the error?

Check my spelling, it's all untested code.

Can you give me the url?

Chris.

Author

Commented:
It's an internal website
Did you get any of the alerts?

If not then my guess is the first alert threw the error, ie textBoxValue didn't get assigned properly.

I'd remove or comment out all your code and just leave:
function validategrdothhrstype(source,arguments)
{
  alert( string( document.getElementById("<%=TimecardGridview.ClientID %>").getElementsByTagName("gridtxtOthHrs").value ) );
  
}

Open in new window

Can you view the source of the rendered page and post the javascript function look like?

Author

Commented:
Same error.
can you put the rendered page code in a text file and post it for me?

Author

Commented:
Do you mean the View source page in the Internet browser or just the code from the page out of VS?
View source page in the Internet browser

Author

Commented:
Attached. Thanks.
viewsource.txt
Back in the morning.

Author

Commented:
Can anyone provide any help for me on this?  Thanks.

Author

Commented:
Still haven't gotten this working...any help would be greatly appreciated. Thanks.
Michel PlungjanIT Expert
Top Expert 2009

Commented:
I see more issues with == versus =

Here is one
document.getElementById("DD_CostCode").selectedIndex = 0
which needs to be
document.getElementById("DD_CostCode").selectedIndex == 0
Here is a simpler version

function ValidateCostCode(source,arguments) {
  arguments.IsValid = (document.getElementById("jobno").value == "" && 
      document.getElementById("DD_CostCode").selectedIndex == 0);
  if (!arguments.IsValid) document.getElementById("DD_CostCode").focus();
}

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial