Question

I'm currently working on a website using c# and asp.net. For this purpose, I need to create dynamic controls but I enconter some issues. I already read official documentation and searched for lots of tutorial but unfortunately, no one allowed me to fix this problem.

Here is a very simplified example of what I'm trying to do;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        CreateControls();
    else
        UpdatePage();
}

protected void CreateControls()
{
    Button button1 = new Button();
    button1.ID = "_Button1";
    button1.Text = "Button1";
    button1.Click += new System.EventHandler(_ClickEvent);
    _Panel.Controls.Add(button1);

    Button button2 = new Button();
    button2.ID = "_Button2";
    button2.Text = "Button2";
    button2.Click += new System.EventHandler(_ClickEvent);
    _Panel.Controls.Add(button2);
}

protected void UpdatePage()
{
    Button button1 = ((Button)_Panel.FindControl("_Button1"));
    button1.Text = "I went through UpdatePage and changed";

    Button button2 = ((Button)_Panel.FindControl("_Button2"));
    button1.Text = "I went through UpdatePage and changed";
}

protected void _ClickEvent(object sender, EventArgs e)
{

}

The aim here would just be to change the buttons' text when clicking on one of them. "Page_Load" method is correctly called as well as the "UpdatePage" one, but in the latter, Button1 and Button2 controls have disappeared (they are not in the panel controls anymore) and an NullPointer exception is obviously raised.

Would anybody have an explanation ? I know I probably missed something about page life cycle but could not find any clear solution anywhere.

Thanks a lot !

Was it helpful?

Solution

You are creating the controls the first time the page is loaded, but the Page_Load event is too late to add controls to the page and have WebForms know about that.

On the initial page load, somewhere between the OnInit and Page_Load, WebForms makes a note of what controls are currently on the page and sets them up in the view state and all of that stuff, so that they next time you post back it knows those controls should be there. If you don't add your controls until Page_Load, WebForms isn't really paying attention any more to what you're adding to the page, so they next time you post back it doesn't know to put those controls on the page.

Move your CreateControls call into the OnInit method. This will tell WebForms at the appropriate time to create the controls (about the same time that any controls from the .aspx markup get added, although slightly later). Then WebForms will be aware of those controls and will apply any view state necessary (if it's a postback), and then finally on Page_Load you can muck with the control data with your UpdatePage call.

protected override void OnInit(EventArgs e)
{
     base.OnInit(e);
     CreateControls();
}

Think of OnInit as "put all of the controls on the page and wire up event handlers".

Think of Page_Load as "put data into the controls that are already there".

OTHER TIPS

The controls that you are creating dynamically will be lost on postback. Try this:

protected void Page_Load(object sender, EventArgs e)
{
        CreateControls();
        UpdatePage();
}

protected void CreateControls()
{
    Button button1 = new Button();
    button1.ID = "_Button1";
    button1.Text = "Button1";
    button1.Click += new System.EventHandler(_ClickEvent);
    _Panel.Controls.Add(button1);

    Button button2 = new Button();
    button2.ID = "_Button2";
    button2.Text = "Button2";
    button2.Click += new System.EventHandler(_ClickEvent);
    _Panel.Controls.Add(button2);
}

protected void UpdatePage()
{
    Button button1 = ((Button)_Panel.FindControl("_Button1"));
    button1.Text = "I went through UpdatePage and changed";

    Button button2 = ((Button)_Panel.FindControl("_Button2"));
    button1.Text = "I went through UpdatePage and changed";
}

protected void _ClickEvent(object sender, EventArgs e)
{

}

Will try it:

    protected String TextButton1
    {
        get { return (String) ViewState["TextButton1"]; }
        set { ViewState["TextButton1"] = value; }
    }

    protected String TextButton2
    {
        get { return (String)ViewState["TextButton2"]; }
        set { ViewState["TextButton2"] = value; }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        CreateControls();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            UpdatePage();
        }
    }

    protected void CreateControls()
    {
        Button button1 = new Button();
        button1.ID = "_Button1";
        button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1; 
        button1.Click += new System.EventHandler(_ClickEvent1);
        _Panel.Controls.Add(button1);

        Button button2 = new Button();
        button2.ID = "_Button2";
        button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2; 
        button2.Click += new System.EventHandler(_ClickEvent2);
        _Panel.Controls.Add(button2);
    }

    protected void UpdatePage()
    {
        Button button1 = ((Button)_Panel.FindControl("_Button1"));
        button1.Text = String.IsNullOrEmpty(TextButton1) ? "The First Value" : TextButton1; 

        Button button2 = ((Button)_Panel.FindControl("_Button2"));
        button2.Text = String.IsNullOrEmpty(TextButton2) ? "The First Value" : TextButton2; 
    }

    protected void _ClickEvent1(object sender, EventArgs e)
    {
        TextButton1 = "test";
        Button b = (Button) sender ;
        b.Text = TextButton1;
    }

    protected void _ClickEvent2(object sender, EventArgs e)
    {
        TextButton2 = "test";
        Button b = (Button)sender;
        b.Text = TextButton2;
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top