how to use required field validator for dropdownlist?.
my dropdown list having
-Day-
item1
item2
Use InitialValue property as the default item that you don't want to be selected :
<asp:DropDownList ID="ddlItems" runat="server">
<asp:ListItem Text="-Day-" Value="-Day-"></asp:ListItem>
<asp:ListItem Text="Item 1" Value="Item 1"></asp:ListItem>
<asp:ListItem Text="Item 2" Value="Item 2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator runat="server" ID="RequiredFieldValidator1"
ErrorMessage="Validation Message !" ControlToValidate="ddlItems"
InitialValue="-Day-"></asp:RequiredFieldValidator>
You could use a CompareValidator like this:
<asp:DropDownList ID="ddlItems" runat="server">
<asp:ListItem Text="-Day-" Value="-1"></asp:ListItem>
<asp:ListItem Text="Item 1" Value="Item 1"></asp:ListItem>
<asp:ListItem Text="Item 2" Value="Item 2"></asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator runat="server" ID="CompareFieldValidator1"
ErrorMessage="Validation Message !" ControlToValidate="ddlItems"
ValueToCompare="-1" Operator="NotEqual"></asp:CompareValidator>
-Edoode
Related
Here is the code of dropdown in actually from dropdown only item selected by default when we click on droppdown but I want 2 items to be selected from the dropdown.
<asp:DropDownList runat="server" ID="dd1" >
<asp:ListItem Text="Male"></asp:ListItem>
<asp:ListItem Text="Female" Value="2"></asp:ListItem>
<asp:ListItem Text="yes" Value="3"></asp:ListItem>
<asp:ListItem Text="no" Value="4"></asp:ListItem>
<asp:ListItem Text="hello" Value="5"></asp:ListItem>
</asp:DropDownList>
Let's say I have this DDL on my page:
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
In this one, the required field will work without problem, if you don't select a value, then it will block the submit.
But what if the initial value is 0?
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="0" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
Now the "required" attribute doesn't work anymore.
What I want to do is put a pattern in the value to only accept values greater than zero, but I'm not familiar with RegExp, and I don't know if the HTML "pattern" attribute will do what I want.
Can someone enlighten me?
I would rather use a solution that uses pure HTML, but if a minimal programming is required, then I don't have choice...
If you just want to write mark up the RequiredFieldValidator should work
<asp:DropDownList ID="senha" runat="server" required="required" CssClass="form-control">
<asp:ListItem Value="0" Text="empty" Selected="True"></asp:ListItem>
<asp:ListItem Value="1" Text="test1"></asp:ListItem>
<asp:ListItem Value="2" Text="test2"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="reqSenha" runat="server" SetFocusOnError="true" InitialValue="0" ErrorMessage="*" ControlToValidate="senha" />
I have a datagrid that has dropdowns in one column :
<asp:TemplateColumn HeaderText="">
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" ID="lblResultId" Visible="false" text='<%# DataBinder.Eval(Container, "DataItem.CurrentId", "{0:g}") %>'/>
<asp:DropDownList ID="ddlCurrentValue" DataTextField="Description" DataValueField="Id" Runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlCurrentValue_OnSelectedIndexChanged">
<asp:ListItem Text="" Value="0" Selected="True"></asp:ListItem>
<asp:ListItem Text="1" Value="1">1</asp:ListItem>
<asp:ListItem Text="2" Value="2">2</asp:ListItem>
<asp:ListItem Text="3" Value="3">3</asp:ListItem>
<asp:ListItem Text="4" Value="4">4</asp:ListItem>
<asp:ListItem Text="5" Value="5">5</asp:ListItem>
<asp:ListItem Text="6" Value="6">6</asp:ListItem>
<asp:ListItem Text="7" Value="7">7</asp:ListItem>
<asp:ListItem Text="8" Value="8">8</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
I set the selected value for each dropdown based on a value taken from the database on ItemDataBound.
Now if the user changes the value of one of the dropdowns, i would like to save the value from that dropdown.
My id is that i have the database-id in lblResultId and use that to save the selectedvalue from the dropdown to the database via a linq query. However, i am already stuck on the 'onselectedindexchanged' for the dropdownlists not firing.....
Any suggestions how to procede or how to do this differently ?
I have 3 dropdowns and each one is populated with same set of values say a,b and c
eg: if i select 'a' from the dropdown and there is another dropdown with the same value 'a' selected. i should get an err msg.
How is this possible.
Use CompareValidator control like this:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddl1" runat="server">
<asp:ListItem Text="a" Value="a"></asp:ListItem>
<asp:ListItem Text="b" Value="b"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl2" runat="server">
<asp:ListItem Text="a" Value="a"></asp:ListItem>
<asp:ListItem Text="b" Value="b"></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ddl3" runat="server">
<asp:ListItem Text="a" Value="a"></asp:ListItem>
<asp:ListItem Text="b" Value="b"></asp:ListItem>
</asp:DropDownList>
<asp:CompareValidator ID="CompareValidator1" runat="server"
ErrorMessage="DropDownList 1 can't be equal DropDownList 2"
ControlToCompare="ddl1" ControlToValidate="ddl2" Operator="NotEqual"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator2" runat="server"
ErrorMessage="DropDownList 2 can't be equal DropDownList 3"
ControlToCompare="ddl2" ControlToValidate="ddl3" Operator="NotEqual"></asp:CompareValidator>
<asp:CompareValidator ID="CompareValidator3" runat="server"
ErrorMessage="DropDownList 1 can't be equal DropDownList 3"
ControlToCompare="ddl2" ControlToValidate="ddl3" Operator="NotEqual"></asp:CompareValidator>
</div>
</form>
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