简体   繁体   English

无法从动态创建的文本框中获取值

[英]Not able to get the value from dynamically created textbox

I have created a textbox programatically in page load as using the below code: 我使用以下代码在页面加载中以编程方式创建了一个文本框:

HtmlTableRow row = new HtmlTableRow();
HtmlTableCell cell1 = new HtmlTableCell();
HtmlTableCell cell2 = new HtmlTableCell();
cell1.Controls.Add(new Label() { ID = LableID1, Text = Name });
cell2.Controls.Add(new TextBox() { ID = TextBoxID1 });
row.Cells.Add(cell1);
row.Cells.Add(cell2);
dynamictable.Rows.Add(row);

And In the button click event i am trying to get the value from the Textbox and assign that value to anohter TextBox which is statically created as below: 在按钮单击事件中,我试图从文本框中获取值并将该值分配给静态创建的anohter TextBox,如下所示:

string id = TextBoxID1                       
TextBox tb = (TextBox)dynamictable.FindControl(id);                        
string valuetext = tb.Text;
TextBox1.Text = valuetext;

Am getting Object Reference Error, I mean, i am not able to Find the control and create the TextBox. 正在收到“对象引用错误”,我是说,我无法找到控件并创建TextBox。

I have tried to create the TextBox as below method also: 我也尝试创建TextBox如下方法:

TextBox tb = (TextBox)form1.FindControl(id);                        
TextBox tb = (TextBox)this.form1.FindControl(id);                        
TextBox tb = (TextBox)page.FindControl(id);  

Any help would be highly helpful for me. 任何帮助都会对我有很大帮助。

I think you might need to find the row and then the cell and then find the textbox. 我认为您可能需要先找到行,然后再找到单元格,然后找到文本框。

Means inpite of doing this: 意味着尽管这样做:

TextBox tb = (TextBox)dynamictable.FindControl(id);   

You need to find the specific row first like 您需要先找到特定的行

// find by it or index etc
HtmlTableRow tb = (HtmlTableRow)dynamictable.FindControl(id);  

// Then find the Table cell and then find textbox..

I hope this will help you 我希望这能帮到您

You might have a method that creates the table, you need to call it on postback to ensure everything is setup. 您可能有一个创建表的方法,您需要在回发时调用它以确保一切都已设置。

protected HtmlTable dynamictable;
protected TextBox tb = new TextBox();


protected override void OnInit(EventArgs args)
{
 base.OnInit(args);
 CreateTableRows();
}

private void CreateTableRows()
{
 HtmlTableRow row = new HtmlTableRow();
 HtmlTableCell cell1 = new HtmlTableCell();
 HtmlTableCell cell2 = new HtmlTableCell();
 cell1.Controls.Add(new Label() { ID = LableID1, Text = Name });
 cell2.Controls.Add(tb });
 row.Cells.Add(cell1);
 row.Cells.Add(cell2);
 dynamictable.Rows.Add(row);
}

protected void OnClick(object sender, EventArgs args)
{
 return tb.Text;
}

In order to work with dynamic controls you need to fully understand the ASP.Net Page Life-cycle 为了使用动态控件,您需要完全了解ASP.Net页面生命周期

Dynamic controls need to be recreated on each post , there's no magic behind responsible of creating your dynamic controls for you, sadly you have to create them explicitly. 需要在每个帖子上重新创建动态控件,为您创建动态控件没有背后的魔力,可悲的是您必须显式地创建它们。

Remember that a page is simply a class that is created when you perform a request and destroyed when the response is sent back to the user . 请记住,页面只是在执行请求时创建类,而在将响应发送回用户时销毁的页面 Therefore controls need to be recreated every time. 因此,需要每次重新创建控件。 This is done for you when the controls are statically declared on the page. 当控件在页面上静态声明时,将为您完成此操作。 But with dynamic controls, you need to recreate them on each post 但是对于动态控件,您需要在每个帖子上重新创建它们

The code provided by @BobTodd is a good start point, however the recommendation from Microsoft is that dynamic controls should be created in the Page_Init event in order to sync their events with the rest of the static controls. @BobTodd提供的代码是一个很好的起点,但是Microsoft的建议是应在Page_Init事件中创建动态控件,以便将其事件与其余静态控件同步。

So your code would look like: 因此您的代码如下所示:

protected void Page_Init(object sender, EventArgs e)
{
 CreateTable();
}

Now, remember this simple rule of dumb, when working with dynamic controls, use always the same ID . 现在,请记住这个简单的哑巴规则,当使用动态控件时,请始终使用相同的ID This is really important because the page viewstate is loaded back based on the control's ID. 这真的很重要,因为页面视图状态是根据控件的ID重新加载的。

Another thing to consider is that all controls created in the Init event, won't load their viewstate until the LoadViewState method is called on every control on the page. 要考虑的另一件事是,在页面上的每个控件上调用LoadViewState方法之前,在Init事件中创建的所有控件都不会加载其视图状态。 This means that if you subscribe to the Page_PreLoad or Page_Load events, you can safely set the control's properties since their value were already loaded from the viewstate and therefore your new values won't be overridden. 这意味着,如果您预订Page_PreLoadPage_Load事件,则可以安全地设置控件的属性,因为它们的值已经从viewstate中加载,因此您的新值将不会被覆盖。

This means that any property assigned to the controls before the PreLoad event, will be replaced by the page viewstate value . 这意味着在PreLoad事件之前分配给控件的任何属性都将被页面viewstate值替换 Therefore is considered a good practice to set your dynamic controls properties after the viewstate has been loaded. 因此,在加载视图状态后设置动态控件属性被认为是一种好习惯。

As a quick view, check the ASP.Net page life-cycle: 快速查看一下ASP.Net页面的生命周期:

在此处输入图片说明

use hidden field to store value of dynamically created text box in java script 使用隐藏字段在Java脚本中存储动态创建的文本框的值

also add runat="server" in hidden feild 还添加runat="server"中隐藏费尔德

and you can access your textbox value from hidden feild. 您可以从隐藏的字段访问您的文本框值。

Another way is to store that value in query string using javascript and then reading from it at back end 另一种方法是使用javascript将值存储在查询字符串中,然后在后端读取该值

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

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