Link to home
Start Free TrialLog in
Avatar of newjeep19
newjeep19Flag for United States of America

asked on

Compare validator in an ASP.NET Gridview

I have a asp.NET gridview that i need to validate a textbox if the textbox has a value (data) then a dropdown list option must be selected. if no data in the textbox then the ddl does not need a value to be selected from is options.
Problem:
I have a compare validator however, the validator requiers for all the rows to have a ddl selection made even though only one textbox in one row has a value (data) in it and not any of the other textboxes in the other rows in the datagrid
Code ASPX
 <asp:TemplateField HeaderText="Tracking #">
                    <ItemTemplate>
                      <asp:TextBox ID="txtTrackingNumb" runat="server" Text='<%# Eval("GTRI_TrackingNumbers").ToString() %>' />
                    </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Tracking Type">
                  <ItemTemplate>
                  <asp:DropDownList ID="ddlTrackingType" runat="server" AutoPostBack="false" SelectedValue='<%# Eval("Gtri_TrackingType") %>'>
                   <asp:ListItem Text="Select..." Value="0" />
                   <asp:ListItem Text="Fed Ex" Value="1" />  
                   <asp:ListItem Text="UPS" Value="2" />  
                   <asp:ListItem Text="Other" Value="3" />                            
                  </asp:DropDownList>

                  <%--<asp:CustomValidator ID="CustomValidator2" runat="server" ControlToValidate="ddlTrackingType" ControlToCompare="txtTrackingNumb" ClientValidationFunction="ClientValidate2" OnServerValidate="ServerValidate2" Display="Static" Font-Names="verdana" Font-Size="10pt">Required!</asp:CustomValidator>--%>
                  <asp:CompareValidator id="CompareValidator1" ControlToValidate="ddlTrackingType" ControlToCompare="txtTrackingNumb" Text="Required!" runat="server" />
                  </ItemTemplate>
                  </asp:TemplateField>

Please help
Avatar of newjeep19
newjeep19
Flag of United States of America image

ASKER

Please help
Avatar of Mrunal
hi
you can do it with client side code like with jQuery or Java-Script.

you can bind blur event to textbox to enable dropdown and make that dropdown disabled by default.
Hope this matches your requirement - you have to enter something before selecting dropdown value.

Can you provide me an example in JavaScript or how to bind a blur event
using the code that i have above how can I use JavaScript?
1. Add reference for jQuery js file.
2. Add cssclass to textbox 'txtTrackingNumb' as 'TrackingNo'
3. Write this code in script tag as:

<script type="text/javascript">
        $(document).ready(function() {
            $('.TrackingNo').live('blur', function() {
                //alert(1);
                if (jQuery.trim($(this).val()) != '')
                    $(this).parent().find('select[id$="ddlTrackingType"]').attr("disabled", "disabled");
                else
                    $(this).parent().find('select[id$="ddlTrackingType"]').removeAttr("disabled");
            });
        });    
    </script>
Thank you for your response......I am new to ASP.NET and I have not worked much with JavaScript please so me how to add a reference for jQuery js file.
Thank you
Also when I click on my submit button all of the dropdown lists in all of the rows in the gridview still show the compare validator error message. So, do i need to remove the asp.net compare validator contol in asp.net?
Thank you
Change code as below

<asp:TemplateField HeaderText="Tracking #">
                    <ItemTemplate>
                      <asp:TextBox ID="txtTrackingNumb" runat="server" Text='<%# Eval("GTRI_TrackingNumbers").ToString() %>' onblur="fncvalidate(this.id)" />
                    </ItemTemplate>
                  </asp:TemplateField>
                  <asp:TemplateField HeaderText="Tracking Type">
                  <ItemTemplate>
                  <asp:DropDownList ID="ddlTrackingType" runat="server" AutoPostBack="false" SelectedValue='<%# Eval("Gtri_TrackingType") %>'>
                   <asp:ListItem Text="Select..." Value="0" />
                   <asp:ListItem Text="Fed Ex" Value="1" />  
                   <asp:ListItem Text="UPS" Value="2" />  
                   <asp:ListItem Text="Other" Value="3" />                            
                  </asp:DropDownList>
<asp:RequiredFieldValidator id="rfv" ControlToValidate="ddlTrackingType" ErrorMessage="Required!" runat="server" Enabled="false" ></asp:RequiredFieldValidator>
                  </ItemTemplate>
                  </asp:TemplateField>



write a script function like below in your aspx head section

<script>
function fncvalidate(txtid)
{
  if(document.getElementById(txtid).value != "")
{
 ValidatorEnable(document.getElementById(txtid.replace('txtTrackingNumb','rfv')), true);
}
else
{
ValidatorEnable(document.getElementById(txtid.replace('txtTrackingNumb','rfv')),false);
}
}
</script>
I added the above code and made the changes and when I typed in the tracking number no validation (error: message) and when i proceeded and clicked the submit butto agian no valadaion occurred.
Sorry........we have to give intialvalue property to dropdown as below

<asp:RequiredFieldValidator id="rfv" IntialValue="Select..." ControlToValidate="ddlTrackingType" ErrorMessage="Required!" runat="server" Enabled="false" ></asp:RequiredFieldValidator>

Try this......
Added the new code and still no validation occured
hmmm OK I noticed that in my error handling for inserting / updating data from my gridview back to the db (Try/Catch block) I do see   (catch (SoapException ex) { string s = ex.Detail.InnerText; } break;) that i get an error message and the database is not updated because the dropdown list item was not selected.......but I still need for an error message to write out that the user when they entered in a tracking number must select a tracking type i.e (Fed Ex, UPS, etc) before they can update or insert the record into the database
ASKER CERTIFIED SOLUTION
Avatar of newjeep19
newjeep19
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.