Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Nov 15, 2012 01:32 PM by Shailendra Saxena
Oct 29, 2012 05:04 PM|LINK
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);
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;
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:
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)
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
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.
Nov 15, 2012 01:32 PM|LINK