【发布时间】: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