简体   繁体   English

从代码隐藏创建aspx文本框

[英]Creating aspx textbox from codebehind

how can I create aspx textbox from code behind in C# and how to access its value in code behind ? 如何从C#中的代码创建aspx文本框以及如何在代码后面访问它的值? I do as follows but on every post backs text box is getting cleared. 我这样做如下,但在每个帖子后面的文本框都被清除。 I need to keep its values on post backs. 我需要在帖子后面保留它的价值。

TextBox txt = new TextBox();
txt.ID = "strtxtbox";
txt.CssClass = "CSS1";
StringBuilder sb = new StringBuilder();
StringWriter writer = new StringWriter(sb);
HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
txt.RenderControl(htmlWriter);

//lbl is an aspx label
lbl.text += @"<td style='width: 5%;'>" + sb.ToString() + "</td>";

And I access text box value as follows 我按如下方式访问文本框值

string tb = Request.Form["strtxtbox"].ToString();

You can start by creating the TextBox control. 您可以从创建TextBox控件开始。 It must be done in the Init() ( Page_Init() ) or PreInit() ( Page_PreInit() ) method, and you have to do it regardless of Page.IsPostBack . 它必须在完成Init() Page_Init()PreInit() Page_PreInit()方法,你必须这样做,无论Page.IsPostBack This will put the element on the page before the ViewState is loaded, and will allow you to retrieve the value on postback. 这将在加载ViewState之前将元素放在页面上,并允许您在回发时检索该值。

var textBox = new TextBox();

Then you should set a few properties on it, including an ID so you can find it later: 然后你应该在它上面设置一些属性,包括一个ID,以便你以后可以找到它:

textBox.ID = "uxTxtSomeName";
textBox.MaxLength = 10; // maximum input length
textBox.Columns = 20; // character width of the textbox
etc...

Then you need to add it to an appropriate container on the page ( Page , or whichever control you want it to appear within): 然后,您需要将其添加到页面上的适当容器( Page ,或您希望它出现在哪个控件中):

parentControl.Controls.Add(textBox);

Then on post back, you can retrieve the value, probably in the Load() method ( Page_Load() ) using the parent's FindControl() function: 然后在回发后,您可以使用父的FindControl()函数检索值,可能在Load()方法( Page_Load() )中:

var input = (parentControl.FindControl("uxTxtSomeName") as TextBox).Text;

Note: The built-in FindControl() only iterates through immediate children. 注意:内置的FindControl()仅迭代直接子节点。 If you want to search through the entire tree of nested server controls, you may need to implement your own recursive FindControl() function. 如果要搜索整个嵌套服务器控件树,可能需要实现自己的递归 FindControl()函数。 There are a million and one examples of recursive FindControl() functions on [so] though, so I'll leave that up to you. 虽然在[so]上有一百万个递归FindControl()函数的例子,所以我会把它留给你。

The problem is that the control won't be available on postback unless you recreate it every time, which is problematic. 问题是,除非您每次都重新创建控件,否则控件将无法在回发中使用,这是有问题的。

One solution I've used in the past is the DynamicControlsPlaceholder, you can check it out here . 我过去使用的一个解决方案是DynamicControlsPlaceholder,你可以在这里查看

Create the textbox as per the code in the comment 根据注释中的代码创建文本框

TextBox myTextBox=new TextBox();

however, you must set an ID/Name. 但是,您必须设置ID /名称。 Additionally, you must create the text box on every postback, in the pre-render or before, so that the value will be populated. 此外,您必须在每个回发,预渲染或之前创建文本框,以便填充该值。 If you delay creation of the textbox to later in the page lifecycle, the value will not be populated from the postback, and then you would have to retrieve it from the Request.Response[] collection manually. 如果您将文本框的创建延迟到页面生命周期的后期,则不会从回发中填充该值,然后您必须手动从Request.Response []集合中检索它。

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

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