Problem with Event handlers for Dynamic created Controls in DatGri

G

Guest

I have a link button "lButton" created dynamically at the item databound
event of the data grid "datagrid1" in the footer.so i want to execute the
item command event of the datagrid on clicking the dynamicaly created link
button, but when at runtime i click the dymanic created control it disapears
from the datagrid footer and then it doesnt fire the itemcommand event of the
datagrid.

I am pasting below the pieces of my code for your better understanding.


//creating the dynamic control "lButton" at the footer in the ITEM DATABOUND
EVENT and assigning th events to it.
private void datagrid1_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Footer)
{
LinkButton lButton=new
LinkButton();
lButton.Text ="Add Details
here";
lButton.ID ="flagbutton";
lButton.CommandName="flagButton";
lButton.Click +=new System.EventHandler(this.lButton_Click);
e.Item.Cells[2].Controls.Add(lButton) ;

}
}



//in the ITEM COMMAND EVENT, executing the a logic when teh link button in
the grid gets clciked
private void datagrid1_ItemCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(String.Compare(e.CommandName,"flagButton",true)==0)
{
Response.Write("done");
}
}


//Event TO BE EXECUTED BY DYNAMIC CREATED CONTROL lButton
private void lButton_Click(object sender, System.EventArgs e)
{
DataGridCommandEventArgs dgcea = (DataGridCommandEventArgs)e;
datagrid1_ItemCommand(sender, dgcea);
}



so could anyone of you help me out in fixing the problem . and the reson for
this type of cause.

Your expert advice in sorting out thsi issue woudl be highly appreciated.

Rgds
Shiju
 
G

garethdjames

You need to populate the datagrid on postback so that the button is
recreated,

If the button is not recreated then the event will not be wired up
 
B

billmiami2

A few things come to mind:

*Why do you need to dynamically add the linkbutton to the datagrid?
Why can't you place the linkbutton into the datagrid at design time and
then simply set its visibility based on the situtation? Handling
events for controls added at runtime can be tricky as you need to pay
close attention to the sequence of page and control events.

*Is there anywhere in your code or tags where you specify that the
onItemCommand event for the datagrid will be handled by your procedure
datagrid1_ItemCommand?

*Is the viewstate on your datagrid enabled?

*If you intend to trap and process the click event for your linkbutton
in the datagrid's ItemCommand event, why are you assigning a different
event handler (lButton_Click) to handle it?

*At what point in the page are you binding data to the datagrid and how
often? Are you rebinding with each postback?

Bill E.
Hollywood, FL
 

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,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top