ASP RadioButtonList .Selected Issues - c#

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

Related

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

Drop Down List Selected Index changed not working in Update panel

I have a drop down list in UpdatePanel_2, it gets populated when Button_1 is clicked in UpdatePanel_1.
My ddlist markup is,
<asp:DropDownList id="drop1" runat="server" EnableViewState="true" AutoPostBack="true" OnSelectedIndexChanged="Drop1_SelectedIndexChanged" />
then code behind is,
protected void Drop1_SelectedIndexChanged(object sender, EventArgs e)
{ }
I also tried putting AutoPostback=true to my DropDownList, still no success.
I also added triggre to update panel 2 but no gain,
<Triggers>
<asp:AsyncPostbackTrigger ControlID="drop1" EventName="SelectedIndexChanged" />
</Triggers>
I am populating DropDownList using a button not PAGE LOAD METHOD PLEASE READ before answering.
Thanks
Check the data to populate the DropDownList in the Page_Load event and always check IspostBack:
if(!IsPostBack)
{
//DropDownList configuration
}
Use EnableViewState:
<asp:DropDownList ID="ddlAddDepPlans" runat="server" AutoPostBack="true" EnableViewState="true" />
Hope it helps you.
I had the same issue. My problem was that the values of my ListItems were all the same :D
<asp:DropDownList ID="ddlFilterLogins" runat="server" Visible="true" AutoPostBack="true">
<asp:ListItem Value="0" Text="All"></asp:ListItem>
<asp:ListItem Value="0" Text="Some"></asp:ListItem>
<asp:ListItem Value="0" Text="Some more"></asp:ListItem>
</asp:DropDownList>
It should be like this:
<asp:DropDownList ID="ddlFilterLogins" runat="server" Visible="true" AutoPostBack="true">
<asp:ListItem Value="0" Text="All"></asp:ListItem>
<asp:ListItem Value="1" Text="Some"></asp:ListItem>
<asp:ListItem Value="2" Text="Some more"></asp:ListItem>
</asp:DropDownList>
Hope this helps. This might be hard to find sometimes :)
Please, when you initialize it in Page_Load() check if not is postback. If you don't do it, you will always set the default value, and this replaces the value setted in the event.
if(!IsPostBack)
{
//DropDownList configuration
}
You can use Init event instead of SelectIndexChanged.
It worked fine for me.
Hope you got my point.
It was also a wired problem for me. finally It was because of identical listitems in the dropdown as shown below. during development you may use same items just for testing. change them.
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>
<asp:ListItem>Business</asp:ListItem>

Bug error: trying to set asp.net radiobuttonlist selected item from codebehind return an error

When using the following code, this error is returned:
'rblPermisSejourA' has a SelectedValue which is invalid because it does not exist in the list of items.
<asp:RadioButtonList ID="rblPermisSejour" runat="server"
DataSourceID="EntityDataSourcePermisSejour" DataTextField="Libelle"
DataValueField="Id" AppendDataBoundItems="True" RepeatDirection="Horizontal">
<asp:ListItem Selected="True" Text="" Value="-1">Aucun</asp:ListItem>
</asp:RadioButtonList>
<asp:RadioButtonList ID="rblPermisSejourA" runat="server"
DataSourceID="EntityDataSourcePermisSejour" DataTextField="Libelle"
DataValueField="Id" AppendDataBoundItems="True" RepeatDirection="Horizontal">
<asp:ListItem Selected="True" Text="" Value="-1">Aucun</asp:ListItem>
</asp:RadioButtonList>
protected void ws2_OnDeactivate(object sender, EventArgs e)
{
rblPermisSejourA.SelectedValue = rblPermisSejour.SelectedValue;
}
Note that "rblPermisSejour" is in a wizard step and "rblPermisSejourA" in another wizard step that is not yet activated (no id and no title for the step in witch there is the "rblPermisSejourA"). When this step is activated, all is working well.
But with the same code and same operation with another RadioButtonList it's working very well and this within the same context (wizard step not activated).
From your comment
<asp:ListItem Selected="True" Value="1">A</asp:ListItem>
<asp:ListItem Selected="True" Value="2">C</asp:ListItem>
you have BOTH items selected="true" so calling .SelectedValue may be a problem as it won't know which value you mean. Are these the list items form rblPermisSejourA or rblPermisSejour? If you edit your question to add both lists we can see what this line
rblPermisSejourA.SelectedValue = rblPermisSejour.SelectedValue;
is trying to do and figure out your error.
EDIT
Have you tried something like
rblPermisSejourA.Items.FindByValue(rblPermisSejour.SelectedValue).Selected = true;
Have you tried
rblPermisSejourA.SelectedValue = rblPermisSejour.SelectedItem.Value;
This is THE solution:
if (rblPermisSejourA.Items.Count <= 1)
rblPermisSejourA.DataBind(); //added to correct the bug
rblPermisSejourA.SelectedValue = rblPermisSejour.SelectedValue;

Getting a dropdownlist to check a checkbox in asp.net/C#

<input runat ="server" type ="checkbox" id="helprequest" />
<label for="helprequest">Help request</label>
<asp:DropDownList ID="options" runat="server" OnSelectedIndexChanged="checkHelpRequest">
<asp:ListItem Text="Windows"></asp:ListItem>
<asp:ListItem Text="Macintosh"></asp:ListItem>
<asp:ListItem Text="Linux"></asp:ListItem>
<asp:ListItem Text="Other"></asp:ListItem>
</asp:DropDownList>
In my codebehind, I have
protected void checkHelpRequest(object sender, EventArgs e)
{
helprequest.Checked = true;
}
But when I select something on the dropdownlist, the checkbox, does not get marked as checked, how do I get the checkbox to appear as checked when I change the index on a dropdownlist?
Your DropDownList does not have AutoPostBack='true' set. Without setting this, your dropdown will not post back when you change the selected index.
Just change it to:
<asp:DropDownList AutoPostBack="true" ID="options"
runat="server" OnSelectedIndexChanged="checkHelpRequest">
Without setting this, your checkHelpRequest method will still be called when your drop down changes index, but only after a postback is caused by some other control, like a button, or another DropDownList that does have AutoPostBack set.

How to get the selected value from RadioButtonList?

I have a RadioButtonList on my page that is populated via Data Binding
<asp:RadioButtonList ID="rb" runat="server">
</asp:RadioButtonList>
<asp:Button Text="Submit" OnClick="submit" runat="server" />
How do I get the value of the radio button that the user selected in my "submit" method?
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);
}
Using your radio button's ID, try rb.SelectedValue.
Technically speaking the answer is correct, but there is a potential problem remaining.
string test = rb.SelectedValue is an object and while this implicit cast works. It may not work correction if you were sending it to another method (and granted this may depend on the version of framework, I am unsure) it may not recognize the value.
string test = rb.SelectedValue; //May work fine
SomeMethod(rb.SelectedValue);
where SomeMethod is expecting a string may not.
Sadly the rb.SelectedValue.ToString(); can save a few unexpected issues.
string radioListValue = RadioButtonList.Text;
radiobuttonlist.selected <value> to process with your code.

Categories