I am dynamically generating a ASP table and adding some Linkbuttons to it at runtime - this occurs at page load as it should be done.
The controls are all given an ID and i link them all up to an event handler. I also make the buttons have a static ID - i read this is supposed to stop ASP from giving problems when the number of link buttons changes.
In a nutshell my code during Page_Load I just call the GenerateTable code something like this:
GenerateTable(); to create the table, the method looks like this:
public void GenerateTable()
{
-- code to create ASP.NET table, rows, columns.
-- at a certain point based on logic generate the following (multiple tmes) depending on a viewstate variable Viewstate["A"] //simplified example.
System.Web.UI.WebControls.LinkButton lb = new LinkButton();
lb.ClientIDMode = System.Web.UI.ClientIDMode.Static;
lb.ID = workplaceCounter; //gives them a static name
lb.Text = "Show more..";
lb.CommandArgument = this.WorkplaceID.ToString();
lb.CssClass = "grdTextMore paddedtext";
lb.Click += new EventHandler(linkbuttonclicked);
Page.Controls.Add(lb);
Cell.Controls.Add(lb);
}
When a user clicks on the link button the event fires and sets the Viewstate["A"] variable which is used to generate the table on Page_Load correctly, however to make it update the table on the event i first Clear() the table and then call my GenerateTable();
function again so that it picks up the changes immediatly otherwise nothing will happen on the click.
The problem with this is that the linkbutton controls are recreated in the GenerateTable() function and when i click on them now they do not work until i click on them a second time.
Simplied this looks like this:
Cycle 1:
Generate Table and Links on Page_Load
Click on link button and clear and regenerate table and links (same IDs). (if i do not regenerate the table nothing happens here, if i do not clear i get 2 tables)
(The link button that was clicked on works fine)
Cycle 2:
When a second link button is clicked the Page_Load fires but the LinkButton event does not fire. (the page creates the buttons correctly)
Clicking on the link button works fine the second time and I regenerate the table and links
Cycle 3:
When a third link button is clicked the Page_Load fires but the LinkButton event does not fire. (the page creates the buttons correctly)
Clicking on the link button works fine the second time and i regenerate the table and links
No buttons work when clicked the first time because they were generated too late in the page state process.
The other alternative to this is that i tried to the access event and control that was executed on page load then i could generate the table at this point in time but i was not able to do this.
Gany__
0 Points
3 Posts
Dynamic controls created and viewstate
Oct 29, 2012 05:04 PM|LINK
Hi
I am dynamically generating a ASP table and adding some Linkbuttons to it at runtime - this occurs at page load as it should be done.
The controls are all given an ID and i link them all up to an event handler. I also make the buttons have a static ID - i read this is supposed to stop ASP from giving problems when the number of link buttons changes.
In a nutshell my code during Page_Load I just call the GenerateTable code something like this:
GenerateTable(); to create the table, the method looks like this:
public void GenerateTable()
{
-- code to create ASP.NET table, rows, columns.
-- at a certain point based on logic generate the following (multiple tmes) depending on a viewstate variable Viewstate["A"] //simplified example.
System.Web.UI.WebControls.LinkButton lb = new LinkButton();
lb.ClientIDMode = System.Web.UI.ClientIDMode.Static;
lb.ID = workplaceCounter; //gives them a static name
lb.Text = "Show more..";
lb.CommandArgument = this.WorkplaceID.ToString();
lb.CssClass = "grdTextMore paddedtext";
lb.Click += new EventHandler(linkbuttonclicked);
Page.Controls.Add(lb);
Cell.Controls.Add(lb);
}
When a user clicks on the link button the event fires and sets the Viewstate["A"] variable which is used to generate the table on Page_Load correctly, however to make it update the table on the event i first Clear() the table and then call my GenerateTable(); function again so that it picks up the changes immediatly otherwise nothing will happen on the click.
protected void linkbuttonclicked(object sender, EventArgs e)
{
Viewstate["A"] = (LinkButton)sender.CommandArgument;
Table.Clear();
GenerateTable();
}
The problem with this is that the linkbutton controls are recreated in the GenerateTable() function and when i click on them now they do not work until i click on them a second time.
Simplied this looks like this:
Cycle 1:
Generate Table and Links on Page_Load
Click on link button and clear and regenerate table and links (same IDs). (if i do not regenerate the table nothing happens here, if i do not clear i get 2 tables)
(The link button that was clicked on works fine)
Cycle 2:
When a second link button is clicked the Page_Load fires but the LinkButton event does not fire. (the page creates the buttons correctly)
Clicking on the link button works fine the second time and I regenerate the table and links
Cycle 3:
When a third link button is clicked the Page_Load fires but the LinkButton event does not fire. (the page creates the buttons correctly)
Clicking on the link button works fine the second time and i regenerate the table and links
No buttons work when clicked the first time because they were generated too late in the page state process.
The other alternative to this is that i tried to the access event and control that was executed on page load then i could generate the table at this point in time but i was not able to do this.
Thanks
Shailendra S...
Member
551 Points
145 Posts
Re: Dynamic controls created and viewstate
Nov 15, 2012 01:32 PM|LINK
http://geekswithblogs.net/FrostRed/archive/2007/02/17/106547.aspx
www.techaray.com