我在gridview 中动态创建一个下拉列表和一个文本框。文本框当前已禁用。这两个控件的 ID 都是从 SQL 表中获取的(因此它们本质上也是动态的,因为从 SQL 接收到的值可能会发生变化)。
我希望仅当在新生成的下拉列表中选择“其他”时才启用文本框。下面是我正在生成的下拉列表的代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[0].Visible = false;
e.Row.Cells[1].Width = 400;
e.Row.Cells[2].Visible = false;
e.Row.Cells[3].Visible = false;
e.Row.Cells[4].Visible = false;
e.Row.Cells[5].Width = 300;
e.Row.Cells[6].Visible = false;
e.Row.Cells[7].Visible = false;
e.Row.Cells[8].Visible = false;
e.Row.Cells[9].Width = 300;
string anstype = e.Row.Cells[2].Text;
string ansname = e.Row.Cells[3].Text;
string othertype = e.Row.Cells[6].Text;
string othername = e.Row.Cells[7].Text;
if (anstype == "DropDownList")
{
DropDownList ddl = new DropDownList();
ddl.ID = ansname;
ddl.AutoPostBack = true;
ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
ddl.Width = 300;
e.Row.Cells[5].Controls.Add(ddl);
}
if (othertype == "Free Text")
{
TextBox txt = new TextBox();
txt.ID = othername;
txt.Width = 300;
txt.Enabled = false;
e.Row.Cells[9].Controls.Add(txt);
}
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddlName = new DropDownList();
ddlName = (DropDownList)sender;
string val = ddlName.SelectedItem.Text.ToString();
if (val == "Other")
{
//ENABLE THE TEXT BOX
}
}
我能想到的最简单的方法是将文本框的名称(othename 变量)传递给事件处理程序。虽然是 C# 的菜鸟,但我不知道该怎么做。请帮忙..!!!
!!..编辑..!!
我想出了引用文本的方法,但现在我面临另一个问题。由于“AutoPostBack”,整个 GridView 都会被刷新,并且下拉列表的偶数不会被触发。有什么建议么?