Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Compare validator in an ASP.NET Gridview

Posted on 2011-10-11
15
Medium Priority
?
912 Views
Last Modified: 2012-05-12
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
Comment
Question by:newjeep19
  • 9
  • 2
  • 2
  • +1
14 Comments
 

Author Comment

by:newjeep19
ID: 36952279
Please help
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 36955458
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
 

Author Comment

by:newjeep19
ID: 36955714
Can you provide me an example in JavaScript or how to bind a blur event
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:newjeep19
ID: 36955768
using the code that i have above how can I use JavaScript?
0
 
LVL 22

Expert Comment

by:Mrunal
ID: 36956502
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
 

Author Comment

by:newjeep19
ID: 36956567
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
 

Author Comment

by:newjeep19
ID: 36956594
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
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36957923
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
 

Author Comment

by:newjeep19
ID: 36957963
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
 
LVL 12

Expert Comment

by:jagssidurala
ID: 36957994
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
 

Author Comment

by:newjeep19
ID: 36958040
Added the new code and still no validation occured
0
 

Author Comment

by:newjeep19
ID: 36958116
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
 

Accepted Solution

by:
newjeep19 earned 0 total points
ID: 36965049
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
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 37936086
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question