简体   繁体   English

未触发DropDownList OnSelectedIndexChanged

[英]DropDownList OnSelectedIndexChanged not triggered

Here is my problem, I've a dropdownlist in my asp.net and a gridview . 这是我的问题,我的asp.net中有一个dropdownlist ,还有一个gridview Based on DropDownList's selected value, I would like to change the content/bind data to GridView. 基于DropDownList的选定值,我想将内容/绑定数据更改为GridView。 But it's not happening, OnSelectedIndexChanged event not firing on change. 但这并没有发生, OnSelectedIndexChanged事件不会在更改时触发。

Aspx code: Aspx代码:

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

Aspx.cs code: Aspx.cs代码:

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. 我已经根据建议设置了Page的EnableViewState="false"

Try moving the BindDropDown() method to earlier in the Page lifecycle and doing it unconditionally. 尝试将BindDropDown()方法移到Page生命周期的早期,然后无条件地进行操作。

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. 澄清一下-是的,这是因为您将Page的EnableViewState设置为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. 实际上,不仅是选定的值-下拉列表项的整个列表(保存在ViewState中)应该对您来说已经消失了。

The SelectedValue property is set once we have the list of items, it depends on it. 一旦有了项目列表,就会设置SelectedValue属性,这取决于它。

With the ViewState disabled, you do not have that list restored automatically on every postback from it. 在禁用ViewState的情况下,您不会在每次回发时自动恢复该列表。

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, 由于尝试时未发现任何代码更改,因此Dropdownchange事件正在触发,请保留断点并进行检查,

ASPX Code: ASPX代码:

<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 Aspx.cs代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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