Validating dropdownlist inside gridview control - asp.net

for testing i have tried customvalidation
function ClientValidate(sender, args) {
//return false for testing...
args.IsValid = false;
}
<asp:CustomValidator runat="server" ID="CustomValidator1" ControlToValidate="ddldetail"
Text="Please select" ValidateEmptyText="true"
ClientValidationFunction="ClientValidate"
Display="Dynamic">
</asp:CustomValidator>
edit: here is what i exaclty wants to happen:
how to validate a dropdownlist and i have done this zillion times but what i am doing wrong here? any second pair of eye might spot it? i am trying to validate the dropdownlist if the user have not select any help?
<asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" CausesValidation="true"/>
<asp:GridView ID="GVInputMapping" runat="server" AutoGenerateColumns="False" DataKeyNames="Id"
EnableModelValidation="True" onrowdatabound="GVInputMapping_RowDataBound">
<Columns>
<asp:BoundField DataField="Name" ControlStyle-Width="250px" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList runat="server" ID="ddldetail">
<asp:ListItem Selected="True" Value="0">Select me</asp:ListItem>
<asp:ListItem Value="1">abc</asp:ListItem>
<asp:ListItem Value="2">GHt</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="requiredDDL" runat="server"
ControlToValidate="ddldetail" ErrorMessage="Please select" InitialValue="Select me" Display="Dynamic"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

You have the InitialValue of the validator set to Select me, but the Value of that item is actually 0:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Text="Select Me" Value="0" />
<asp:ListItem Text="Foo" Value="1" />
<asp:ListItem Text="Bar" Value="2" />
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="DropDownList1"
ErrorMessage="Please select"
InitialValue="0"
Display="Dynamic">
</asp:RequiredFieldValidator>
You'll also need to assign a unique validation group for each row, otherwise validation will kick off for every row. To assign a unique validation group, you can use the Id column:
ValidationGroup='<%# string.Format("Group_{0}", Eval("Id")) %>'
You would add this to the validator and the button in the row.

Related

ASP.NET RequiredFieldValidator : How can i bind value to " InitialValue " property?

I have the following code
<div>
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue="Choose" ControlToValidate="testDropDownList"></asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
<br />
</div>
in which i validate Dropdownlist by RequiredFieldValidator
If changed the value of initialvalue property to read from static property in stactic classs .. but it always give me emtpy string in runtime unless this property have the value "Choose" ...
<asp:DropDownList ID="testDropDownList" runat="server" ValidationGroup="testValidationGroup">
<asp:ListItem Value="Choose">[ Select Item ... ]</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="testRequiredFieldValidator" runat="server" ValidationGroup="testValidationGroup"
ErrorMessage="*" InitialValue='<%# Util.ChooseValue %>' ControlToValidate="testDropDownList">
</asp:RequiredFieldValidator>
<br />
<asp:Button ID="testButton" runat="server" OnClick="testButton_Click" Text="Button"
ValidationGroup="testValidationGroup" />
Could Any one help me to know what's the issue in my code ??
Please call
testRequiredFieldValidator.databind()
in page load event and let me know if this is still an issue.
Set InitialValue of RequiredFieldValidator on page_load event in aspx.cs page.

Calling a RequiredFieldValidator and ValidatorCallExtender inside GridView

I have a GridView with its columns being TextBoxes and after user made modifications to data within GridView I have a "Commit" Button thats located outside the GridView and also located under a different Content within the page... How do I set the Validator to work even though I have the validator set correctly but when I click on the "Commit" button it doesn't check for the validation and I believe its because the button is not inside the GridView or UpdatePanel... Is there a way to get around that? or A better approach?
Thanks for your help in advance.
Please select a Test from the dropdown below.<br />
<asp:DropDownList ID="ddlResult" runat="server"
onselectedindexchanged="ddlResult_SelectedIndexChanged"
AutoPostBack="True" CausesValidation="false">
</asp:DropDownList>
<br />
<asp:UpdatePanel ID="upGrid" runat="server">
<ContentTemplate>
<asp:GridView ID="grdResults" runat="server"
CssClass="gridview"
RowStyle-CssClass="gridview_itm"
AlternatingRowStyle-CssClass="gridview_aitm"
HeaderStyle-CssClass="gridview_hdr"
Width="100%" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" Text='<%#Eval("Test")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Result">
<ItemTemplate>
<asp:TextBox ID="tbResult" runat="server" Text='<%#Request.QueryString["t_ID"] == null ? null : Eval("n_Result") %>'></asp:TextBox>
<asp:Label ID="lblResult" runat="server" Text='<%#Eval("Validate")%>' ForeColor="#D50000"></asp:Label>
<asp:FilteredTextBoxExtender ID="ftbe" runat="server" TargetControlID="tbResult" FilterType="Custom, Numbers" ValidChars='<%#Eval("n_Mask")%>' />
<asp:RequiredFieldValidator runat="server" ID="RReq"
ControlToValidate="tbResult"
Display="None"
ErrorMessage="A Result is required." />
<asp:ValidatorCalloutExtender runat="Server" ID="RReqE"
TargetControlID="RReq"
HighlightCssClass="validatorCalloutHighlight" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Completed">
<ItemTemplate>
<asp:TextBox ID="tbDate" runat="server" Text='<%#Request.QueryString["t_ID"] == null ? null : Eval("d_DateCompleted") %>'></asp:TextBox>
<asp:Image ID="imgCalendar" runat="server" ImageUrl="~/App_Themes/Sugar2006/images/Calendar_scheduleHS.png" ImageAlign="Middle" />
<asp:CalendarExtender ID="ce" runat="server" TargetControlID ="tbDate" PopupButtonID="imgCalendar" />
<asp:MaskedEditExtender ID="mex" runat="server"
TargetControlID="tbDate"
Mask="99/99/9999"
MaskType="Date"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError" />
<asp:MaskedEditValidator ID="mev" runat="server"
ControlToValidate="tbDate"
ControlExtender="mex"
Display="Dynamic"
InvalidValueMessage="This date is invalid!" Font-Bold="True"
ForeColor="#D50000" />
<asp:RequiredFieldValidator runat="server" ID="DReq"
ControlToValidate="tbDate"
Display="None"
ErrorMessage="A Date is required." />
<asp:ValidatorCalloutExtender runat="Server" ID="DReqE"
TargetControlID="DReq"
HighlightCssClass="validatorCalloutHighlight" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Label ID="lblSave" runat="server" Text="**After Each Test Entry Please Save." ForeColor="#D50000"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlResult" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
<asp:Content ID="Content1" ContentPlaceHolderID="cntSidebar" runat="server">
<asp:Button ID="btnSave" runat="server" Text="Save" Width="80%"
onclick="btnSave_Click"/> <br />
<asp:Button ID="btnClose" runat="server" Text="Close" Width="80%"
onclick="btnClose_Click" CausesValidation="false"/>
</asp:Content>
Have you tried setting the following properties on the Commit button:
CausesValidation="true" ValidationGroup="vgMyGroup"
Also, try setting the validation controls property: ValidationGroup="vgMyGroup"
Another suggestion could be to do a postback on the Commit button's click event and check for
if(Page.IsValid)...
I would be interested to see if setting the ValidationGroup property persists across the ASP content controls..

Isolate a required field validator?

I have two search buttons on a page, one linked to a dropdown list and one linked to a dropdown list with a textbox for more search criteria. I have required field validators on all of the aforementioned controls. When I choose something from the first dropdown and click the appropriate search button, the field validator for the textbox fires, disabling the first search button. Is there a way to localize/isolate the validators to only associate with one of the two buttons? Code below:
<asp:Panel ID="Panel1" runat="server" HorizontalAlign="Center">
<asp:Label ID="Label1" runat="server" Text="Search by status:"></asp:Label>
<asp:DropDownList ID="DdlStatus" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Status" DataValueField="Status" AppendDataBoundItems="true">
<asp:ListItem Text="Choose a status" Value="0" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="BtnStatusSearch" runat="server" Text="Search" onclick="BtnStatusSearch_Click" />
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [Status] FROM [Status]"></asp:SqlDataSource>
<asp:Label ID="LblSearch" runat="server" Text="Other search:"></asp:Label>
<asp:DropDownList ID="DdlSearch" runat="server">
<asp:ListItem Selected="True" Value="0">Choose search criteria</asp:ListItem>
<asp:ListItem Value="1">Broker</asp:ListItem>
<asp:ListItem Value="2">Customer</asp:ListItem>
<asp:ListItem Value="3">Customer State</asp:ListItem>
<asp:ListItem Value="4">Broker State</asp:ListItem>
</asp:DropDownList><asp:RequiredFieldValidator ID="RfvDdlSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="DdlSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:TextBox ID="TbSearch" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvTbSearch" runat="server" Display="Dynamic"
ErrorMessage="Required field" ControlToValidate="TbSearch" CssClass="ErrorMessage"></asp:RequiredFieldValidator>
<asp:Button ID="BtnSearch" runat="server" onclick="BtnSearch_Click" Text="Search" />
Yes, you can use the ValidationGroup property and set that validation group to your button control: ValidationGroup="button1"
<asp:RequiredFieldValidator ID="rfv" runat="server" ValidationGroup="button1"
ErrorMessage="*"></asp:RequiredFieldValidator>
<asp:Button ID="btnLogin" runat="server" Text="Login" ValidationGroup="button1" OnClick="btnLogin_Click" />

asp.net Validation on DropDownList box

I have a dropdownlist (cboViewAlbums) which has displays album values. The first item is a
Please select an album.... I am trying to use validation which when lb_create_album linkButton is clicked throws an error if the cboViewAlbums list has the value 0 selected.
Below is the code for the this and my attempt:
<asp:DropDownList ID="cboViewAlbums" runat="server"
DataSourceID="SqlDataSource1" DataTextField="album_name"
DataValueField="album_id" Width="250px" AutoPostBack="True" AppendDataBoundItems="true">
<asp:ListItem Value="0">Please select an album...</asp:ListItem>
</asp:DropDownList>
<asp:LinkButton ID="lb_create_album" runat="server">Create Album</asp:LinkButton>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:fpaConnectionString %>"
SelectCommand="SELECT [album_id], [album_name] FROM [fpa_albums] ORDER BY [album_name]">
</asp:SqlDataSource>
<br />
<asp:HyperLink CssClass="example7" ID="hLinkUploadPhotos" NavigateUrl="multiple_upload.aspx" runat="server">Upload Multiple Photos</asp:HyperLink>
<br />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="Please select an album..."></asp:RequiredFieldValidator>
Any idea how I can get this working?
Thanks
You have to use Range validator with dropdown list & set mininmum value greater then 0 & maximum value to set any max value , aslo provide type value of min & max and that is integer.
Below is the sample code i have make for you you have to bind your datasource insted of static list items.
<asp:DropDownList runat="server" ID="ddl1" >
<asp:ListItem Value="0" Text="Select value" />
<asp:ListItem Value="1" Text="text1" />
<asp:ListItem Value="2" Text="text2" />
</asp:DropDownList>
<asp:RangeValidator ErrorMessage="Please select value" ControlToValidate="ddl1" runat="server"
MinimumValue="1" MaximumValue="100000000" Type=Integer />
<asp:Button Text="text" runat="server" />
If this helpful to you please mark as an answer
Thanks
Arun
First, you can't do validation with HyperLink, better use the LinkButton. HyperLink does not do post back, so that's your first error.
Second, on your RequiredFieldValidator, put the initialvalue=0, and that should fix your problem.
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="cboViewAlbums" ErrorMessage="Please Select an Album"
InitialValue="0"></asp:RequiredFieldValidator>

Databinding of RadioButtonList using SelectedValue...possible?

I am databinding a GridView to an object datasource. The gridview contains a TemplateField which contains a RadioButtonList with ListItems defined inline.
I want to be able to databind the SelectedValue of the RadioButtonList to the same underlying table as the other grid columns, but it doesn't work!
Do I have my syntax wrong, or is this impossible and requires looping code to individually select the proper item in each row?
<llblgenpro:LLBLGenProDataSource ID="llbComputerApplication" DataContainerType="EntityCollection" runat="server"></llblgenpro:LLBLGenProDataSource>
<asp:GridView ID="gridComputerApps" DataSourceID="llbComputerApplication" runat="server" AutoGenerateColumns="False"
EmptyDataText ="NO APPLICATIONS FOUND FOR THIS COMPUTER."
DataKeyNames="ComputerID, ApplicationID" EnableViewState="False"
style="border-style:dotted;border-width:thin"
>
<Columns>
<asp:BoundField DataField="ApplicationID" HeaderText="Application ID" SortExpression="ApplicationID" Visible="True" />
<asp:TemplateField HeaderText="Application Name"><ItemTemplate><%#Eval("Application.ApplicationName")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Normalized Name"><ItemTemplate><%#Eval("Application.NormalizedAppName")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Notes"><ItemTemplate><%#Eval("Application.NormalizedNotes")%></ItemTemplate></asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<asp:RadioButtonList SelectedValue='<%#Eval("RequirementOption")%>' ID="rblRequirementOption" RepeatDirection="Horizontal" runat="server">
<asp:ListItem Value="Need Now" Text="Need Now"></asp:ListItem>
<asp:ListItem Value="Need Someday" Text="Need Someday"></asp:ListItem>
<asp:ListItem Value="Do Not Need" Text="Do Not Need"></asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="NormalizedNotes" HeaderText="Notes" Visible="False" />
</Columns>
</asp:GridView>
What you have should work. Are you getting an error? Here's a working example copied from my current project. I'm binding to a nullable bit field - so have a hidden list item to accept the nulls.
<asp:RadioButtonList runat="server" ID="MyRbl" SelectedValue='<%# Bind("MyRblField") %>'
CssClass="NormalTextBox" RepeatDirection="Horizontal">
<asp:ListItem Value="false" Text="No" />
<asp:ListItem Value="true" Text="Yes" />
<asp:ListItem Value="" Text="" style="display: none" />
</asp:RadioButtonList>
I also experienced this problem (nothing selected in radiobuttonlist) when binding against boolean values in MS SQL:
radDefault.Items.Add(new ListItem("Yes", "true"));
radDefault.Items.Add(new ListItem("No", "false"));
In my case, the solution was to capitalize the first letter of the true/false values, then the radiobuttonlistworked as expected:
radDefault.Items.Add(new ListItem("Yes", "True"));
radDefault.Items.Add(new ListItem("No", "False"));
Or, declaratively:
<asp:RadioButtonList runat="server" ID="radDefault" SelectedValue='<%# Bind("DB_FIELD") %>'>
<asp:ListItem Value="False" Text="No" />
<asp:ListItem Value="True" Text="Yes" />
</asp:RadioButtonList>
I didn't like the idea of using css to hide an item. Instead, I found this solution to add a blank item but remove it in the code behind.
<asp:RadioButtonList ID="MyRadioButtonList" runat="server"
SelectedValue='<%# Bind("Blah") %>'
OnDataBound="MyRadioButtonList_DataBound">
<asp:ListItem Value=""></asp:ListItem>
<asp:ListItem Value="A"></asp:ListItem>
<asp:ListItem Value="B"></asp:ListItem>
<asp:ListItem Value="C"></asp:ListItem>
</asp:RadioButtonList>
protected void MyRadioButtonList_DataBound(object sender, EventArgs e)
{
RadioButtonList list = (RadioButtonList)sender;
ListItem blank = list.Items.FindByValue("");
if (blank != null)
list.Items.Remove(blank);
}
<asp:RadioButtonList runat="server" ID="MyRbl" SelectedValue='<%# Bind("MyRblField") %>'
CssClass="NormalTextBox" RepeatDirection="Horizontal">
<asp:ListItem Value="false" Text="No" />
<asp:ListItem Value="true" Text="Yes" />
<asp:ListItem Value="" Text="" selected="true" style="display: none" />
</asp:RadioButtonList>
It work me .....
gnanasekar.s vilangulathur

Resources