• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 965
  • Last Modified:

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
0
newjeep19
Asked:
newjeep19
  • 9
  • 2
  • 2
  • +1
1 Solution
 
newjeep19Author Commented:
Please help
0
 
MrunalCommented:
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.

0
 
newjeep19Author Commented:
Can you provide me an example in JavaScript or how to bind a blur event
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
newjeep19Author Commented:
using the code that i have above how can I use JavaScript?
0
 
MrunalCommented:
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>
0
 
newjeep19Author Commented:
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
0
 
newjeep19Author Commented:
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
0
 
jagssiduralaCommented:
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>
0
 
newjeep19Author Commented:
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.
0
 
jagssiduralaCommented:
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......
0
 
newjeep19Author Commented:
Added the new code and still no validation occured
0
 
newjeep19Author Commented:
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
0
 
newjeep19Author Commented:
I figured out a way using my button click event for my submit button:
Solution:

 if (r.RowType == DataControlRowType.DataRow)
                        {
                            // Tracing Type validator
                            TextBox txtTrackingNumb = (TextBox)r.FindControl("txtTrackingNumb");
                            DropDownList ddlTrackingType = (DropDownList)r.FindControl("ddlTrackingType");
                            string tracType = String.Empty;
                            if (!String.IsNullOrEmpty(txtTrackingNumb.Text))
                            {
                                tracType = ddlTrackingType.SelectedValue;
                                if (tracType == "0")
                                {
                                    //message: Need to enter in a tracking type (1,2,3) for the tracking
                                    lblMessage.Text = "You must enter in a Tracking Type (FedEx, UPS or Other) for Tracking # :  " + txtTrackingNumb.Text;
                                    lblMessage.ForeColor = System.Drawing.Color.Red;
                                    return;
                                }
                            }
0
 
Amandeep Singh BhullarCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 9
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now