I'm not following the other branch of this conversation, but where are you
initially doing your for loop?
for(i=1;i<=someVariable;i++){
LinkButton PageLink = new LinkButton();
PageLink.CommandName = i.ToString();
PageLink.Text = "Page " + i.ToString();
PageLink.ID = "pageLink" + i.ToString(); //added by me
PageLink.Command += new CommandEventHandler(PageLink_Click);
Page.Controls.Add(PageLink) //added by me
}
Anyways, you need to do the same for loop when the page is posted back.
For example, doing this should work:
void page_load{
for(i=1;i<=someVariable;i++){
LinkButton PageLink = new LinkButton();
PageLink.CommandName = i.ToString();
PageLink.Text = "Page " + i.ToString();
PageLink.ID = "pageLink" + i.ToString(); //added by me
PageLink.Command += new CommandEventHandler(PageLink_Click);
Page.Controls.Add(PageLink) //added by me
}
}
but doing this won't:
void page_load{
if (!Page.IsPostBack){
for(i=1;i<=someVariable;i++){
LinkButton PageLink = new LinkButton();
PageLink.CommandName = i.ToString();
PageLink.Text = "Page " + i.ToString();
PageLink.ID = "pageLink" + i.ToString(); //added by me
PageLink.Command += new CommandEventHandler(PageLink_Click);
Page.Controls.Add(PageLink) //added by me
}
}
}
because the controls aren't created on postback
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Vi" <Vi@discussions.microsoft.com> wrote in message
news:17**********************************@microsof t.com...
I do the controls to the form. But I'm not sure how to recreate them on
postback, since I'm creating them dinamically (they're not defined in the
aspx file) and if I try:
If(IsPostBack)
{
PageLink.Command += new CommandEventHandler(PageLink_Click);
}
it fails because PageLink is not defined (Error: "Object reference not set
to an instance of an object")
I'm really stock here.
Thanks
"Karl Seguin" wrote:
Try:
PageLink.Command += new CommandEventHandler(PageLink_Click);
Also, I assume you only showed up part of the code, but you are adding
this control to the page somehow..and finally, you need to make sure to
recreate these on postback in order for the event handler to get hooked up and
thus fire.
Karl
--
MY ASP.Net tutorials
http://www.openmymind.net/
"Vi" <Vi@discussions.microsoft.com> wrote in message
news:1D**********************************@microsof t.com... Hi,
I'm trying to dinamically add LinkButton Controls to a web form. I do
something like:
for(i=1;i<=someVariable;i++)
{
LinkButton PageLink = new LinkButton();
PageLink.CommandName = i.ToString();
PageLink.Text = "Page " + i;
PageLink.Command += PageLink_Click;
}
The problem is with the last lane. What I'm trying to do there is to
specify what methon in the code behind needs to called when the LinkButton is
clicked. But it generates an error at compilation.
Please Help,
Thank you