Defining a Command for a LinkButton

G

Guest

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
 
G

Guest

LinkButton.Command requires a CommandEventHandler. Try chaning that line to:
PageLink.Command += new CommandEventHandler(PageLink_Click);

Hope that helps.
 
K

Karl Seguin

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
 
G

Guest

I did that, but when I'm clicking the LinkButton, the form gets posted back,
but the handler method PageLink_Click is not called. Any idea why?

Thanks again
 
G

Guest

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
 
G

Guest

At what point are the LinkButtons getting added to the page's control
collection?

Seams like the the for loop should look something like:
for(int i=1; i<someVariable; i++)
{
LinkButton PageLink = new LinkButton();
PageLink.CommandName = i.ToString();
PageLink.Text = "Page " + i;
PageLink.Command += new CommandEventHandler(PageLink_Click);
this.Controls.Add(PageLink);
}
 
G

Guest

I have a Button that retrieves all the data from the DB and builds this
LinkButtons as well based on how much data was retrieved.

for(int i=1;i<=iPageCount;i++)
{
oCell = new HtmlTableCell();
LinkButton PageLink = new LinkButton();
PageLink.CommandName = i.ToString();
PageLink.Text = "Page " + i;
PageLink.Command += new CommandEventHandler(PageLink_Click);
PageLink.CausesValidation = false;
oCell.Controls.Add(PageLink);

oTR.Cells.Add(oCell);
}
oTBL.Rows.Add(oTR);

The data is retrieved and the list of pages looks fine.
When I click on the page link, the form is posted back, but the method
PageLink_Click is not called (only Page_Load runs).
Karl Seguin also replied to this post and he suggested that I have " to make
sure to recreate these on postback in order for the event handler to get
hooked up and thus fire" - I guess this is what I'm missing, but I don't know
how to do it, since all the LinkButtons are generated dinamically (are not
defined in the aspx) and therefore
If(IsPostBack)
{
PageLink.Command += new CommandEventHandler(PageLink_Click);
}
generates the error "Object reference not set to an instance of an object".
The same thing happens if I try to put this code in InitializeComponent().
Any idea how to solve this problem?

Thanks a lot.
 
G

Guest

I have a Button that retrieves all the data from the DB and builds this
LinkButtons as well based on how much data was retrieved.

for(int i=1;i<=iPageCount;i++)
{
oCell = new HtmlTableCell();
LinkButton PageLink = new LinkButton();
PageLink.CommandName = i.ToString();
PageLink.Text = "Page " + i;
PageLink.Command += new CommandEventHandler(PageLink_Click);
PageLink.CausesValidation = false;
oCell.Controls.Add(PageLink);

oTR.Cells.Add(oCell);
}
oTBL.Rows.Add(oTR);

The data is retrieved and the list of pages looks fine.
When I click on the page link, the form is posted back, but the method
PageLink_Click is not called (only Page_Load runs).
Karl Seguin also replied to this post and he suggested that I have " to make
sure to recreate these on postback in order for the event handler to get
hooked up and thus fire" - I guess this is what I'm missing, but I don't know
how to do it, since all the LinkButtons are generated dinamically (are not
defined in the aspx) and therefore
If(IsPostBack)
{
PageLink.Command += new CommandEventHandler(PageLink_Click);
}
generates the error "Object reference not set to an instance of an object".
The same thing happens if I try to put this code in InitializeComponent().
Any idea how to solve this problem?

Thanks a lot.
 
G

Guest

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.
 
K

Karl Seguin

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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top