dynamically add linkbutton

H

Hu Dong

HI, I got some difficulties in adding linkbuttons through C# coding. I added
a panel as follows:
<asp:panel id="LblNav" Runat="server" Visible="True"
Height="4Px"></asp:panel>
then I created a method to generate a few links and inserted into this
panel;
void RenderNav (int CurrentPage, int TotalPage)
LinkButton bn;
for (int i = 1; i <= TotalPage; i++)
{
bn = new LinkButton();
bn.ID = "link" + i.ToString();
bn.Text = i.ToString();
bn.CausesValidation = false;
bn.Command += new CommandEventHandler(NavigationLink_Click);
bn.CommandName = "Numbered";
bn.CommandArgument = i.ToString();
LblNav.Controls.Add(bn);
LblNav.Controls.Add(new LiteralControl(" "));
}
}

the event handler is:
void NavigationLink_Click ( Object sender, CommandEventArgs e ) {
switch ( e.CommandName ) {
case "First": _currentPageNumber = 1; break;
case "Last": _currentPageNumber = Int32.Parse ( TotalPages.Text ); break;
case "Next": _currentPageNumber = Int32.Parse ( CurrentPage.Text ) + 1;
break;
case "Prev": _currentPageNumber = Int32.Parse ( CurrentPage.Text ) - 1;
break;
case "Numbered": _currentPageNumber =
Int32.Parse(e.CommandArgument.ToString()); break; }
BindData();
}

What I expected was, when I clicked the numbered link button, I should jump
to that page. However, it did not work. And the worse thing was, the
linkbutton list had gone when I clicked either of the buttons. There are
four more static linkbuttons in the page, while they all work very
beautiful. I added a break point in the NavigationLink_Click(), but I even
could not get in when I clicked the numbered linkbutton while I got in if I
clicked other four buttons (First, last, prev, and next).

Do you have any idea how to fix this problem? I am struggling with it for
two days...

Thanks!!!

HU
 
N

Natty Gur

Hi,

Are you calling RenderNav from Page_load ?

You should create dynamic control each and every time the page is load
(including post back).


Natty Gur[MVP]

blog : http://weblogs.asp.net/ngur
Mobile: +972-(0)58-888377
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,226
Latest member
KristanTal

Latest Threads

Top