RaJesh RiJo
RaJesh RiJo

Reputation: 4400

DropDownList OnSelectedIndexChanged not triggered

Here is my problem, I've a dropdownlist in my asp.net and a gridview. Based on DropDownList's selected value, I would like to change the content/bind data to GridView. But it's not happening, OnSelectedIndexChanged event not firing on change.

Aspx code:

<asp:DropDownList ID="drpRegion" runat="server" CssClass="ddlfield" AutoPostBack="true"
                OnSelectedIndexChanged="drpRegion_SelectedIndexChanged" />

Aspx.cs code:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindDropDown();    //data binding for dropdownlist
        BindRegionWiseTally();    //data binding for gridview
    }
}

protected void drpRegion_SelectedIndexChanged(object sender, EventArgs e)
{
     BindRegionWiseTally();   //data binding for gridview
}

I have set Page's EnableViewState="false" based on suggestions.

Upvotes: 0

Views: 320

Answers (2)

Catty
Catty

Reputation: 51

Try moving the BindDropDown() method to earlier in the Page lifecycle and doing it unconditionally.

protected override void OnInit(EventArgs e)
{
    BindDropDown();//data binding for dropdownlist
}

To clarify - yes, it is because you set the Page's EnableViewState to false.

In fact, it is not only the selected value - the whole list of dropdown items (which is persisted in the ViewState) should have probably disappeared for you.

The SelectedValue property is set once we have the list of items, it depends on it.

With the ViewState disabled, you do not have that list restored automatically on every postback from it.

Upvotes: 3

Ravi Kanth
Ravi Kanth

Reputation: 1210

As i don't find any change in code when i tried it the Dropdownchange event is firing,please keep the break point and check it,

ASPX Code:

<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
     <asp:ListItem Value="1">Tracking Reader</asp:ListItem>
     <asp:ListItem Value="2" Selected ="True" >Dropbox Reader</asp:ListItem>
</asp:DropDownList>

Aspx.cs Code

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDropDown();//data binding for dropdownlist
            //BindRegionWiseTally();//data binding for gridview
        }
    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        Response.Write("Invoked sucessfully.");
    }
    protected void BindDropDown()
    {
        DropDownList1.Items.Add("All");
        DropDownList1.Items.Add("New");
        DropDownList1.Items.Add("Update");
        DropDownList1.Items.Add("Delete");
    }

Upvotes: 1

Related Questions