468,146 Members | 1,470 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,146 developers. It's quick & easy.

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...
Expand|Select|Wrap|Line Numbers
  1. LinkButton lnkbtnDel = new LinkButton();
  2. lnkbtnDel.Text = "Delete";
  3. lnkbtnDel.ID = "lnkDel" + i.ToString();
  4. lnkbtnDel.Attributes.Add("runat", "server");
  5. lnkbtnDel.Click += new EventHandler(Dynamic_Click);
  6. lnkbtnDel.OnClientClick = "confirm('Are you sure you want to delete this permanently');";
  7. lnkbtnDel.CommandName = "DeleteReq";
  8. lnkbtnDel.Font.Size = 7;
  9. lnkbtnDel.Attributes.Add("style", "position:relative;bottom:3px;padding-bottom:3px;color:red;");
Expand|Select|Wrap|Line Numbers
  1. protected void Dynamic_Click(object sender, EventArgs e)
  2. {
  3.       Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "OK", "<script language='javascript'>alert('Expected');</script>");
  4. }
In the above code OnClientClick attribute method is working fine and Click EventHandler is not working
Jun 11 '13 #1
2 19800
Frinavale
9,735 Expert Mod 8TB
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
Jun 11 '13 #2
Sherin
77 64KB
Try This Code

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Web.UI.WebControls;
  3. using System.Data;
  4.  
  5. namespace WebFormDemo
  6. {
  7.     public partial class DynamicControlInGridView : System.Web.UI.Page
  8.     {
  9.         protected void Page_Load(object sender, EventArgs e) {
  10.             if (!IsPostBack)
  11.                 BindGridView();
  12.         }
  13.  
  14.         protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) {
  15.             if (e.Row.RowType == DataControlRowType.DataRow) {
  16.  
  17.                 LinkButton lb = new LinkButton();
  18.                 lb.ID = "LinkButton1";
  19.                 lb.Text = "Click Me!";
  20.                 lb.Click += OnLinkClick;
  21.  
  22.                 PlaceHolder p = (PlaceHolder)e.Row.FindControl("PlaceHolder1");
  23.                 p.Controls.Add(lb);
  24.  
  25.             }
  26.         }
  27.  
  28.         protected void OnLinkClick(object sender, EventArgs e) {
  29.             LinkButton lb = (LinkButton)sender;
  30.             GridViewRow row = (GridViewRow)lb.NamingContainer;
  31.             if (row != null) {
  32.                 Response.Write("Found it!");
  33.             }
  34.         }
  35.  
  36.         private void BindGridView() {
  37.             GridView1.DataSource = CreateDataSource();
  38.             GridView1.DataBind();
  39.         }
  40.  
  41.         public DataTable CreateDataSource() {
  42.             DataTable dt = new DataTable();
  43.             DataRow dr;
  44.  
  45.             dt.Columns.Add(new DataColumn("ID", typeof(string)));
  46.             dt.Columns.Add(new DataColumn("Name", typeof(string)));
  47.             dt.Columns.Add(new DataColumn("Lastname", typeof(string)));
  48.  
  49.             dr = dt.NewRow();
  50.             //add values to each columns
  51.             dr["ID"] = 1;
  52.             dr["Name"] = "Vincent";
  53.             dr["LastName"] = "Durano";
  54.             dt.Rows.Add(dr);
  55.             return dt;
  56.         }
  57.     }
  58. }
Dec 16 '20 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Alice Lee | last post: by
reply views Thread by Diane Yocom | last post: by
2 posts views Thread by Sheryl Landon | last post: by
1 post views Thread by tshad | last post: by
2 posts views Thread by =?Utf-8?B?Uml0YUc=?= | last post: by
3 posts views Thread by ICPooreMan | last post: by
30 posts views Thread by didacticone | last post: by
1 post views Thread by gcdp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.