473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 24329
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

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

Similar topics

3
by: Jim Mitchell | last post by:
I have some code behind that generates 10 imagebutton controls.... I can not seem to figure out how to trap the onclick event for each image and determine which image was clicked. Can someone...
3
by: dave | last post by:
I have created a control that inherits from datagrid. Before I render the grid html I want to create a linkbutton. I am using the following code to do so Dim lnk As New LinkButto lnk.Text =...
3
by: Alice Lee | last post by:
Hi, My web from has one button and by clicking this button a list of linkbuttons must be dynamically displayed based on information in database. Then click any one of these linkbuttons another...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
2
by: Sheryl Landon | last post by:
Hi all - I'm creating a link button at runtime, and putting in a table cell, and then trying to hook up the click event handler... but it's not working. Here's the code, any idea what might be...
1
by: tshad | last post by:
Is there a way to change the onClick event of a LinkButton? At the moment, I am using 2 buttons that I toggle the visibility on and off. I would like to use just one button, if possible and just...
1
by: fabrice | last post by:
Hello, I d like to modify the Onclik Event of an ImageButton control in code behind. But when i do it , i get an error. This is ma code : The control is the pasx page :
0
by: felixch | last post by:
Hi all, I tried to add an OnClick event to a LinkButton by using Attribute.sAdd, which resides in a Repeater and the Repeater is bind with a DataTable with around 2000 records. If I try to run...
2
by: =?Utf-8?B?Uml0YUc=?= | last post by:
Hello. On a form how can I programtically (jscript) click a LinkButton which will force the onClick event for that LinkButton to fire? TIA, Rita
3
by: ICPooreMan | last post by:
The following is a very simple example of what I want to do take an elements oncontextmenu and changing it dynamically onclick of that same element. The code below will fail unless you change the...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.