I'm looking for the best way to handle a change of index being selected on a ASP.net RadioButtonList (C# code behind). I have 3 list items. For the first one, I want it to show a hidden asp:textbox on the page, whereas the other 2 will hide the textbox.
//asp.net side
<asp:RadioButtonList ID="_indicatorAckType" runat="server" RepeatDirection="Horizontal"
enabled="true" OnSelectedIndexChanged="onAckTypeChanged">
<asp:ListItem Text="None" />
<asp:ListItem Text="SHOW" />
<asp:ListItem Text="HIDE" />
</asp:RadioButtonList>
//code behind
protected void onAckTypeChanged(object sender, EventArgs e)
{
if (_indicatorAckType.SelectedItem.Text == "SHOW")
_myTextboxID.Visible = true;
else
_myTextboxID.Visible = false;
}
I initially tried using onclick event handlers, but I've been told that ListItem's cannot use onclick events with radio button items. What am I doing wrong here? This does not throw any errors or have any visibly obvious problems. I have tried making onSelectedIndexChanged do nothing except show the textbox and that doesn't work either.
Any help is appreciated! Thanks everyone.
On the RadioButtonList, set the AutoPostBack attribute to true.
Have a look at this it might help. And I suggest to turn off autopostback if enabled on radio button do it all on client side using jquery.
example:
Using jQuery, hide a textbox if a radio button is selected
Related
I have a radiobuttonlist, a label, and a dropdown as follows:
<asp:RadioButtonList id="rbList" runat="server" AutoPostBack="true" EnableViewState="false"
OnSelectedIndexChanged="rbList_SelectedIndexChanged"
RepeatLayout="Table" RepeatDirection="Horizontal" RepeatColumns="3">
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
<asp:ListItem> Radio 2 </asp:ListItem>
<asp:ListItem> Radio 3 </asp:ListItem>
</asp:RadioButtonList>
<asp:Label runat="server" ID="lbl" text="1,2" EnableViewState="false"></asp:Label>
<asp:DropDownList runat="server" ID="ddl" Visible="false">
</asp:DropDownList>
My rbList_SelectedIndexChanged is as follows:
protected void rbList_SelectedIndexChanged(object sender, EventArgs e)
{
if (rbList.SelectedIndex == 0 | rbList.SelectedIndex==1)
{
lbl.Text = "1,2";
ddl.Visible = false;
//ddl.Attributes.Add("style", "display:none");
}
else if (rbList.SelectedIndex == 2)
{
lbl.Text = "3";
ddl.Visible = true;
//ddl.Attributes.Add("style", "");
}
}
Now when I change from radio3 to radio2, the event is getting fired as usual and everything looks good. But when I change from radio3 to radio1, I don't see the event getting fired (I inserted a breakpoint) the ddl stays visible but the value of lbl changes to 1,2.
My 2 questions are as follows:
1) Why is the event not getting fired on changing from radio3 to radio1?
2) How is the label value getting changed when the event is not firing?
Any help or comments are much appreciated..Thanks in advance!
I am not sure this is a bug or not, but...
When EnableViewState="false" with DDL or RBL and user trying to pick the first list item (index 0), the SelectedIndexChanged will NOT get fired.
If you set EnableViewState="true", then the DDL or RBL should work properly when user pick the first list item while non-first item was selected...
Preselecting a radio button in your markup is causing your problems. going from any other option back to option 1 will not trigger the changed event.
this line is your culprit.
<asp:ListItem Selected="True"> Radio 1 </asp:ListItem>
if you remove the Selected attribute the event should register properly
<asp:ListItem> Radio 1 </asp:ListItem>
you could handle the preselection in your code behind.
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
rbList.SelectedIndex = 0;
}
}
As far as I can tell, programmatically setting SelectedIndex (even on first post) results in the same behaviour as setting Selected="True" on the markup.
The only sure-fire way seems to be using an UpdatePanel with the RadioButtonList as an async trigger, making sure the markup changes with every change.
That is, unless you want to go the jQuery route..
I had a similar problem, but it had to do with the "ChildrenAsTriggers" property of the update panel being set to false. All other radio button indexes worked fine this way, except index 0.
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
I have one dropdownlist in my form in .aspx File.
Once I select "1", then next time if I redirect to same page then "1" should be
unselectable or Hidden for making sense that I have selected "1" previously because I have large items in my example.
Disabling any particular item in a dropdownlist is not possible.
Alternate:
You can use a BulletedList Server Control and use its Enable = False property to disable any particular item and all user can see that item as disabled.. here is a design time example..
<asp:BulletedList ID="BulletedList1" runat="server">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem Enabled="False">3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:BulletedList>
Took from here
You can use a hidden field to store which item was selected in code behind and disable that item using the following code.
//Code Starts
$(document).ready(function() {
$('#ddlList option:contains("HTML")').attr("disabled","disabled");
});
//Code Ends
You can track your selection using Cookies,
on the "onChange" event of your dropdown list, create a cookie with the selected value,
then on body "onLoad" u can check and disable the items that are there in Cookie.
Or you can hold your selected values in a hidden field
To remove the last selected item in your dropdownlist (DDL), considering the following code for your DDL form, you can use the onselectedindexchanged event of your DDL (that is raised when you click on one of your DDL items):
<asp:DropDownList ID="_DDL" runat="server"
onselectedindexchanged="_DDL_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
</asp:DropDownList>
And put on the code behind the RemoveAt() method to remove the selected Item from your DDL:
protected void _DDL_SelectedIndexChanged(object sender, EventArgs e)
{
int ItemToRemove = _DDL.SelectedIndex;
_DDL.Items.RemoveAt(ItemToRemove);
}
Hope this will be helpful :)
Sorry for my ignorance, but I've always believed that setting the SelectedIndex property of a DropDownList the SelectedIndexChanged event is fired... Am I wrong?
Searching in the documentation I haven't found anything clear...
Thanks
No it cannot fire the selectionchange event.
if you are settting programatically than it not fire the selectionchange event.
DropDownlist Class Selected Index Changed Event fires when the selection from the list control changes between posts to the server.
Please make sure about following to fire the event.
AutoPostBack="true" How to use DropDownList AutoPostBack feature
onselectedindexchanged="Name of the Handler"
Below is the code.
<asp:DropDownList ID="ddl1" runat="server" AutoPostBack="true"
onselectedindexchanged="ddl1_SelectedIndexChanged">
<asp:ListItem Text ="1" Value="1"></asp:ListItem>
<asp:ListItem Text ="2" Value="2"></asp:ListItem>
</asp:DropDownList>
protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)
{
}
Note - On setting the index at runtime will never fire the event.
Please go through the following links as well.
How to: Respond to Changes in List Web Server Controls
How to use DropDownList AutoPostBack feature
i change dropdown list with button click like this:
ddl.selectedIndex+=1
my ddl has a SelectedIndexChanged event which is not firing if index changed through button. Is it a normal behavior? Should I create separate method and call it right after the ddl.selectedIndex+=1 or there's a better way?
I'm assuming you're using ASP.NET? If so, setting AutoPostBack may help:
<asp:DropDownList ID="ddl" runat="server" AutoPostBack="true">
</asp:DropDownList>
I am working with ASP.NET
I have a DropDownList item and in there i have hyperlinks as values. What event must i use in my code behind to redirect the user to that URL when he selects the "eRate" item?
My code
<asp:DropDownList ID="dropSelect" runat="server" Width="126px">
<asp:ListItem>Please select</asp:ListItem>
<asp:ListItem Value="http://www.erate.co.za">eRate</asp:ListItem>
</asp:DropDownList>
thanks in advance!!
add a onselectedindexchanged to the dropdown like this
OnSelectedIndexChanged="dropSelect_OnSelectedIndexChanged"
then codebehind you can do like this.
protected void dropSelect_OnSelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect(dropSelect.SelectedValue);
}
you can do some extra null check and all that but this is the basic idea you can use