简体   繁体   中英

Dropdownlist SelectIndexChanged Not firing C#

I don't get on well with post back/DDL's.. yes I have used autopostback = true!

Below, I am trying to get the selected index changed... to fire on budgetDDL1 however, whatever I try it doesn't!

I'm binding data from a DB to the ddl...

I've tried binding/adding the ddl to the table inside/outside the post backs and enabling/disabling the view states etc.. none of this works.. there must be an easy answer?!

In what order do I need to create/bind the dropdowns for the index changed method to fire an explanation would be useful too!

  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> 

Place auto post back code out side of the ! 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);
    }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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