How to make an asp dropdown list item unselectable - c#

I have written following code to display drop down list :
<asp:DropDownList AutoPostBack="True" ID="ddlCities" runat="server" class="form-control input-sm" placeholder="" TabIndex="5">
<asp:ListItem>Select City</asp:ListItem>
<asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>
<asp:ListItem value="3_3004"> Ahmedabad East</asp:ListItem>
<asp:ListItem value="3_3005"> Ahmedabad West</asp:ListItem>
<asp:ListItem value="3_3006"> Ahmedabad-Bopal and Surroundings</asp:ListItem>
<asp:ListItem value="3_3007"> Ahmedabad-Gandhinagar</asp:ListItem>
<asp:ListItem value="3_3008"> Ahmedabad-Sabarmati and Surroundings</asp:ListItem>
<asp:ListItem value="3_3009"> Ahmedabad-SG Highway and Surroundings</asp:ListItem>
</asp:DropDownList>
I want to make following item inside dropdown unselectable :
<asp:ListItem value="3">Ahmedabad (All) ---------------</asp:ListItem>
Note : I CAN'T USE OPTGROUP!!!!
and i don't want to hide it. it will be shown in dropdown, but user can't select this item.
I have tried adding 'disabled' attribute, but it hides that item.
i have also tried :
ddlCities.Items[1].Attributes.Add("Style", "cursor:not-allowed");
It doesn't allow cursor, but still user can select this item, is there any other way to make this particular item unselectable??

Even though you said that disabled attribute hides the element, you are wrong.
Disabled attribute is exactly what you should use for this:
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Value="1">First</asp:ListItem>
<asp:ListItem Value="2" disabled="disabled">Second</asp:ListItem>
<asp:ListItem Value="3">First</asp:ListItem>
</asp:DropDownList>
Result:

I think you're looking for something like this. Please see if it can help you.
[Update]
just looked back to your post and understand you cannot use OPTGROUP. May i know the reason? as your requirement needs this option.

Related

How to Avoid default Selected from dropdown list

In my current code we have a drop down lists similar like below which is a mandatory field earlier so we need to select the value before submitting the form but now there is no need mandatory so removed the Mandatory check condition and current code as below.
<asp:dropdownlist id="ddlTest" Runat="server" TabIndex="01">
<asp:ListItem Selected="True" ">Select</asp:ListItem>
<asp:ListItem Value="N" >No</asp:ListItem>
<asp:ListItem Value="Y">Yes</asp:ListItem>
</asp:dropdownlist>
prmddlTest.Value = Format_SQL(ddlTest.SelectedItem.Value)
cmdSaveData.Parameters.Add(prmddlTest)
Function Format_SQL(ByVal sInput) As Object
If sInput = "" Or sInput = "-1" Then
Return DBNull.Value
Else
Return sInput
End If
End Function
I am getting error during submit the form because of default "Select" If I select No or Yes value going fine. How to avoid "Select" in code during submission.
Thanks in Advance
Try this one:
<asp:dropdownlist id="ddlTest" Runat="server" TabIndex="01">
<asp:ListItem Text="" Selected="True"></asp:ListItem>
<asp:ListItem Value="N" >No</asp:ListItem>
<asp:ListItem Value="Y">Yes</asp:ListItem>
</asp:dropdownlist>

Can't two ListItem objects have same value property?

My Code looks like
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="100">A</asp:ListItem>
<asp:ListItem Value="100">B</asp:ListItem>
</asp:ListBox>
Now when I try to select B, It by default selects A always. B is never selected. What is the reason for this behavior ?
The <asp:ListBox> renders as a <select> with <option>s in HTML. The value is persisted across postbacks, not the text. So it can't tell which value you wanted when there are multiple options with the same value, so it assumed the first. It's best practice to use unique option values.
It's recommended to use different values for 2 or more items. No two items can have the same values.
Set AutoPostBack="True" and you'll find that no matter how many items you add in the ListBox, it will always select the 0th index of that particular value.
<asp:ListBox ID="ListBox1" runat="server" AutoPostBack="True">
<asp:ListItem Value="101">D</asp:ListItem>
<asp:ListItem Value="100">A</asp:ListItem>
<asp:ListItem Value="100">B</asp:ListItem>
<asp:ListItem Value="102">E</asp:ListItem>
<asp:ListItem Value="100">C</asp:ListItem>
</asp:ListBox>
Like, selecting B or C will always select A; where A being the 0th index for that particular value 100.

Styling Asp:Dropdownlist and Asp:ListItem using Bootstrap?

just a quick question, How do you style an Asp:Dropdownlist and its ListItem using bootstrap?
Does it have to do with CssClass?
From what the system says CssClass is not a valid attribute in the element ListItem.
Can you add Span Class inside the text?
Here's a sample code:
<asp:DropDownList runat="server" id="dropdownrole" data-style="btn-primary" CssClass="dropdown-menu">
<asp:ListItem CssClass="btn-primary" Text="-- Select a user to proceed --"></asp:ListItem>
<asp:ListItem Text="Admin"></asp:ListItem>
<asp:ListItem Text="Student" ></asp:ListItem>
<asp:ListItem Text="Teacher"></asp:ListItem>
</asp:DropDownList>
Did you try this?
For me it was enough to add CssClass="form-control"
<asp:DropDownList CssClass="dropdown-menu form-control" runat="server" id="dropdownrole" data-style="btn-primary">
I think you are looking for that:
<asp:DropDownList runat="server" CssClass="form-control" id="dropdownrole" >
<asp:ListItem Text="-- Select a user to proceed --"></asp:ListItem>
<asp:ListItem Text="Admin"></asp:ListItem>
<asp:ListItem Text="Student" ></asp:ListItem>
<asp:ListItem Text="Teacher"></asp:ListItem>
</asp:DropDownList>
I think you can't change the selects style with bootstrap, please read the selects section here. (scroll down)
Regards.
<asp:ListItem>Allahabad</asp:ListItem>
<asp:ListItem>Kanpur</asp:ListItem>
<asp:ListItem>Rewa</asp:ListItem>
<asp:ListItem>Bhopal</asp:ListItem>
<asp:ListItem>Indore</asp:ListItem>
<asp:ListItem>Jabalpur</asp:ListItem>
Refer this:
http://www.mindstick.com/Blog/639/Dropdownlist%20using%20BootStrap%20in%20ASP%20Net#.VgUYlVanJ7Y

Adding image to a radio button list in ASP.Net

I am trying to add an image to a radio button list control but its not working..
I tried this..
RadioButtonList2.Items.Add(new ListItem(String.Format("src='../Colors/Dallas_#625527_1.1.png'>")));
but the whole image tag appears as text
I tried I design time as well
<asp:RadioButtonList ID="rbListImages" runat="server">
<asp:ListItem Text="One" Value="1"><img src="../Colors/Dallas_#625527_1.1.png" alt="" /></asp:ListItem>
</asp:RadioButtonList>
but it says img tag cant be nested with the listitem tag.. Please help me out..
You need to specify the control control, you are trying to set the src tag but there is no image control. Try this:-
RadioButtonList2.Items.Add(new ListItem("<img src='"+"../Colors/Dallas_#625527_1.1.png"+"'/>"));
You can also add this at design time, like this:-
<asp:RadioButtonList ID="imagetest" runat="server">
<asp:ListItem Text='<img src="Image1.jpg" alt="img1" />' Value="1" Selected="True" />
<asp:ListItem Text='<img src="Image2.jpg" alt="img2" />' Value="2"></asp:ListItem>
</asp:RadioButtonList>
<asp:RadioButtonList ID="RadioButtonList1" runat="server" >
<asp:ListItem Text="<img src="Images/AddIcon.gif"/> Option1" Value="1"> </asp:ListItem>
<asp:ListItem Text="<img src="Images/1.gif"/> Option2" Value="2"></asp:ListItem>
<asp:ListItem Text="<img src="Images/2.gif"/> Option3" Value="3"></asp:ListItem>
<asp:ListItem Text="<img src="Images/3.gif"/> Option4" Value="4"></asp:ListItem>
(OR)
ANother way :
From Code Behind : Display Images in RadioButtonList Control

DropDown not showing selected value when loaded

I have dropdown as below:
<asp:DropDownList ID="ddlPriority" runat="server">
<asp:ListItem Text="Yes" Value="True" Selected="True"></asp:ListItem>
<asp:ListItem Text="No" Value="False"></asp:ListItem>
</asp:DropDownList>
From above code it should show default text as "Yes" when loaded.
I havent wrote any code behind for binding this drop down. Just wanted to have hard-Code Yes No values in it.
But its not showing me anything selected when i load the page.
Its as below when i load page:
When i done inspect element for this dropdown i got:
<select name="ctl00$MainContent$ddlPriority" id="MainContent_ddlPriority">
<option selected="selected" value="True">Yes</option>
<option value="False">No</option>
</select>
I am wondering why default selected Yes is not comming in drop down...
Note: Yes - No values are comming when i scroll the drop down, but default selected value is not comming when i load the page.
Please help me.
Try to add this property to your DropDownlist: AppendDataBoundItems="true"
try add these properties to your DropDownlist ddlPriority
EnableViewState = "true"
AppendDataBoundItems="true"
Have you try this :
<asp:DropDownList id="ddlPriority" runat="server" >
<asp:ListItem value="0">Select</asp:ListItem>
<asp:ListItem value="1" Selected="True">Yes</asp:ListItem>
<asp:ListItem value="2">No</asp:ListItem>

Categories