我在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变量)的名称传递给eventhandler。虽然是C#的菜鸟,但我不知道该怎么做。请帮助.. !!!
!! EDIT .. !!
我想出了引用文本的方法,但现在是我面临的另一个问题。由于“AutoPostBack”,整个GridView得到刷新,下拉列表的偶数不会被触发。有什么建议吗?
答案 0 :(得分:1)
根据目前给出的评论,如果我可以总结一下,那么您有一组问题需要从数据库中加载调查。您希望人们从控件中选择答案,然后针对这些问题生成服务器端。到目前为止,这一切都是直截了当的,但是当它变得复杂时,你需要一个文本框,当他们从答案下拉列表中选择“其他”时,并在该框中获得其余的响应。如果您尝试在服务器端执行此操作,因为控件通常不属于gridview的列定义,它们都会消失,您将丢失答案。
为什么不尝试总是在那里使用文本框,而是通过javascript设置CSS visibility property呢?示例函数(源自here)是:
<script type="text/javascript">
function toggleVisibility(controlId)
{
var control = document.getElementById(controlId);
if(control.style.visibility == "visible" || control.style.visibility == "")
control.style.visibility = "hidden";
else
control.style.visibility = "visible";
}
</script>
因此,您将在下拉列表的Click
事件中绑定对此函数(或包装函数)的调用,并始终在页面上显示文本框,只是隐藏。这样,在完成并提交所有答案之前,您不需要回复。