【问题标题】:DropDownList selectedindexchanged not firing on default selected item during dropdown bind within Gridview在 Gridview 中的下拉绑定期间,DropDownList selectedindexchanged 未在默认选定项目上触发
【发布时间】:2014-09-20 04:52:15
【问题描述】:

我有一个带有下拉列表的 Gridview,它是在 gridview 的 OnRowDataBound 事件中动态创建的,最初我正在设置一个选定的值。

问题是当我切换到下拉列表的不同索引时,它工作正常,但是当我更改为默认选定索引时,SelectedIndexChanged 不会被触发。

请帮帮我..

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
 DropDownList DropDownList1 = new DropDownList();
                    DropDownList1.ID = "DropDownList1";
                    DropDownList1.SelectedIndexChanged += new EventHandler(DropDownList1_SelectedIndexChanged);
                    DropDownList1.EnableViewState = true;
                    DropDownList1.AutoPostBack = true;
                    DropDownList1.EnableViewState = true;
                    string sql1 = ".....";
                    DataTable dtDDL = new DataTable();
                    dtDDL = SQL.ReturnDataTable(sql1);
                    if (dtDDL.Rows.Count > 0)
                    {
                        DropDownList1.DataSource = dtDDL;
                        DropDownList1.DataTextField = "CODE";
                        DropDownList1.DataValueField = "CODE";
                        DropDownList1.DataBind();
                        DropDownList1.Font.Size = 8;
                        //DropDownList1.Items.Insert(0, new ListItem("0", "0"));
                    }

                        DropDownList1.SelectedValue = dtShift.Rows[0]["SHIFT_CODE"].ToString();
                        DropDownList1.ToolTip = dtShift.Rows[0]["ShiftTime"].ToString();
                  }






  protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
       //not coming here for default index changed
    }

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    AutoPostBack="true" 试试看

    <asp:DropDownList ID="Drplist1" AutoPostBack="true" runat="server" OnSelectedIndexChanged="Drplist1_SelectedIndexChanged"></asp:DropDownList>
    

    【讨论】:

      【解决方案2】:

      当列表中有多个具有相同值的值时,我也注意到了这种行为。 因此,您可以使用永远不会相同的序列号,而不是 Code for Value。

      所以例如

      如果选择的索引值是“X”并且下一个选择的值也是“X”那么它可能不会调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-12-18
        • 1970-01-01
        • 2011-01-16
        • 1970-01-01
        • 2011-06-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多