Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net DropDownList OnSelectedIndexChange not firing

Tags:

c#

asp.net

I have the following DropDownList control:

<asp:label runat="server" text="Filter by state: "></asp:label>
<asp:dropdownlist runat="server" id="filterbystate" 
    OnSelectedIndexChanged="FilterByState">
    <asp:ListItem value="all" selected="True">All</asp:ListItem>
    <asp:ListItem value="ca" selected="False">California</asp:ListItem>
    <asp:ListItem value="co" selected="False">Colorado</asp:ListItem>
    <asp:ListItem value="id" selected="False">Idaho</asp:ListItem>
    <asp:ListItem value="ut" selected="False">Utah</asp:ListItem>
</asp:dropdownlist>

Here is the method:

protected void FilterByState(object sender, EventArgs e)
{
    var value = e;
}

The method will not fire for some reason. I select a different value and nothing happens. What I am trying to do is reload the page passing in the state value so I can filter the results by it.

What am I doing wrong?

like image 527
James Wilson Avatar asked Apr 23 '13 15:04

James Wilson


People also ask

What is SelectedIndexChanged in asp net?

When an item is changed in ASP.Net DropDownList, the following OnSelectedIndexChanged event handler is executed. Inside the event handler, the selected Text and Value of the DropDownList Selected Item is fetched and displayed using JavaScript Alert message box.

What is selected index changed in C#?

The example uses the SelectedIndexChanged event to determine when the selected item in the ListBox is changed. The example code then reads the text of the item using the SelectedItem property and calls the FindString method on a different ListBox using the text returned by SelectedItem in the first ListBox.


2 Answers

Set AutoPostBack=True as an attribute of your DDL and it will automatically post back the selected index change event

like image 62
Josh E Avatar answered Oct 16 '22 05:10

Josh E


Add this to dropdown list aspx it will cause a request to be send to the server and your event will be fired.

AutoPostBack="true"
like image 33
DotNetUser Avatar answered Oct 16 '22 05:10

DotNetUser