下拉列表 SelectIndexChanged 不触发 C#

我对回传/DDL 不太熟悉。是的,我使用过 autopostback = true!


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


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


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


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


   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>

繁星淼淼
浏览 337回答 2
2回答

慕无忌1623718

将自动回发代码放在 !IsPostback&nbsp; &nbsp; DropDownList budgetDDL1 = new DropDownList();&nbsp; &nbsp; protected void Page_Load(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (!IsPostBack)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string QueryString = "SELECT [BudgetCode], [Department], CONCAT([BudgetCode],' - ', [Department]) AS 'textvalue' FROM [tblBudget]";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (SqlConnection myConnection = new SqlConnection(ConnectionString))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (SqlCommand cmd = new SqlCommand(QueryString, myConnection))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; myConnection.Open();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataTable dt = new DataTable();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; dt.Load(dr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; budgetDDL1.SelectedIndex = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; budgetDDL1.DataSource = dt;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; budgetDDL1.DataTextField = "textvalue";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; budgetDDL1.DataValueField = "BudgetCode";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; budgetDDL1.AutoPostBack = true;&nbsp; &nbsp; &nbsp; &nbsp; budgetDDL1.SelectedIndexChanged += budgetDDL1_SelectedIndexChanged;&nbsp; &nbsp; &nbsp; &nbsp; budgetDDL1.DataBind();&nbsp; &nbsp; &nbsp; &nbsp; table1.Controls.Add(budgetDDL1);&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP