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. Link Button on User Control not firing?

Link Button on User Control not firing?

Scheduled Pinned Locked Moved ASP.NET
questiondebugging
14 Posts 3 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.
  • A Offline
    A Offline
    AndyASPVB
    wrote on last edited by
    #1

    Hi I have a link button which is part of my user control. Further, the control is actually added to the control dynamically. Whilst, I can see the control, and is added to the page without any visible errors, but when I click on to fire an event which on that user control nothing happens. What is even more odd, if I add a break point to event, and then click in debug mode, I can't even reach the break point! To try and resolve this, I have tried giving the link button ids, and ensured they unique, added override using OnInit and used the createchildcontrols method, but nothing seems to work! Has anyone came across and if so, can give me a solution?

    R 1 Reply Last reply
    0
    • A AndyASPVB

      Hi I have a link button which is part of my user control. Further, the control is actually added to the control dynamically. Whilst, I can see the control, and is added to the page without any visible errors, but when I click on to fire an event which on that user control nothing happens. What is even more odd, if I add a break point to event, and then click in debug mode, I can't even reach the break point! To try and resolve this, I have tried giving the link button ids, and ensured they unique, added override using OnInit and used the createchildcontrols method, but nothing seems to work! Has anyone came across and if so, can give me a solution?

      R Offline
      R Offline
      Rhys Jacob
      wrote on last edited by
      #2

      Where in the code is the linkbutton created dynamically? Put a breakpoint on page_load and you should see that this is hit when you click the linkbutton. It doesn't fire the event because the control is not recreated again as part of the postback. Try recreating the control in page_load if isPostBack = true. Remember to recreate your event handler here as well.

      A 1 Reply Last reply
      0
      • R Rhys Jacob

        Where in the code is the linkbutton created dynamically? Put a breakpoint on page_load and you should see that this is hit when you click the linkbutton. It doesn't fire the event because the control is not recreated again as part of the postback. Try recreating the control in page_load if isPostBack = true. Remember to recreate your event handler here as well.

        A Offline
        A Offline
        AndyASPVB
        wrote on last edited by
        #3

        Hi I had already done all of this. The linkbutton is created on the usercontrol form. I have added the linkbutton creation to, with this.linkbutton1.click += EventHandler(this.linkbutton1_click); to Page_Load, OnInit, Page_PreRender and CreateChildControls. The event will not fire even after doing all of this!

        R 1 Reply Last reply
        0
        • A AndyASPVB

          Hi I had already done all of this. The linkbutton is created on the usercontrol form. I have added the linkbutton creation to, with this.linkbutton1.click += EventHandler(this.linkbutton1_click); to Page_Load, OnInit, Page_PreRender and CreateChildControls. The event will not fire even after doing all of this!

          R Offline
          R Offline
          Rhys Jacob
          wrote on last edited by
          #4

          Could you post the code behind please?

          R A 2 Replies Last reply
          0
          • R Rhys Jacob

            Could you post the code behind please?

            R Offline
            R Offline
            Ravindra Nidhonkar
            wrote on last edited by
            #5

            As you said the Link button is created Dynamically, you have to create it Every Time when the page is post back, also create it in the Init event of the user control.

            A 1 Reply Last reply
            0
            • R Rhys Jacob

              Could you post the code behind please?

              A Offline
              A Offline
              AndyASPVB
              wrote on last edited by
              #6

              Here is the user control with the link button on it! public partial class ucCustomerDetails : System.Web.UI.UserControl { protected override void CreateChildControls( ) { base.CreateChildControls( ); this.lnkShowAllCust.ID = "lnkShowAllCust"; this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click); this.Controls.Add(lnkShowAllCust); } protected override void OnInit (EventArgs e) { CreateChildControls( ); base.OnInit(e); } protected override void OnLoad(EventArgs e) { base.EnsureChildControls( ); } protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { CreateChildControls( ); } } protected void lnkShowAllCust_Click(object sender, EventArgs e) { this.OnCustShowAllClicked(new EventArgs ( )); } protected virtual void OnCustShowAllClicked(EventArgs args) { if (this.ViewAllClicked != null) { this.ViewAllClicked(this, args); } } public event EventHandler ViewAllClicked; } I have deleted other stuff that is on it, but these other controls have nothing to do with the link button

              R R 2 Replies Last reply
              0
              • R Ravindra Nidhonkar

                As you said the Link button is created Dynamically, you have to create it Every Time when the page is post back, also create it in the Init event of the user control.

                A Offline
                A Offline
                AndyASPVB
                wrote on last edited by
                #7

                Here is the user control with the link button on it! public partial class ucCustomerDetails : System.Web.UI.UserControl { protected override void CreateChildControls( ) { base.CreateChildControls( ); this.lnkShowAllCust.ID = "lnkShowAllCust"; this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click); this.Controls.Add(lnkShowAllCust); } protected override void OnInit (EventArgs e) { CreateChildControls( ); base.OnInit(e); } protected override void OnLoad(EventArgs e) { base.EnsureChildControls( ); } protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { CreateChildControls( ); } } protected void lnkShowAllCust_Click(object sender, EventArgs e) { this.OnCustShowAllClicked(new EventArgs ( )); } protected virtual void OnCustShowAllClicked(EventArgs args) { if (this.ViewAllClicked != null) { this.ViewAllClicked(this, args); } } public event EventHandler ViewAllClicked; } I have deleted other stuff that is on it, but these other controls have nothing to do with the link button

                1 Reply Last reply
                0
                • A AndyASPVB

                  Here is the user control with the link button on it! public partial class ucCustomerDetails : System.Web.UI.UserControl { protected override void CreateChildControls( ) { base.CreateChildControls( ); this.lnkShowAllCust.ID = "lnkShowAllCust"; this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click); this.Controls.Add(lnkShowAllCust); } protected override void OnInit (EventArgs e) { CreateChildControls( ); base.OnInit(e); } protected override void OnLoad(EventArgs e) { base.EnsureChildControls( ); } protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { CreateChildControls( ); } } protected void lnkShowAllCust_Click(object sender, EventArgs e) { this.OnCustShowAllClicked(new EventArgs ( )); } protected virtual void OnCustShowAllClicked(EventArgs args) { if (this.ViewAllClicked != null) { this.ViewAllClicked(this, args); } } public event EventHandler ViewAllClicked; } I have deleted other stuff that is on it, but these other controls have nothing to do with the link button

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

                  Try following code:

                  protected override void OnInit(EventArgs e)
                  {
                  base.OnInit(e);
                  LinkButton lnk = new LinkButton();
                  lnk.Text = "My Link";
                  lnk.Click += new EventHandler(lnk_Click);
                  plcHld.Controls.Add(lnk);
                  }

                      void lnk\_Click(object sender, EventArgs e)
                      {
                          lblMessage.Text = "Link Clicked";
                      }
                  
                  A 1 Reply Last reply
                  0
                  • R Ravindra Nidhonkar

                    Try following code:

                    protected override void OnInit(EventArgs e)
                    {
                    base.OnInit(e);
                    LinkButton lnk = new LinkButton();
                    lnk.Text = "My Link";
                    lnk.Click += new EventHandler(lnk_Click);
                    plcHld.Controls.Add(lnk);
                    }

                        void lnk\_Click(object sender, EventArgs e)
                        {
                            lblMessage.Text = "Link Clicked";
                        }
                    
                    A Offline
                    A Offline
                    AndyASPVB
                    wrote on last edited by
                    #9

                    I have tried this, and still no luck. One thing I did omit to mention is that I have an AJAX control on the ASPX page that encompasses the whole user control. All this does is to refresh the page every 5 mins. I commented this AJAX control out, added a break point to the link button event, and all that happened was the java script fired, but it didn't reach the break point.

                    1 Reply Last reply
                    0
                    • A AndyASPVB

                      Here is the user control with the link button on it! public partial class ucCustomerDetails : System.Web.UI.UserControl { protected override void CreateChildControls( ) { base.CreateChildControls( ); this.lnkShowAllCust.ID = "lnkShowAllCust"; this.lnkShowAllCust.Click += new EventHandler(this.lnkShowAllCust_Click); this.Controls.Add(lnkShowAllCust); } protected override void OnInit (EventArgs e) { CreateChildControls( ); base.OnInit(e); } protected override void OnLoad(EventArgs e) { base.EnsureChildControls( ); } protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { CreateChildControls( ); } } protected void lnkShowAllCust_Click(object sender, EventArgs e) { this.OnCustShowAllClicked(new EventArgs ( )); } protected virtual void OnCustShowAllClicked(EventArgs args) { if (this.ViewAllClicked != null) { this.ViewAllClicked(this, args); } } public event EventHandler ViewAllClicked; } I have deleted other stuff that is on it, but these other controls have nothing to do with the link button

                      R Offline
                      R Offline
                      Rhys Jacob
                      wrote on last edited by
                      #10

                      This is a simple example of recreating your control on postback. So assuming that works and you still have the problem: is there any validation on the front end? Could it be that the form isn't valid so you don't get the postback?

                      public partial class WebUserControl1 : System.Web.UI.UserControl
                      {
                      static int x = 0;
                      protected void Page_Load(object sender, EventArgs e)
                      {
                      if (IsPostBack)
                      {
                      if (ViewState["x"] != null)
                      {
                      CreateLB();
                      }
                      }
                      }

                          protected void Button1\_Click(object sender, EventArgs e)
                          {
                              CreateLB();
                          }
                      
                          private void CreateLB()
                          {
                              LinkButton lb = new LinkButton();
                              lb.Text = "click me";
                              lb.Click += new EventHandler(lb\_Click);
                              this.Controls.Add(lb);
                              ViewState\["x"\] = true;
                          }
                      
                          void lb\_Click(object sender, EventArgs e)
                          {
                              x += 1;
                              Label1.Text = x.ToString();
                          }
                      }
                      

                      front end:

                      <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

                      A 1 Reply Last reply
                      0
                      • R Rhys Jacob

                        This is a simple example of recreating your control on postback. So assuming that works and you still have the problem: is there any validation on the front end? Could it be that the form isn't valid so you don't get the postback?

                        public partial class WebUserControl1 : System.Web.UI.UserControl
                        {
                        static int x = 0;
                        protected void Page_Load(object sender, EventArgs e)
                        {
                        if (IsPostBack)
                        {
                        if (ViewState["x"] != null)
                        {
                        CreateLB();
                        }
                        }
                        }

                            protected void Button1\_Click(object sender, EventArgs e)
                            {
                                CreateLB();
                            }
                        
                            private void CreateLB()
                            {
                                LinkButton lb = new LinkButton();
                                lb.Text = "click me";
                                lb.Click += new EventHandler(lb\_Click);
                                this.Controls.Add(lb);
                                ViewState\["x"\] = true;
                            }
                        
                            void lb\_Click(object sender, EventArgs e)
                            {
                                x += 1;
                                Label1.Text = x.ToString();
                            }
                        }
                        

                        front end:

                        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
                        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

                        A Offline
                        A Offline
                        AndyASPVB
                        wrote on last edited by
                        #11

                        Hi There is no form validation at all on the page. The only other thing I have is an AJAX Control, which refreshes the page every 5 mins. I will try your code, and see how I get on!

                        A 1 Reply Last reply
                        0
                        • A AndyASPVB

                          Hi There is no form validation at all on the page. The only other thing I have is an AJAX Control, which refreshes the page every 5 mins. I will try your code, and see how I get on!

                          A Offline
                          A Offline
                          AndyASPVB
                          wrote on last edited by
                          #12

                          Hi I have tried your code, and nothing happened. However, as part of my debugging and trying to get to grips with this, I added a break point to the page_load method and ran again. The page_load method didn't get hit, I even then moved the code from there to OnLoad, and it did get hit, but inside the if statement for the viewstate, it skipped over the CreateLB() method! Any thoughts?

                          R 1 Reply Last reply
                          0
                          • A AndyASPVB

                            Hi I have tried your code, and nothing happened. However, as part of my debugging and trying to get to grips with this, I added a break point to the page_load method and ran again. The page_load method didn't get hit, I even then moved the code from there to OnLoad, and it did get hit, but inside the if statement for the viewstate, it skipped over the CreateLB() method! Any thoughts?

                            R Offline
                            R Offline
                            Rhys Jacob
                            wrote on last edited by
                            #13

                            Have you wired up the button on the front page to hit right method:

                            protected void Button1_Click(object sender, EventArgs e)
                            {
                            CreateLB();
                            }

                            The idea is, you click the button on the form, this dynamically creates the linkbutton, then when you click the linkbutton it stays there and is recreated on postback every time you click it.

                            A 1 Reply Last reply
                            0
                            • R Rhys Jacob

                              Have you wired up the button on the front page to hit right method:

                              protected void Button1_Click(object sender, EventArgs e)
                              {
                              CreateLB();
                              }

                              The idea is, you click the button on the form, this dynamically creates the linkbutton, then when you click the linkbutton it stays there and is recreated on postback every time you click it.

                              A Offline
                              A Offline
                              AndyASPVB
                              wrote on last edited by
                              #14

                              Yes, I have. Since my reply to you, I have been digging a bit deeper, and my feelings at the moment are starting to sway if the button event is not being wired up correctly. Therefore, I double checked the AutoWireEvent, and this has been set to true on the usercontrol. By the way, the Page_Load is being overriden by the OnLoad method, which I need to have on my page, because I am setting up a grid view, which needs to reload on PostBack, because it is also built dynamically. What I have also done is put on a simple button on the control, and ran the ocde in debug through OnLoad. This has set the button text value, and I can see the event handler being added in. Next I clicked the button on Post Back. When the page is reload via OnLoad, which also has a if (IsPostBack) this is hit, and what I wanted to do is to reset the text value to something else. However, what happened instead was that the button disappeared off the screen. Event for that button was never reached! Am I missing a trick here?

                              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