[英]Dropdownlist SelectIndexChanged Not firing C#
I don't get on well with post back/DDL's.. yes I have used autopostback = true! 我对回发/ DDL不太满意。是的,我使用了autopostback = true!
Below, I am trying to get the selected index changed... to fire on budgetDDL1 however, whatever I try it doesn't! 在下面,我尝试更改选定的索引...但是在预算DDL1上触发,但是无论如何,我都不会尝试!
I'm binding data from a DB to the ddl... 我正在将数据从数据库绑定到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?! 我试过将ddl绑定/添加到表的内部/外部,以及启用/禁用视图状态等。这些都不起作用..必须有一个简单的答案?!
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 . 动态控件应添加在Init上 。 After OnLoad finishes executing ASP.NET starts processing control's events and values.
OnLoad完成执行后,ASP.NET将开始处理控件的事件和值。 You can read/write their properties on or after Load.
您可以在加载时或加载后读取/写入它们的属性。
Suggest you to check the place where you have created DropDownList . 建议您检查创建DropDownList的位置 。
Answer: because you have created DropDownList after the events have been processed. 答案:因为您已经在处理事件之后创建了DropDownList。
Have a look here: http://www.4guysfromrolla.com/articles/092904-1.aspx 在这里看看: http : //www.4guysfromrolla.com/articles/092904-1.aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.