访问C#中动态创建的文本框
我的代码在C#中动态生成一个文本框(访问C#中动态创建的文本框,c#,asp.net,textbox,findcontrol,C#,Asp.net,Textbox,Findcontrol,我的代码在C#中动态生成一个文本框(page_load函数)。我可以稍后在代码中访问它吗?它确实给了我编译错误,而且似乎不起作用。有人能证实吗 附加问题代码 aContent += "<table>"; aContent += "<tr><td>lablel </td><td style='bla blah'><input type='textbox' id='col-1' name='col-1'/></td>
page_load
函数)。我可以稍后在代码中访问它吗?它确实给了我编译错误,而且似乎不起作用。有人能证实吗
附加问题代码
aContent += "<table>";
aContent += "<tr><td>lablel </td><td style='bla blah'><input type='textbox' id='col-1' name='col-1'/></td></tr> ... 10 such rows here
</table>"
spanMap.InnerHtml = aContent;
假设您正确地持久化了它,您应该能够使用
FindControl
方法在代码隐藏中访问它。根据控件的位置,您可能必须通过控件层次结构进行递归搜索:
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
使用FindControlRecursive
:
TextBox txt = this.FindControlRecursive(Page.Form, "TextBox1") as TextBox;
if (txt != null)
{
string text = txt.Text;
}
如果使用上述方法仍无法找到该控件,请确保在每次回发之后创建控件,例如在Page\u Load
之前,如OnInit
编辑
我认为您需要改变向容器添加内容的方式。我不使用
,而是使用面板
,而不是构建标记,只需在代码隐藏中向面板添加控件:
TextBox txt = new TextBox();
txt.ID = String.Format("txt_{0}", Panel1.Controls.Count);
Panel1.Controls.Add(txt);
假设您正确地持久化了它,您应该能够使用
FindControl
方法在代码隐藏中访问它。根据控件的位置,您可能必须通过控件层次结构进行递归搜索:
private Control FindControlRecursive(Control root, string id)
{
if (root.ID == id)
{
return root;
}
foreach (Control c in root.Controls)
{
Control t = FindControlRecursive(c, id);
if (t != null)
{
return t;
}
}
return null;
}
使用FindControlRecursive
:
TextBox txt = this.FindControlRecursive(Page.Form, "TextBox1") as TextBox;
if (txt != null)
{
string text = txt.Text;
}
如果使用上述方法仍无法找到该控件,请确保在每次回发之后创建控件,例如在Page\u Load
之前,如OnInit
编辑
我认为您需要改变向容器添加内容的方式。我不使用
,而是使用面板
,而不是构建标记,只需在代码隐藏中向面板添加控件:
TextBox txt = new TextBox();
txt.ID = String.Format("txt_{0}", Panel1.Controls.Count);
Panel1.Controls.Add(txt);
下面是一个例子:
<%@ Page Language="C#" %>
<script type="text/C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
var textBox = new TextBox();
textBox.ID = "myTextBox";
textBox.Text = "hello";
Form1.Controls.Add(textBox);
}
protected void BtnTestClick(object sender, EventArgs e)
{
var textBox = (TextBox)Form1.FindControl("myTextBox");
lblTest.Text = textBox.Text;
}
</script>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<asp:LinkButton ID="btnTest" runat="server" Text="Click me" OnClick="BtnTestClick" />
<asp:Label ID="lblTest" runat="server" />
</form>
</body>
</html>
受保护的无效页面加载(对象发送方、事件参数e)
{
var textBox=新的textBox();
textBox.ID=“myTextBox”;
textBox.Text=“你好”;
Form1.Controls.Add(文本框);
}
受保护的无效BtnTestClick(对象发送方,事件参数e)
{
var textBox=(textBox)Form1.FindControl(“myTextBox”);
lblTest.Text=textBox.Text;
}
以下是一个示例:
<%@ Page Language="C#" %>
<script type="text/C#" runat="server">
protected void Page_Load(object sender, EventArgs e)
{
var textBox = new TextBox();
textBox.ID = "myTextBox";
textBox.Text = "hello";
Form1.Controls.Add(textBox);
}
protected void BtnTestClick(object sender, EventArgs e)
{
var textBox = (TextBox)Form1.FindControl("myTextBox");
lblTest.Text = textBox.Text;
}
</script>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="Form1" runat="server">
<asp:LinkButton ID="btnTest" runat="server" Text="Click me" OnClick="BtnTestClick" />
<asp:Label ID="lblTest" runat="server" />
</form>
</body>
</html>
受保护的无效页面加载(对象发送方、事件参数e)
{
var textBox=新的textBox();
textBox.ID=“myTextBox”;
textBox.Text=“你好”;
Form1.Controls.Add(文本框);
}
受保护的无效BtnTestClick(对象发送方,事件参数e)
{
var textBox=(textBox)Form1.FindControl(“myTextBox”);
lblTest.Text=textBox.Text;
}
你能把你正在使用的代码贴出来吗?你的代码在哪里?你把它添加到页面控件了吗?您是否在每次回发时都重新添加它?有人如何验证?把密码贴出来。我在问是否可能。我认为这个问题解释得很好。顺便说一句,这不是我的代码,是别人写的。这是一个老问题,但第一个“文本框”不是文本框,它是html输入。您应该添加textbox,比如textbox nTxt=newtextbox();nTxt.id=“col-1”;spanMap.Controls.Add(nTxt);你能把你用的密码贴出来吗?你的密码在哪里?你把它添加到页面控件了吗?您是否在每次回发时都重新添加它?有人如何验证?把密码贴出来。我在问是否可能。我认为这个问题解释得很好。顺便说一句,这不是我的代码,是别人写的。这是一个老问题,但第一个“文本框”不是文本框,它是html输入。您应该添加textbox,比如textbox nTxt=newtextbox();nTxt.id=“col-1”;spanMap.Controls.Add(nTxt);所以我想FindControl是我访问它的唯一方法。我不能直接访问它@下面的sylence确实提到添加引用,但这并不十分清楚。是的,您肯定需要FindControl
来访问动态控件。您无法直接访问它。我尝试了此代码,但无效。我的html实际上在一个范围内<代码>spanMap.InnerHtml=a内容代码>。里面有一张桌子,桌子里面是我的文本框。虽然您需要将作为Textbox
更改为Textbox(…)
,但在第二段代码中,我很困惑。。。有什么问题吗?上面的方法在遍历树时应该没有任何问题。我在问题中包含了其他代码。它只是找不到任何子元素,只有一个literalcontrol。如果有什么不同的话,我也在使用母版。我以前使用过它,但它确实有效(不是这个特殊的例子),所以我想FindControl是我访问它的唯一方法。我不能直接访问它@下面的sylence确实提到添加引用,但这并不十分清楚。是的,您肯定需要FindControl
来访问动态控件。您无法直接访问它。我尝试了此代码,但无效。我的html实际上在一个范围内<代码>spanMap.InnerHtml=a内容代码>。里面有一张桌子,桌子里面是我的文本框。虽然您需要将作为Textbox
更改为Textbox(…)
,但在第二段代码中,我很困惑。。。有什么问题吗?上面的方法在遍历树时应该没有任何问题。我在问题中包含了其他代码。它只是找不到任何子元素,只有一个literalcontrol。如果有什么不同的话,我也在使用母版。我以前使用过它,但它确实有效(不是这个特殊的例子)。