ASP DropdownList selected index changing after postback - c#

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?

Related

How to make an asp dropdown list item unselectable

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.

ASP RadioButtonList .Selected Issues

I have a radio button list inside of an AJAX panel. Here is the radio button list:
<asp:RadioButtonList ID="RadioButtonList" runat="server" TextAlign="Right" AutoPostBack="true">
<asp:ListItem Text="Option3" Value="Option3" Selected="True" />
<asp:ListItem Text="Option1" Value="Option1" />
<asp:ListItem Text="Option2" Value="Option2" />
</asp:RadioButtonList>
I have a function that loads the value based on saved settings. It looks similar to this:
string selectedOption = savedRecord.RadioButtonListValue.ToString();
RadioButtonList.Items.FindByValue(selectedOption).Selected = true;
It only seems to correctly load the value if I haven't changed the selected option.
If I load the page, the load the settings it will correctly set to the saved option.
If I load the page, change the option, then load the settings it will not change the option.
I have tried with AutoPostBack set to true and false and it doesn't seem to change the result. Any ideas?
I have been able to hard code a value and it seems to always load correctly:
//Working
RadioButtonList.Items.FindByValue("Option1").Selected = true;
//Not-working
string selectedOption = savedRecord.RadioButtonListValue.ToString(); //"Option1"
RadioButtonList.Items.FindByValue(selectedOption).Selected = true;
Here are the combinations that I have come up with.
Please try the ASPX code will look something like this:
The ASPX code will look something like this:
<asp:RadioButtonList ID="rblist1" runat="server">
<asp:ListItem Text ="Item1" Value="1" />
<asp:ListItem Text ="Item2" Value="2" />
<asp:ListItem Text ="Item3" Value="3" />
<asp:ListItem Text ="Item4" Value="4" />
</asp:RadioButtonList>
<asp:Button ID="btn1" runat="server" OnClick="Button1_Click" Text="select value" />
And the code behind:
protected void Button1_Click(object sender, EventArgs e)
{
string selectedValue = rblist1.SelectedValue;
Response.Write(selectedValue);
}

Why dropdownlist is getting assigned with wrong value?

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();

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