Hello,
Controls are populated with postback data during LoadPostBack data Stage. If dynamic control is not created and added to control tree during OnInit stage, but later, then control won’t get populated with postback data, as evident by the following code, where Label1 will display an empty string:
public partial class _Default : System.Web.UI.Page
{
TextBox textB;
protected void Page_Load(object sender, EventArgs e)
{
textB = new TextBox();
textB.ID = "dynamicTextC";
Panel1.Controls.Add(textB);
Label1.Text = textB.Text; // displays an empty string
}
But if I put Label1.Text = textB.Text; inside event handler subscribed to textB’s TextChanged event, then Label1 will display text user entered into textB. Now that suggests that controls are populated with same postback data twice – during LoadPostBack data Stage and during Raise Postback Events stage. So why does control need to be populated with same postback data twice?
BTW - I realize that during Raise Postback Events stage control compares postback value to postback value from previous page load and then decides if event needs to be raised
TextBox textB;
protected void Page_Load(object sender, EventArgs e)
{
textB = new TextBox();
textB.ID = "dynamicTextC";
Panel1.Controls.Add(textB);
textB.TextChanged += textB_TextChanged;
}
protected void textB_TextChanged(object sender, EventArgs e)
{
Label1.Text = textB.Text; // Label1 displays value user
entered into textB
}
thanx
EDIT:
The control will get created and will be part of viewstate (because it is a server-side control. Hence, it need not be created again on postback)
That's not what happens, at least in my case TextBox dissapears on a postback.