About SelectedIndex property in a DropDownList - c#

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

Related

SelectedIndexChanged event not firing for 1 list item

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.

Fire SelectedIndexChanged event on button click for dropdown

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>

RadioButtonList OnSelectedIndexChanged

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

Why isn't selectedindexchanged firing?

I have the following DropDownList defined:
<asp:DropDownList ID="ddlStuff" CssClass="myCssClass" OnSelectedIndexChanged="PopulateAnotherDropdown" runat="server"></asp:DropDownList>
However, my PopulateAnotherDropdown method isn't firing. I have a breakpoint set on the method, and it's not being hit.
Here is my method write-up in code-behind:
public void PopulateAnotherDropdown(object sender, EventArgs e)
{
...
}
For what it's worth, the page is rendering as follows:
<select name="ctl00$MainContent$ddlStuff" id="MainContent_ddlStuff" class="myCssClass">
Any ideas?
Because you forget to add: AutoPostBack="true"
To fire the SelectedIndex of the dropdownlist control, it needs to postback to the server. For that to happen you to have to set AutoPostBack="true" in the control property.
Please Set
AutoPostBack="true"
of the dropdown.Because by default it is false except button control

Working with ASP.NET DropDownList item not working - need an event?

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

Categories

Resources