Dropdownlist SelectIndexChanged不触发C#

时间:2018-07-24 14:45:00

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

我对回发/ 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>

2 个答案:

答案 0 :(得分:0)

将自动回发代码放在!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);
    }

答案 1 :(得分:0)

Dynamic controls should be added on Init. After OnLoad finishes executing ASP.NET starts processing control's events and values. You can read/write their properties on or after Load.

Suggest you to check the place where you have created DropDownList.

Answer: because you have created DropDownList after the events have been processed.

Have a look here: http://www.4guysfromrolla.com/articles/092904-1.aspx

相关问题