Aspx gets value of dynamically added text field

I added two text fields dynamically to the aspx page.

I would like to get the values ​​from these two text fields on the server side after the postback (after pressing Btn1).

And here is my first problem - the controls were not found. I tried to find the controls with:

Page.FindControl("txt4"); 

What is wrong with that? Isn't all the pages sent back with all the controls?

My other question is: where to get these values ​​in the code? In the Page_load event, before they are added again? I assume that when the Btn1_Click event is fired, these two controls are already added, so are the values ​​of the original postback lost? (The Page_load event fires before Btn1_Click).

I am really struggling with this.

This is much simpler if controls are added via markup - they are directly accessible in the code behind their identifier. But in the project that I am currently working on, controls are mostly added dynamically, and many of them.

Code behind:

  public partial class About : Page { protected void Page_Load(object sender, EventArgs e) { btn1.Click += Btn1_Click; if(IsPostBack) { System.Web.UI.Control txt4_dynamic = Page.FindControl("txt4"); System.Web.UI.Control txt5_dynamic = Page.FindControl("txt5"); if(txt4_dynamic != null) { string str1 = ((TextBox)txt4_dynamic).Text; } if (txt5_dynamic != null) { string str1 = ((TextBox)txt5_dynamic).Text; } } TextBox txt4 = new TextBox(); txt4.ClientIDMode = ClientIDMode.Static; txt4.ID = "txt4"; TextBox txt5 = new TextBox(); txt5.ClientIDMode = ClientIDMode.Static; txt5.ID = "txt5"; panel1.Controls.Add(txt4); panel1.Controls.Add(txt5); } private void Btn1_Click(object sender, EventArgs e) { System.Web.UI.Control txt4_dynamic = Page.FindControl("txt4"); System.Web.UI.Control txt5_dynamic = Page.FindControl("txt5"); if (txt4_dynamic != null) { string str1 = ((TextBox)txt4_dynamic).Text; } if (txt5_dynamic != null) { string str1 = ((TextBox)txt5_dynamic).Text; } } } 

Markup:

 <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <asp:Button ID="btn1" runat="server" Text="Button" /> <asp:Panel ID="panel1" runat="server" ClientIDMode="Static"> </asp:Panel> </asp:Content> 

UPDATE:

I updated my code with

  protected void Page_Init(object sender, EventArgs e) { if(IsPostBack) { TextBox txt4 = new TextBox(); txt4.ClientIDMode = ClientIDMode.Static; txt4.ID = "txt4"; TextBox txt5 = new TextBox(); txt5.ClientIDMode = ClientIDMode.Static; txt5.ID = "txt5"; panel1.Controls.Add(txt4); panel1.Controls.Add(txt5); string st1 = txt4.Text; string st2 = txt5.Text; System.Web.UI.Control txt4_dynamic = Page.FindControl("txt4"); System.Web.UI.Control txt5_dynamic = Page.FindControl("txt5"); if (txt4_dynamic != null) { string str1 = ((TextBox)txt4_dynamic).Text; } if (txt5_dynamic != null) { string str1 = ((TextBox)txt5_dynamic).Text; } } } 

I checked the code in the debugger - the controls are added to the postback, but the values ​​for st1, st2, txt4_dynamic and txt5_dynamic are still empty / null.

UPDATE 2:

There were two problems:

  • The search for the control should be performed using:
 Page.Controls[0].FindControl("MainContent").FindControl("txt4"); 
  1. The code above finds the control text only in control events (and they are triggered after Page_Load ).

So: everything can stay, since I was originally posted - the only change that is required is the code to search for the control, which should be in the click event. If the same code is in the Page_Load event, it will not work.

0
source share
1 answer

Modify the code as follows (basically move creating / adding dynamic controls to Page_Init ):

 public partial class About : Page { protected void Page_Init(object sender, EventArgs e) { TextBox txt4 = new TextBox(); txt4.ClientIDMode = ClientIDMode.Static; txt4.ID = "txt4"; TextBox txt5 = new TextBox(); txt5.ClientIDMode = ClientIDMode.Static; txt5.ID = "txt5"; panel1.Controls.Add(txt4); panel1.Controls.Add(txt5); } protected void Page_Load(object sender, EventArgs e) { btn1.Click += Btn1_Click; if (IsPostBack) { System.Web.UI.Control txt4_dynamic = Page.Controls[0].FindControl("MainContent").FindControl("txt4"); System.Web.UI.Control txt5_dynamic = Page.Controls[0].FindControl("MainContent").FindControl("txt5"); if (txt4_dynamic != null) { string str1 = ((TextBox)txt4_dynamic).Text; } if (txt5_dynamic != null) { string str1 = ((TextBox)txt5_dynamic).Text; } } } private void Btn1_Click(object sender, EventArgs e) { System.Web.UI.Control txt4_dynamic = Page.Controls[0].FindControl("MainContent").FindControl("txt4"); System.Web.UI.Control txt5_dynamic = Page.Controls[0].FindControl("MainContent").FindControl("txt5"); if (txt4_dynamic != null) { string str1 = ((TextBox)txt4_dynamic).Text; } if (txt5_dynamic != null) { string str1 = ((TextBox)txt5_dynamic).Text; } } } 

A management tree is created on the server side with each postback. Because controls are added dynamically, they need to be added to every answer that you already make.

The reason for moving this code to the Page_Init event is because the values ​​sent from clients are set in the LoadPostData event, which occurs after Page_Init and before Page_Load .
When you have the code in Page_Init , by the time of the LoadPostData event, LoadPostData will happen that the control tree has already been created and the displayed value is set correctly.
When you had the code in Page_Load , the LoadPostData event occurred before the control was created and the output value was not set.

+2
source

Source: https://habr.com/ru/post/1312513/


All Articles