简体   繁体   English

如何阅读动态创建的文本框

[英]How can I read a dynamically created textbox

I create some dynamic textbox's and a button in a placeholder and would like to save info in textbox's when button is clicked but not sure how to retrieve data from the textbox 我在占位符中创建了一些动态文本框和一个按钮,并希望在单击按钮时将信息保存在文本框的中,但是不确定如何从文本框中检索数据

LiteralControl spacediv3 = new LiteralControl("&nbsp&nbsp");
Label  lblComText = new Label();
lblComTitle.Text = "Comment";
TextBox txtComment = new TextBox();
txtComment.Width = 200;
txtComment.TextMode = TextBoxMode.MultiLine;
phBlog.Controls.Add(lblComText);
phBlog.Controls.Add(spacediv3);
phBlog.Controls.Add(txtComment);

Button btnCommentSave = new Button();
btnCommentSave.ID = "mySavebtnComments" ;
btnCommentSave.Text = "Save ";
phBlog.Controls.Add(btnCommentSave);
btnCommentSave.CommandArgument = row["ID"].ToString();
btnCommentSave.Command += new CommandEventHandler(btnSave_Click);

protected void btnSave_Click(object sender, CommandEventArgs e)
{
    firstelement.InnerText = txtComment.text // this gives error on txtComment.text
}

You need to get a reference to your control in btnSave_Click. 您需要在btnSave_Click中获得对控件的引用。 Something like: 就像是:

protected void btnSave_Click(object sender, CommandEventArgs e)
{
    var btn = (Button)sender;
    var container = btn.NamingContainer;
    var txtBox = (TextBox)container.FindControl("txtComment");
    firstelement.InnerText = txtBox.text // this gives error on txtComment.text
}

You also need to set the ID on txtComment and recreate any dynamically created controls at postback. 您还需要在txtComment上设置ID,并在回发时重新创建任何动态创建的控件。

You will need some mechanism to create a relation between the Button and the TextBox obviously. 显然,您将需要某种机制来创建Button和TextBox之间的关系。 In winforms this would be easy, where each control has a Tag property, which can contain a reference to pretty much anything. 在winforms中,这很容易,其中每个控件都有一个Tag属性,该属性可以包含对几乎所有内容的引用。 The web controls don't have such a property (that I know of), but it is still easy to maintain such relations. Web控件没有这样的属性(我知道),但是保持这种关系仍然很容易。 One approach would be to have Dictionary in the page storing button/textbox relations: 一种方法是在存储按钮/文本框关系的页面中使用Dictionary

private Dictionary<Button, TextBox> _buttonTextBoxRelations = new  Dictionary<Button, TextBox>();

When you create the button and textbox controls, you insert them in the dictionary: 创建按钮和文本框控件时,将它们插入字典中:

TextBox txtComment = new TextBox();
// ...

Button btnCommentSave = new Button();
// ...
_buttonTextBoxRelations.Add(btnCommentSave, txtComment);

...and then you can look up the text box in the button's click event: ...然后您可以在按钮的click事件中查找文本框:

protected void btnSave_Click(object sender, CommandEventArgs e)
{
    TextBox commentTextBox = _buttonTextBoxRelations[(Button)sender];
    firstelement.InnerText = txtComment.text // this gives error on txtComment.text
}

Add an 'ID' to the textbox 在文本框中添加一个“ ID”

txtComment.ID = "txtComment"

Request the information from the submitted form (provided you have a form on the page) 从提交的表单中请求信息(如果您在页面上有表单)

comment = Request.Form("txtComment")

Try during postback to load txtComment (with the same ID) in overridden LoadViewState method after calling base.LoadViewState. 尝试在回发期间调用base.LoadViewState之后,在重写的LoadViewState方法中加载txtComment(具有相同的ID)。 In this case you do load it before postback data are handled and txtComment control comes loaded. 在这种情况下,您必须在处理回发数据和txtComment控件之前加载它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM