469,898 Members | 1,611 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,898 developers. It's quick & easy.

Defining a Command for a LinkButton

Vi
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
Nov 18 '05 #1
10 4453
LinkButton.Command requires a CommandEventHandler. Try chaning that line to:
PageLink.Command += new CommandEventHandler(PageLink_Click);

Hope that helps.

--
Jason Whitted

"Vi" wrote:
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

Nov 18 '05 #2
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

Nov 18 '05 #3
Vi
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

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

Hope that helps.

--
Jason Whitted

"Vi" wrote:
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

Nov 18 '05 #4
Vi
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


Nov 18 '05 #5
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);
}

"Vi" wrote:
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

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

Hope that helps.

--
Jason Whitted

"Vi" wrote:
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

Nov 18 '05 #6
Vi
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.
"JWhitted" wrote:
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);
}

"Vi" wrote:
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

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

Hope that helps.

--
Jason Whitted

"Vi" wrote:

> 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

Nov 18 '05 #7
Vi
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.
"JWhitted" wrote:
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);
}

"Vi" wrote:
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

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

Hope that helps.

--
Jason Whitted

"Vi" wrote:

> 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

Nov 18 '05 #8
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.

--
Jason Whitted
Nov 18 '05 #9
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


Nov 18 '05 #10
Vi
It worked, you really saved me.
Thanks a lot, J
"JWhitted" wrote:
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.

--
Jason Whitted

Nov 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Paul Hodgson | last post: by
reply views Thread by David Lozzi | last post: by
reply views Thread by Michael | last post: by
reply views Thread by bgreer5050 | last post: by
reply views Thread by Clodoaldo | last post: by
1 post views Thread by Waqarahmed | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.