【问题标题】:Dropdownlist SelectIndexChanged Not firing C#下拉列表 SelectIndexChanged 不触发 C#
【发布时间】:2019-01-01 05:56:46
【问题描述】:

我对回发/DDL 的处理不太好。是的,我使用过 autopostback = true!

在下面,我正在尝试更改所选索引...以在 budgetDDL1 上触发,但是无论我尝试什么都不会!

我正在将数据从数据库绑定到 ddl...

我已经尝试将 ddl 绑定/添加到回帖内部/外部的表格,并启用/禁用视图状态等。这些都不起作用.. 必须有一个简单的答案?!

我需要以什么顺序创建/绑定索引更改方法的下拉菜单以触发解释也很有用!

   DropDownList budgetDDL1 = new DropDownList();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string QueryString = "SELECT [BudgetCode], [Department], CONCAT([BudgetCode],' - ', [Department]) AS 'textvalue' FROM [tblBudget]";
                using (SqlConnection myConnection = new SqlConnection(ConnectionString))
                {
                    using (SqlCommand cmd = new SqlCommand(QueryString, myConnection))
                    {
                        myConnection.Open();
                        SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                        DataTable dt = new DataTable();
                        dt.Load(dr);
                        budgetDDL1.SelectedIndex = 0;
                        budgetDDL1.DataSource = dt;
                        budgetDDL1.DataTextField = "textvalue";
                        budgetDDL1.DataValueField = "BudgetCode";
 budgetDDL1.AutoPostBack = true;
            budgetDDL1.SelectedIndexChanged += budgetDDL1_SelectedIndexChanged;
            budgetDDL1.DataBind();
                    }
                }
            }
       
            table1.Controls.Add(budgetDDL1);
        }
 protected void budgetDDL1_SelectedIndexChanged(object sender, EventArgs e)
    { *I have a breakpoint here which doesn't fire*
        string msg = budgetDDL1.SelectedItem.Text;
        ScriptManager.RegisterClientScriptBlock(sender as System.Web.UI.Control, this.GetType(), "alert", "alert('" + msg + "')", true);
    }

view:
    <body>
    <form  runat="server">
    <table>
      <tr>    
       <td id="table1" runat="server">
       </td>
    </tr>
    </table>
    </form>
    </body>

【问题讨论】:

  • 可能您的控件没有在页面回发中添加 ro 控件层次结构。请分享您的查看代码。
  • 我的视图代码和 aspx 一样吗?它只是表单标签中的一个长表。我将 DDL 添加到表单元格(td)中,id 为 table1
  • 添加了视图..它有更多的行等,但只是显示了基本结构

标签: c# asp.net drop-down-menu postback


【解决方案1】:

将自动回发代码放在 !IsPostback 之外

    DropDownList budgetDDL1 = new DropDownList();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string QueryString = "SELECT [BudgetCode], [Department], CONCAT([BudgetCode],' - ', [Department]) AS 'textvalue' FROM [tblBudget]";
            using (SqlConnection myConnection = new SqlConnection(ConnectionString))
            {
                using (SqlCommand cmd = new SqlCommand(QueryString, myConnection))
                {
                    myConnection.Open();
                    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    DataTable dt = new DataTable();
                    dt.Load(dr);
                    budgetDDL1.SelectedIndex = 0;
                    budgetDDL1.DataSource = dt;
                    budgetDDL1.DataTextField = "textvalue";
                    budgetDDL1.DataValueField = "BudgetCode";

                }
            }
        }
        budgetDDL1.AutoPostBack = true;
        budgetDDL1.SelectedIndexChanged += budgetDDL1_SelectedIndexChanged;
        budgetDDL1.DataBind();

        table1.Controls.Add(budgetDDL1);
    }

【讨论】:

    【解决方案2】:

    应在初始化上添加动态控件。 OnLoad 完成执行后,ASP.NET 开始处理控件的事件和值。您可以在 Load 上或之后读/写它们的属性。

    建议你查看你创建DropDownList的地方。

    答案:因为您在处理完事件后创建了 DropDownList。

    看看这里:http://www.4guysfromrolla.com/articles/092904-1.aspx

    【讨论】:

    • 谢谢你,我会读一读!出于某种原因,当我单击按钮(不同的回发)时,我可以看到我的断点 DDL indexchange 触发,因此它被搁置在某个地方。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-12
    相关资源
    最近更新 更多