Add an onclick event to a dynamically created linkbutton

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RameshKumar
    New Member
    • Jul 2011
    • 7

    Add an onclick event to a dynamically created linkbutton

    Hi Friends,

    I have a small query. I am working on ASP.NET with C#. I am trying to implement a dynamic link on my web page using C# code and from that link OnClick event i need to call a Click EventHandler method. My below Code is not working. Help me... Thanks in advance...
    Code:
    LinkButton lnkbtnDel = new LinkButton();
    lnkbtnDel.Text = "Delete";
    lnkbtnDel.ID = "lnkDel" + i.ToString();
    lnkbtnDel.Attributes.Add("runat", "server");
    [B]lnkbtnDel.Click += new EventHandler(Dynamic_Click);[/B]
    lnkbtnDel.OnClientClick = "confirm('Are you sure you want to delete this permanently');";
    lnkbtnDel.CommandName = "DeleteReq";
    lnkbtnDel.Font.Size = 7;
    lnkbtnDel.Attributes.Add("style", "position:relative;bottom:3px;padding-bottom:3px;color:red;");
    Code:
    protected void Dynamic_Click(object sender, EventArgs e)
    {
          Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "OK", "<script language='javascript'>alert('Expected');</script>");
    }
    In the above code OnClientClick attribute method is working fine and Click EventHandler is not working
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    When creating dynamic controls you have to take into consideration the life cycle of asp.net pages.

    You have to create your control in the Page_Init event (before the page load event) so that the events can get created for your control....

    If the events aren't created, then you can't handle them.

    Check out this bytes article about how to use dynamic controls in asp.net.

    -Frinny

    Comment

    • Sherin
      New Member
      • Jan 2020
      • 77

      #3
      Try This Code

      Code:
      using System;
      using System.Web.UI.WebControls;
      using System.Data;
      
      namespace WebFormDemo
      {
          public partial class DynamicControlInGridView : System.Web.UI.Page
          {
              protected void Page_Load(object sender, EventArgs e) {
                  if (!IsPostBack)
                      BindGridView();
              }
      
              protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
                  if (e.Row.RowType == DataControlRowType.DataRow) {
      
                      LinkButton lb = new LinkButton();
                      lb.ID = "LinkButton1";
                      lb.Text = "Click Me!";
                      lb.Click += OnLinkClick;
      
                      PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
                      p.Controls.Add(lb);
      
                  }
              }
      
              protected void OnLinkClick(object sender, EventArgs e) {
                  LinkButton lb = (LinkButton)sender;
                  GridViewRow row = (GridViewRow)lb.NamingContainer;
                  if (row != null) {
                      Response.Write("Found it!");
                  }
              }
      
              private void BindGridView() {
                  GridView1.DataSource = CreateDataSource();
                  GridView1.DataBind();
              }
      
              public DataTable CreateDataSource() {
                  DataTable dt = new DataTable();
                  DataRow dr;
      
                  dt.Columns.Add(new DataColumn("ID", typeof(string)));
                  dt.Columns.Add(new DataColumn("Name", typeof(string)));
                  dt.Columns.Add(new DataColumn("Lastname", typeof(string)));
      
                  dr = dt.NewRow();
                  //add values to each columns
                  dr["ID"] = 1;
                  dr["Name"] = "Vincent";
                  dr["LastName"] = "Durano";
                  dt.Rows.Add(dr);
                  return dt;
              }
          }
      }

      Comment

      Working...