One solution is to put a hidden form field on the form. This hidden field
can contain a comma seperated list of page ids that were initially loaded.
Then on postback the linkbuttons can be recreated. I have included an
example:
#########################
Test.aspx
#########################
<%@ Page language="c#" Codebehind="Test.aspx.cs" AutoEventWireup="false"
Inherits="Testing.Test" %>
<html>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Table ID="table" Runat="server" />
<input type="hidden" ID="pageLinks" runat="server" />
</form>
</body>
</table>
#########################
Test.aspx.cs
#########################
using System;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace Testing
{
public class Test : System.Web.UI.Page
{
protected Table table;
protected HtmlInputHidden pageLinks;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
int[] ids;
if(!this.IsPostBack)
{
// Initial Load: Lookup the page id's
ids = new int[]{1,3,5,7,9};
// Need to populate the pageLinks hidden text field.
StringBuilder sb = new StringBuilder();
for(int x=0; x<ids.Length; x++)
sb.Append((x==0 ? "" : ",") + ids[x]);
pageLinks.Value = sb.ToString();
}
else
{
// PostBack: we need to parse the hidden text field.
string[] vals = pageLinks.Value.Split(',');
ids = new int[vals.Length];
for(int x=0; x<ids.Length; x++)
ids[x] = Convert.ToInt32(vals[x], 10);
}
foreach(int id in ids)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
LinkButton btn = new LinkButton();
btn.Text = "Button " + id;
btn.CommandName = id.ToString();
btn.Command += new CommandEventHandler(btn_Command);
cell.Controls.Add(btn);
row.Controls.Add(cell);
table.Rows.Add(row);
}
}
private void btn_Command(object sender, CommandEventArgs e)
{
LinkButton button = sender as LinkButton;
Response.Output.WriteLine("{0} was clicked. The command name
was {1}.", button.Text, e.CommandName);
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
Hope that helps.