【发布时间】:2015-09-17 11:36:33
【问题描述】:
我有一个网格视图,它的前三列是手动创建的,我想在后面的代码中动态添加剩余的列,而且剩余的列必须包含文本框。我该怎么做? 我尝试了以下链接中的代码: How to add TemplateField programmatically 通过使用它,我可以在列中添加文本框,但找不到访问文本框中值的方法
public class AddTemplateToGridView : ITemplate
{
ListItemType _type;
string _colName,status;
int i = 0;
public AddTemplateToGridView(ListItemType type, string colname,string stat)
{
_type = type;
_colName = colname;
status = stat;
}
public AddTemplateToGridView()
{
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_type)
{
case ListItemType.Item:
TextBox txt = new TextBox();
txt.ID =id;
if (status == "havevalue")
{
txt.DataBinding += new EventHandler(ht_DataBinding);
}
container.Controls.Add(txt);
break;
}
}
这是我添加文本框的代码。如何为每个文本框添加不同的 ID?而且我发现当我点击另一个按钮时id会丢失,有什么办法可以防止这种情况吗?
【问题讨论】:
-
"剩余的列必须包含文本框"??你能显示你当前的网格吗?我认为这是重复的:stackoverflow.com/questions/12581088/…
-
当您想在gridview 中获取文本框的值时,只需使用gridview 中的行数循环。通过使用以下代码获取值,
string Txt= (TextBox)gridViewID.Rows[i].Cells[0].FindControl("TextboxID").Text; -
我需要在按钮单击中获取每个文本框的值,我可以在按钮单击中获取值吗?
-
先生,当我使用此代码时。文本显示错误。然后我将代码编辑为 TextBox Txt = (TextBox)GridView1.Rows[i].Cells[j].FindControl("text");然后Txt获取空值
-
“文本”是您的文本框 ID 吗?如果返回 null 表示网格中没有 id 为“text”的文本框