HTML5 "required" attribute on asp:DropDownList - asp.net

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" />

Related

how to create a dropdown list with checkbox in user control page(ascx) in asp.net

How to add Drop Down List with checkbox, multiple selection from drop down in asp.net c#.
I want to select multiple items from drop down control inside the user control page
You can achieve this by making selection mode to multiple:
<asp:ListBox ID="lstFruits" runat="server" SelectionMode="Multiple">
<asp:ListItem Text="Mango" Value="1" />
<asp:ListItem Text="Apple" Value="2" />
<asp:ListItem Text="Banana" Value="3" />
<asp:ListItem Text="Guava" Value="4" />
<asp:ListItem Text="Orange" Value="5" />
</asp:ListBox>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
for Reference see the following link
here

How to add ajax combobox properly in asp.net webform application

Right now i'm trying to use combobox in the application i'm working by following this page.
But when i run the page, only the dropdown is working but the auto suggestion feature is not working and also the text field is not editable. So how to do it?
Here is the code i'm using:
<cc1:ComboBox ID="ComboBox1" runat="server"
AutoPostBack="True"
DropDownStyle="DropDownList"
AutoCompleteMode="SuggestAppend"
CaseSensitive="False"
CssClass=""
ItemInsertLocation="Append" >
<asp:ListItem Text="Mild" Value="0" />
<asp:ListItem Text="Medium" Value="1" />
<asp:ListItem Text="Hot" Value="2" />
</cc1:ComboBox>
Change your DropDownStyle to Simple or DropDown and you'll get the desired result, here is the updated Code:
<cc1:ComboBox
ID="ComboBox1"
runat="server"
AutoPostBack="True"
DropDownStyle="Simple"
AutoCompleteMode="SuggestAppend"
CaseSensitive="False"
CssClass=""
ItemInsertLocation="Append">
<asp:ListItem Text="Mild" Value="0" />
<asp:ListItem Text="Medium" Value="1" />
<asp:ListItem Text="Hot" Value="2" />
</cc1:ComboBox>

How to bind dropdownlist using asp.net c#?

<asp:DropDownList id="ddlSampleSize" runat="server">
<asp:ListItem Text="10" Value="10">
<asp:ListItem Text="100" Value="100">
<asp:ListItem Text="1000" Value="1000">
<asp:DropDownList/>
I have bind from codebehind file but I can't see same.
Please help me.
You need to use some sort of datasource to bind. You are just showing static bound data here.
In markup:
<asp:DropDownList id="ddlSampleSize" runat="server">
<asp:ListItem Text="10" Value="10" />
<asp:ListItem Text="100" Value="100" />
<asp:ListItem Text="1000" Value="1000" />
</asp:DropDownList>
In code-behind:
ddlSampleSize.Items.Add(new ListItem("10", "10"));
ddlSampleSize.Items.Add(new ListItem("100", "100"));
ddlSampleSize.Items.Add(new ListItem("1000", "1000"));

ASP.NET RadioButtonList: the element 'br' cannot be nested within the element 'listitem'

for example i have this radio button.
(o) item1 (o) item 2
however, i want it to be like:
(o) item1 (o) item2
line here line here
My current markup is:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" Width="650px" Font-Size="Smaller">
<asp:ListItem Selected="True" Value="0">Relax<br>(MY MAN)</asp:ListItem>
<asp:ListItem Value="1">Relax Again<br>(My Man)</asp:ListItem>
<asp:ListItem Value="2">Relax Again 2<br>(MyMan)</asp:ListItem>
<asp:ListItem Value="3">Relax 3<br>(My Man)</asp:ListItem>
<asp:ListItem Value="4">Relax 4<br>(My Man)</asp:ListItem>
<asp:ListItem Value="5">Relax 5<br>(My Man)</asp:ListItem>
<asp:ListItem Value="6">Relax 6<br>(My Man)</asp:ListItem>
</asp:RadioButtonList>
But it generates the following validation error messages: the element 'br' cannot be nested within the element 'listitem'.
Note: the solution given here actually works but is more complicated than necessary. See J Talati's answer to his own post for the best answer.
If you want to have a markup without errors, you can reproduce the RadioButtonList with separate RadioButtons sharing the same GroupName, in a table (which is the way it is rendered by the RadioButtonList):
<table id="RadioButtonTable1" runat="server" class="radioList">
<tr>
<td>
<asp:RadioButton runat="server" GroupName="RadioList" Checked="true" Value="0" Text="Relax<br />(MY MAN)" />
</td>
<td>
<asp:RadioButton runat="server" GroupName="RadioList" Value="1" Text="Relax Again<br />(My Man)" />
</td>
<td>
<asp:RadioButton runat="server" GroupName="RadioList" Value="2" Text="Relax Again 2<br />(MyMan)" />
</td>
...
</tr>
</table>
The radioList class style allows to customize the layout:
.radioList td
{
text-align: left;
width: 80px;
}
Now, getting the selected value is not as simple as for a RadioButtonList. You can use the class provided by Jimmy in Better way to find control in ASP.NET:
ControlFinder<RadioButton> radios = new ControlFinder<RadioButton>();
radios.FindChildControlsRecursive(RadioButtonTable1);
foreach (RadioButton rb in radios.FoundControls)
{
if (rb.Checked)
{
string value = rb.Attributes["Value"];
...
break;
}
}
The RadioButtons don't have a Value property but the attribute can be added in markup and retrieved in code-behind as indicated.
<asp:radiobuttonlist id="RadioButtonList1" runat="server" repeatdirection="Horizontal" width="650px" font-size="Smaller" xmlns:asp="#unknown">
<asp:listitem selected="True" value="0" text="Relax <br>(MY MAN)"></asp:listitem>
<asp:listitem value="1" text="Relax Again<br>(My Man)"></asp:listitem>
<asp:listitem value="2" text="Relax Again 2<br>(MyMan)"></asp:listitem>
<asp:listitem value="3" text="Relax 3<br>(My Man)"></asp:listitem>
<asp:listitem value="4" text="Relax 4<br>(My Man)"></asp:listitem>
<asp:listitem value="5" text="Relax 5<br>(My Man)"></asp:listitem>
<asp:listitem value="6" text="Relax 6<br>(My Man)"></asp:listitem>
Got this to work! Woohoo! just add text property thats all.

RequiredFieldvalidator

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

Resources