I am dynamically creating links button and attaching events to it. It is working fine when I am invoking it from page_load or page_init event. but when I am calling it from a button click event it is not working. could any body solve my problem. thanks. my code is following public partial class Default4 : System.Web.UI.Page { SqlConnection conn = new SqlConnection("Data Source=COMPUTER6\\SQLEXPRESS;Initial Catalog=test_SRS;Integrated Security=True"); string ConnnectionString = "Data Source=COMPUTER6\\SQLEXPRESS;Initial Catalog=test_SRS;Integrated Security=True"; protected void Page_Load(object sender, EventArgs e) { test2(); } protected void test2() { DataSet d = new DataSet(); LinkButton[] links = new LinkButton[5]; d = binddata(); int r = d.Tables["House_hold"].Rows.Count; for (int i = 0; i < r; i++) { links = new LinkButton(); links.ID = "link" + i; links.Text = d.Tables["House_hold"].Rows[6].ToString() + "<br /><br />"; links.Click += new System.EventHandler(Lnkbut_Click); Panel1.Controls.Add(links); } } protected DataSet fetchdata(string a) { DataSet ds = new DataSet(); if (conn.State == ConnectionState.Open) conn.Close(); string connectionString = "Data Source=COMPUTER6\\SQLEXPRESS;Initial Catalog=test_SRS;Integrated Security=True"; conn.ConnectionString = ConnnectionString; conn.Open(); // Create a data adapter String s = "SELECT * FROM HH_Member where HH_ID='" + a.Substring(0,2) + "'"; //where State='" + DropDownList1.Text.Trim() + "' and SUB_EB='" + DropDownList5.Text.Trim() + "'"; SqlDataAdapter da = new SqlDataAdapter(s, conn); da.Fill(ds, "HH_Member"); return ds; } void Lnkbut_Click(object sender, EventArgs e) { DataSet d2 = new DataSet(); Label1.Text = "hello"; Response.Write(e.ToString()); Response.Write("hello"); LinkButton b = (LinkButton)sender; string s = b.Text; Label1.Text = s; d2=fetchdata(b.Text); GridView1.DataSource = d2.Tables["HH_Member"].DefaultView; GridView1.DataBind(); } protected DataSet binddata() { if (conn.State == ConnectionState.Open) conn.Close(); ConnnectionString = "Data Source=(local);Initial Catalog=SRS;Integrated Security=True"; conn.ConnectionString = ConnnectionString; conn.Open(); // Create a data adapter SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM House_hold", conn); // Create DataSet, fill it and view in data grid DataSet ds = new DataSet(); da.Fill(ds, "House_hold"); return ds; }
protected void Page_Init(object sender, EventArgs e) { this.AddControl(); } protected void Page_Load(object sender, EventArgs e) { } private void AddControl() { LinkButton links = new LinkButton(); links.ID = "lnk1"; links.Text = "ClickMe"; links.ClientIDMode = System.Web.UI.ClientIDMode.AutoID; links.Click += new EventHandler(links_Click); this.Panel1.Controls.Add(links); } void links_Click(object sender, EventArgs e) { Response.Write("hello"); } Code (markup): Try that code. However, If I were you, I would replace that with a repeater, easier to maintain. Cheers,
Sir, I have seen your code in which you have called addcontrol method from page_init event. but I want to dynamically create control from button_click or some other event. Because its based on table. I am searching in the table then based on that filter I want to show matching record, and number of records shown will depend on some parameter. So I want to say I can not use page_init event. event should be called after accepting parameter. So will you please help me.
I am told to use link button. and after accepting certain parameter I have to show data from table based on those parameter. so I can not use page_load and page_init event.
Repeater is a template based control to render collection of data. Use a repeater, put your link button into repeater and load the data from your table into repeater in your onclick event.