0

I have some buttons that are being dynamically added to an asp.net page. However the onclick event is not being fired. Here is the code for it being added and it is ran when the page loads. I am very new to ASP.NET so I am sure I am making some basic errors. TIA.

    protected void Page_Load(object sender, EventArgs e)
    {
        FillTable();
        string rownum = (goalstable.Rows.Count).ToString();
        Button bt = new Button();
        bt.Text = "View";
        bt.ID = (rownum);
        bt.CssClass = "button";
        bt.Click += Viewbutton_Click;
        goalstable.Rows[1].Cells[0].Controls.Add(bt);
    }

FillTable() is a method that fills a table from an SQL DB. The on click event for the button that has been added.

     protected void Viewbutton_Click(object sender, EventArgs e)
    {
        getGID();
        setGoalDets();
        goals.Style.Add("display", "block");
        darkLayer2.Style.Add("display", "block"); 
    }

Any Ideas what I may be doing wrong.

ProgrammingRookie
  • 193
  • 3
  • 3
  • 17
  • Or possible duplicate of one of the myriad of identically titled questions in the sidebar over there -------->>>> – J... Apr 25 '14 at 16:47

1 Answers1

3

In a nutshell, you need to add the button earlier in the Page lifecycle, before the Page_Load event.

What's happening is every server event — even simple button clicks — is a new HTTP request to your page. In turn, every HTTP request for your page results in a completely new-from-scratch C# page object. Therefore you start with a brand new Page object and a brand new ViewButton when the click event for your ViewButton is triggered.

To make things work correctly, so the new page has the same properties as the old, ASP.Net relies on a feature called ViewState. ViewState information is (typically) submitted with the http request from the client's browser, and is used to build a new Page object with the same Controls and property values as the old one.

Here's the trick: ViewState is restored for the page before the load event is processed. If the button does not exist yet at the time the ViewState is restored, that information is thrown away, and the page will not later know it needs to raise the click event (or rather, it will think there there is no button for the click event code to run in the first place).

Therefore, you need to move the code to create your button to the Pre_Init event, which runs before the ViewState is restored.

When working with "dynamic" controls in ASP.Net WebForms, I often find it easier to just add a reasonable number of controls to the page in a static manner and set them all so that their Visible property is false. Then at runtime I will set Visible back to true for just the controls that I need.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • Thanks for the nice explanation starting to get more to grips with the page life cycle, a lot to take on when first learning and all. I do like your idea of adding plenty of controls and just change the visibility. Thanks Once again – ProgrammingRookie Apr 25 '14 at 16:57