newjeep19
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 ").ToStrin g() %>' />
</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="ddlTrac kingType" ControlToCompare="txtTrack ingNumb" ClientValidationFunction=" ClientVali date2" OnServerValidate="ServerVa lidate2" Display="Static" Font-Names="verdana" Font-Size="10pt">Required! </asp:Cust omValidato r>--%>
<asp:CompareValidator id="CompareValidator1" ControlToValidate="ddlTrac kingType" ControlToCompare="txtTrack ingNumb" Text="Required!" runat="server" />
</ItemTemplate>
</asp:TemplateField>
Please help
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
</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="ddlTrac
<asp:CompareValidator id="CompareValidator1" ControlToValidate="ddlTrac
</ItemTemplate>
</asp:TemplateField>
Please help
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.
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.
ASKER
Can you provide me an example in JavaScript or how to bind a blur event
ASKER
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('blu r', function() {
//alert(1);
if (jQuery.trim($(this).val() ) != '')
$(this).parent().find('sel ect[id$="d dlTracking Type"]').a ttr("disab led", "disabled");
else
$(this).parent().find('sel ect[id$="d dlTracking Type"]').r emoveAttr( "disabled" );
});
});
</script>
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('blu
//alert(1);
if (jQuery.trim($(this).val()
$(this).parent().find('sel
else
$(this).parent().find('sel
});
});
</script>
ASKER
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
Thank you
ASKER
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
Thank you
Change code as below
<asp:TemplateField HeaderText="Tracking #">
<ItemTemplate>
<asp:TextBox ID="txtTrackingNumb" runat="server" Text='<%# Eval("GTRI_TrackingNumbers ").ToStrin g() %>' onblur="fncvalidate(this.i d)" />
</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:RequiredFieldValidato r id="rfv" ControlToValidate="ddlTrac kingType" ErrorMessage="Required!" runat="server" Enabled="false" ></asp:RequiredFieldValida tor>
</ItemTemplate>
</asp:TemplateField>
write a script function like below in your aspx head section
<script>
function fncvalidate(txtid)
{
if(document.getElementById (txtid).va lue != "")
{
ValidatorEnable(document.g etElementB yId(txtid. replace('t xtTracking Numb','rfv ')), true);
}
else
{
ValidatorEnable(document.g etElementB yId(txtid. replace('t xtTracking Numb','rfv ')),false) ;
}
}
</script>
<asp:TemplateField HeaderText="Tracking #">
<ItemTemplate>
<asp:TextBox ID="txtTrackingNumb" runat="server" Text='<%# Eval("GTRI_TrackingNumbers
</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:RequiredFieldValidato
</ItemTemplate>
</asp:TemplateField>
write a script function like below in your aspx head section
<script>
function fncvalidate(txtid)
{
if(document.getElementById
{
ValidatorEnable(document.g
}
else
{
ValidatorEnable(document.g
}
}
</script>
ASKER
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:RequiredFieldValidato r id="rfv" IntialValue="Select..." ControlToValidate="ddlTrac kingType" ErrorMessage="Required!" runat="server" Enabled="false" ></asp:RequiredFieldValida tor>
Try this......
<asp:RequiredFieldValidato
Try this......
ASKER
Added the new code and still no validation occured
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
ASKER