Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
4.67/5 (3 votes)
See more:
Hi,

I have gridview control with dynamically added templatefield using ITemplate interface and InstantiateIn() method which is as follows.
C#
public class MyTemplate:ITemplate
   {
       #region ITemplate Members
       public void InstantiateIn(Control container)
       {
           //EventHandlerClass MyEventClass = new EventHandlerClass();
           LinkButton link = new LinkButton();
           link.ID="lnk";
           link.CommandName="Update";
           link.EnableViewState = true;
           link.Text="Update";
           link.ToolTip="Click to Update Details";
           //link.Click+=new DelegateClickEvent(MyEventClass.Show);

           container.Controls.Add(link);
       }
       #endregion
   }

I am successfully able to add itemtemplate link button to gridview but I am stuck to hadle Events for linkbutton(ItemTemplate) column. Even unable to fire GridView event GridView_RowCommand() i.e after clicking link button event doesn't get fired.

Please help me on the same.

Thank you in advance.
Posted
Updated 27-Apr-11 2:27am
v2
Comments
jim lahey 27-Apr-11 9:37am    
Of course your event won't fire, you've commented out the line that assigns your event handler
R. Giskard Reventlov 27-Apr-11 10:14am    
:-)
shashank1487 28-Apr-11 1:52am    
Well jim even on removing the comment the event does not fire
jim lahey 28-Apr-11 4:18am    
my point is that if you want people to help you, you have to help them by providing a correct example
YOGESH DHAGE 28-Apr-11 7:31am    
Had you debug code before event occur.I think it will help you what exactly happen,before you come across the event.

Hello Shashank,

you need to bubble up the click event to its naming container i.e. GridView:
public class MyTemplateField : Control, ITemplate
    {
        Button btnCommand;
        public string CommandName { get; set; }
        public MyTemplateField()
        {
            btnCommand = new Button();
            btnCommand.ID = "btnInTemplateField";
            btnCommand.Text = "Template Field's button";
            btnCommand.Click += new EventHandler(btn_Click);
            CommandName = "Update";
        }
        void btn_Click(object sender, EventArgs e)
        {
            //Create command event arguments
            CommandEventArgs commandEventArgs = new CommandEventArgs(this.CommandName, btnCommand);
            //Bubble the command event to the container of MyTemplateField i.e. GridView
            RaiseBubbleEvent(btnCommand, commandEventArgs);
        }
        public void InstantiateIn(Control container)
        {
            container.Controls.Add(btnCommand);
        }
    }


Hope this will help.

Thanks,
Hemant
 
Share this answer
 
v2
@Hemant-Sharma is wright. ASP.Net doesn't retain dynamically created controls once there are created. Unless you call for each post back on Page_Load() or somewhere else. For example
protected void Page_Load(object sender, EventArgs e)
{
    LinkButton lb = new LinkButton();
    lb.Text = "Click me"
    lb.Click += new EventHandler(clickMe);
    this.Panel1.Controls.Add(lb);
}
private void clickMe(object sender, EventArgs e)
{

}


The link button will raise it's event whenever click, which eventually calls a post back. Without doing this it won't retain it's event.

I hope this will give you a clue.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900