views:

516

answers:

2

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.

+1  A: 

I think you should put this code under the check

if (!IsPostBack)
{
 textB = new TextBox();
 textB.ID = "dynamicTextC";
 Panel1.Controls.Add(textB)
}

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)

Try seeing the HTML markup it generates when the page is loaded for the first time & next time to understand the markup text for "dynamicTextC".

shahkalpesh
I don't understand. Why should I put it inside 'if' statement?
SourceC
Ahhh, the dreaded if(!IsPostBack). God I'm glad I'm using MVC now.
Simucal
A: 

Do you have a specific reason why you do not want to create the dynamic Control in Page_Init? This code seems to work:

  protected void Page_Init(object sender, EventArgs e) {
    textB = new TextBox();
    textB.ID = "dynamicTextC";
    Panel1.Controls.Add(textB);
  }


  protected void Page_Load(object sender, EventArgs e) {
    Label1.Text = textB.Text;
  }
I know that above code would work. But my question was more about what's going on under the hood and reasons why controls get populated with postback data twice
SourceC