473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 4592
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dotnetified | last post by:
Reposting after about 2 weeks of no response ... thanks if you can help... ---------------------------------------------------------------------------- -------------- To anyone who thinks they...
3
by: Paul Hodgson | last post by:
I have quite a few pages that have multiple command buttons on them - for example one button might be to logout, and others might be to display different segments of the data being shown. The...
0
by: Keithb | last post by:
My application has a LinkButton that I am using as a Command button in the ItemTemplate of a GridView control. Regardless of the value set for the Enabled property (true or false), The control is...
0
by: David Lozzi | last post by:
Howdy, I have a gridview with a linkbutton in a template column (code is below). When the linkbutton is clicked i want to display a little confirm right in the gridview to confirm deleting the...
1
by: ganesh22 | last post by:
Hi, Iam using Gridview in Asp.net, for that i added one linkbutton using template field, for linkbutton iam added command argument now i want when i click that linkbutton i want that linkbutton...
0
by: Michael | last post by:
I know I am missing something simple, but I am stuck. When I submit the following for the employee info gets stored in scale1 and scale1 shows the employee number. Can someone show me my...
0
by: bgreer5050 | last post by:
I know I am missing something simple, but I am stuck. When I submit the following for the employee info gets stored in scale1 and scale1 shows the employee number. Can someone show me my...
0
by: Clodoaldo | last post by:
The insert command in the sqlDataSource is ignored. The OnInserted event is executed. As the command was ignored i issued a wrong insert command ("x") just to see if it would throw an execption...
8
by: Hamayun Khan | last post by:
Hi I have created linkbuttons dynamically using the below code Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32) Dim i =...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.