Why dropdownlist is getting assigned with wrong value? - c#

I am assingning a value returned from DataTable to a dropdownlist but it picks the very first value only -1. Why?
ddlBPS.SelectedValue = r["ServiceInfoInitialBPS"].ToString();
The value of ServiceInfoInitialBPS inside the datatable and db is 6.
<asp:DropDownList ID="ddlBPS" runat="server" CssClass="form-control white">
<asp:ListItem Value="-1" Text="-Select-"></asp:ListItem>
<asp:ListItem Value="01" Text="01"></asp:ListItem>
<asp:ListItem Value="02" Text="02"></asp:ListItem>
<asp:ListItem Value="03" Text="03"></asp:ListItem>
<asp:ListItem Value="04" Text="04"></asp:ListItem>
<asp:ListItem Value="05" Text="05"></asp:ListItem>
<asp:ListItem Value="06" Text="06"></asp:ListItem>
<asp:ListItem Value="07" Text="07"></asp:ListItem>
<asp:ListItem Value="08" Text="08"></asp:ListItem>
<asp:ListItem Value="09" Text="09"></asp:ListItem>
<asp:ListItem Value="10" Text="10"></asp:ListItem>
<asp:ListItem Value="11" Text="11"></asp:ListItem>
<asp:ListItem Value="12" Text="12"></asp:ListItem>
<asp:ListItem Value="13" Text="13"></asp:ListItem>
<asp:ListItem Value="14" Text="14"></asp:ListItem>
<asp:ListItem Value="15" Text="15"></asp:ListItem>
<asp:ListItem Value="16" Text="16"></asp:ListItem>
<asp:ListItem Value="17" Text="17"></asp:ListItem>
<asp:ListItem Value="18" Text="18"></asp:ListItem>
<asp:ListItem Value="19" Text="19"></asp:ListItem>
<asp:ListItem Value="20" Text="20"></asp:ListItem>
<asp:ListItem Value="21" Text="21"></asp:ListItem>
<asp:ListItem Value="22" Text="22"></asp:ListItem>
</asp:DropDownList>
Why is that? I have debugged but the value assigned to it is always -1.

I assume that the value 6 is not found because you use leading zeros as in 06. If it's a postback you would get an exception, but since it's the initial load nothing(so the first) item gets "selected".
MSDN:
ArgumentOutOfRangeException: The selected value is not in the list of available values and view
state or other state has been loaded (a postback has been performed).
For more information, see the Remarks section.
One way, fill it with a leading zero:
ddlBPS.SelectedValue = r["ServiceInfoInitialBPS"].ToString().PadLeft(2, '0');
or (presuming it's an int):
ddlBPS.SelectedValue = r.Field<int>("ServiceInfoInitialBPS").ToString("D2");

Append "0" before the value coming from database. It is a string value not an int so it should completely with value of option.
ddlBPS.SelectedValue = "0" + r["ServiceInfoInitialBPS"].ToString();

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>

ASP DropdownList selected index changing after postback

I have this problem concerning asp DropDownList. My DropDownList contains many ListItem with different texts but some may have the same value.
Now whenever I select an item in the DropDownList and click a button to save, the selected Index changes to a different index. I realize that the new selected Index changed to usually have the same value as the index I selected in the first place.
<asp:DropDownList ID="ddlNature_of_Business" runat="server" >
<asp:ListItem Value="" Text="SELECT"></asp:ListItem>
<asp:ListItem Value="3" Text="BDC / MFB"></asp:ListItem>
<asp:ListItem Value="3" Text="Money Transmitter"></asp:ListItem>
<asp:ListItem Value="3" Text="Money Transmitter"></asp:ListItem>
<asp:ListItem Value="3" Text="Leather Goods Store"></asp:ListItem>
<asp:ListItem Value="3" Text="Car Dealer"></asp:ListItem>
<asp:ListItem Value="3" Text="Travel Agency"></asp:ListItem>
<asp:ListItem Value="3" Text="Jewel & Gem Dealer"></asp:ListItem>
<asp:ListItem Value="3" Text="Imports/Exports"></asp:ListItem>
<asp:ListItem Value="3" Text="Cash Intensive Business-Restaurants"></asp:ListItem>
<asp:ListItem Value="3" Text="Cash Intensive Business-Retail Stores"></asp:ListItem>
<asp:ListItem Value="3" Text="Cash Intensive Business-Furnished apartments"></asp:ListItem>
<asp:ListItem Value="3" Text="Cash Intensive Business-Hotels"></asp:ListItem>
<asp:ListItem Value="3" Text="Cash Intensive Business-etc"></asp:ListItem>
<asp:ListItem Value="3" Text="Govt Contractors"></asp:ListItem>
<asp:ListItem Value="3" Text="Weapons & Arms Dealers"></asp:ListItem>
<asp:ListItem Value="2" Text="Charity Organizations"></asp:ListItem>
<asp:ListItem Value="2" Text="NGOs"></asp:ListItem>
<asp:ListItem Value="2" Text="Professional Service Provider - Lawyer"></asp:ListItem>
<asp:ListItem Value="2" Text="Professional Service Provider - Accountant"></asp:ListItem>
<asp:ListItem Value="2" Text="Professional Service Provider - etc"></asp:ListItem>
<asp:ListItem Value="1" Text="Manufacturing"></asp:ListItem>
<asp:ListItem Value="1" Text="Construction"></asp:ListItem>
<asp:ListItem Value="1" Text="Others"></asp:ListItem>
</asp:DropDownList>
Whenever I select Car Dealer, it changes to BDC / MFB, NGOs changes to Charity Organizations while Construction changes to Manufacturing
From my observation, the selected index changes to to the first ListItem with the same value as the selected ListItem
I've tried using HTML <select></select> instead but it still behaves the same even on different browsers.
The only solution I could think of now is to remove the Value attribute from the ListItem and use only the Text attribute. Then I'll save all the DropDownList Items in a table and write a C# method that gets their values using the SelectedItem's Text. But then I have over 30 more of DropDownList like this one but with different group of ListItem.
Does anybody knows a better workaround?

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

Get the text from a radiobuttonlist instead of the value

I've searched all over the internet and i can't seem to find a solution to my little problem.
i got this radiobuttonlist where i get the selected value by saying:
RadioButtonList1.SelectedValue
This works great when trying to get the value
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" RepeatLayout="Table" CellSpacing="50">
<asp:ListItem Value="1" Text="Helt enig"></asp:ListItem>
<asp:ListItem Value="2" Text="Delvist enig"></asp:ListItem>
<asp:ListItem Value="3" Text="Hverken eller"></asp:ListItem>
<asp:ListItem Value="4" Text="Delvist uenig"></asp:ListItem>
<asp:ListItem Value="5" Text="Helt uenig"></asp:ListItem>
</asp:RadioButtonList>
But now i've come to the point where i would like to get the text also.
Usually i would just get it by saying:
RadioButtonList1.Text;
But for some reason i just get the "Value" again.
Does anyone know if there's a way to get the "Text" also?
RadioButtonList1.SelectedItem.Text
This should do it.

Categories