Dynamic LinkButton not firing it's events.

M

Mufasa

I have code to dynamically generate some link buttons (It's not know how
many are needed until runtime.)

I am adding the linkbutton to a cell in a table and the adding works fine.
It's firing of the event doesn't seem to be happening. Am I missing
something?

Here's the code:

To create the link button:

ldrCell = new TableCell();
LinkButton lbLink = new LinkButton();
lbLink.Text = "Button - " + ldrCatRow["ProductDesc"].ToString();
lbLink.CommandName = "Desc";
lbLink.CommandArgument = "FieldValue";
lbLink.Command += new CommandEventHandler(LinkButton1_Command);
lbLink.Click += new EventHandler(LinkButton1_Click);

ldrCell.Controls.Add(lbLink);


And the code that handles the events:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
string lstemp = e.CommandArgument.ToString();
string lsmsg = lstemp + " ----";
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
string lstemp = "It was clicked.";
string lsmsg = lstemp + " --------";
}


TIA - Jeff.
 
M

Mufasa

Thanks for the help.

I was creating the objects in pre-render.

George Ter-Saakov said:
At which moment do you create your LinkButton
Check http://msdn2.microsoft.com/en-us/library/ms178472.aspx "ASP.NET Page
Life Cycle "

You should be done with it before Load event...



Mufasa said:
I have code to dynamically generate some link buttons (It's not know how
many are needed until runtime.)

I am adding the linkbutton to a cell in a table and the adding works
fine. It's firing of the event doesn't seem to be happening. Am I missing
something?

Here's the code:

To create the link button:

ldrCell = new TableCell();
LinkButton lbLink = new LinkButton();
lbLink.Text = "Button - " + ldrCatRow["ProductDesc"].ToString();
lbLink.CommandName = "Desc";
lbLink.CommandArgument = "FieldValue";
lbLink.Command += new CommandEventHandler(LinkButton1_Command);
lbLink.Click += new EventHandler(LinkButton1_Click);

ldrCell.Controls.Add(lbLink);


And the code that handles the events:
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
string lstemp = e.CommandArgument.ToString();
string lsmsg = lstemp + " ----";
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
string lstemp = "It was clicked.";
string lsmsg = lstemp + " --------";
}


TIA - Jeff.
 
Joined
Jun 15, 2009
Messages
1
Reaction score
0
add link button dynamically

but i want to add it on some button click event then what to do?
kindly reply asap.
 

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,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top