Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Click Event of dynamically created Link Button is not working

Click Event of dynamically created Link Button is not working

Scheduled Pinned Locked Moved ASP.NET
help
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ravindra Bisen
    wrote on last edited by
    #1

    Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((

    S N V R 4 Replies Last reply
    0
    • R Ravindra Bisen

      Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((

      S Offline
      S Offline
      Sandeep Mewara
      wrote on last edited by
      #2

      Did you re-bind the events at postback? You would need to.

      R 1 Reply Last reply
      0
      • S Sandeep Mewara

        Did you re-bind the events at postback? You would need to.

        R Offline
        R Offline
        Ravindra Bisen
        wrote on last edited by
        #3

        I donk know how to rebind the event please tell me ..

        S 1 Reply Last reply
        0
        • R Ravindra Bisen

          I donk know how to rebind the event please tell me ..

          S Offline
          S Offline
          Sandeep Mewara
          wrote on last edited by
          #4

          Let the same code run on each postback! You must be skipping the formation of the button and event assignment on consecutive postbacks.

          1 Reply Last reply
          0
          • R Ravindra Bisen

            Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((

            N Offline
            N Offline
            Nishant Singh
            wrote on last edited by
            #5

            On Pageload event repeat the above code

            1 Reply Last reply
            0
            • R Ravindra Bisen

              Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((

              V Offline
              V Offline
              vishnukamath
              wrote on last edited by
              #6

              write this code in protected void Page_PreInit(object sender, EventArgs e) { LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not } private void ctrl_Click(object sender, EventArgs e) { } dont write in protected void Page_Load(object sender, EventArgs e) { }

              R 1 Reply Last reply
              0
              • R Ravindra Bisen

                Hi All, I created a link button dynamically. using following code LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not working Please Help me... Note : i tried a lot using google help befor posting this thread. :((

                R Offline
                R Offline
                raju melveetilpurayil
                wrote on last edited by
                #7

                Check this, it will help you.

                <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicButton.aspx.cs" Inherits="dynamicButton" %>

                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                <html xmlns="http://www.w3.org/1999/xhtml">
                <head runat="server">
                <title></title>
                </head>
                <body>
                <form id="form1" runat="server">
                <div>
                <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                </div>
                </form>
                <script >
                function jsEvent() {
                alert("hai");
                }
                </script>
                </body>
                </html>

                using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Web;
                using System.Web.UI;
                using System.Web.UI.WebControls;

                public partial class dynamicButton : System.Web.UI.Page
                {
                protected void Page_Load(object sender, EventArgs e)
                {
                Button button = new Button();
                button.ID = "Button1";
                button.Text = "DynaicButton";
                button.Attributes.Add("onclick", "return jsEvent();");
                button.Click += new EventHandler(button_Click);
                PlaceHolder1.Controls.Add(button);
                }
                protected void button_Click(object sender, EventArgs e)
                {
                Response.Write("Server Event: From Dynamic Button");
                }
                }

                Raju.M

                R 1 Reply Last reply
                0
                • R raju melveetilpurayil

                  Check this, it will help you.

                  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="dynamicButton.aspx.cs" Inherits="dynamicButton" %>

                  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

                  <html xmlns="http://www.w3.org/1999/xhtml">
                  <head runat="server">
                  <title></title>
                  </head>
                  <body>
                  <form id="form1" runat="server">
                  <div>
                  <asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
                  </div>
                  </form>
                  <script >
                  function jsEvent() {
                  alert("hai");
                  }
                  </script>
                  </body>
                  </html>

                  using System;
                  using System.Collections.Generic;
                  using System.Linq;
                  using System.Web;
                  using System.Web.UI;
                  using System.Web.UI.WebControls;

                  public partial class dynamicButton : System.Web.UI.Page
                  {
                  protected void Page_Load(object sender, EventArgs e)
                  {
                  Button button = new Button();
                  button.ID = "Button1";
                  button.Text = "DynaicButton";
                  button.Attributes.Add("onclick", "return jsEvent();");
                  button.Click += new EventHandler(button_Click);
                  PlaceHolder1.Controls.Add(button);
                  }
                  protected void button_Click(object sender, EventArgs e)
                  {
                  Response.Write("Server Event: From Dynamic Button");
                  }
                  }

                  Raju.M

                  R Offline
                  R Offline
                  Ravindra Bisen
                  wrote on last edited by
                  #8

                  thanks for Helping me. i pasted the code in Page load and it is working fine. but page load call this code on each event call. So is it good things to call and fatch record from database for creating table/grid? again and again for same page?

                  R 1 Reply Last reply
                  0
                  • V vishnukamath

                    write this code in protected void Page_PreInit(object sender, EventArgs e) { LinkButton lbt = new LinkButton(); lbt.Text = objDt.Rows[j]["sub_grd"].ToString(); lbt.Click += new EventHandler(ctrl_Click); // not working //then i used lbt.Attributes.Add("OnClick", "ctrl_Click();"); // not } private void ctrl_Click(object sender, EventArgs e) { } dont write in protected void Page_Load(object sender, EventArgs e) { }

                    R Offline
                    R Offline
                    Ravindra Bisen
                    wrote on last edited by
                    #9

                    Hi Visnu, Thanks for helping me. It does not change the behaviour of link button. Page_PreInit(object sender, EventArgs e) calls the functions to creats the button programatically but when i click the link button then the dynamic table gets disapeared. for the solution of problem i wrote the code in the Page_load event and got the results. But my question, Is it good thing to call same function again and again and get data from database to create table dynamic for same page?

                    1 Reply Last reply
                    0
                    • R Ravindra Bisen

                      thanks for Helping me. i pasted the code in Page load and it is working fine. but page load call this code on each event call. So is it good things to call and fatch record from database for creating table/grid? again and again for same page?

                      R Offline
                      R Offline
                      raju melveetilpurayil
                      wrote on last edited by
                      #10

                      for Dynamically created controls its good.its for avoid disappearing control in PlaceHolder after postback.

                      Raju.M

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups