简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM