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. Label Click

Label Click

Scheduled Pinned Locked Moved ASP.NET
question
5 Posts 2 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.
  • V Offline
    V Offline
    VickyC
    wrote on last edited by
    #1

    I am new to asp and I want a clickable label within another control public class MyLabel : Label, IPostBackEventHandler{ public event EventHandler Click; protected virtual void onclick(EventArgs e){ if(this.Click != null){ this.Click(this, e); } } public void RaisePostBackEvent(string eventArgument){ onclick(new EventArgs()); } protected override void AddAttributesToRender(HtmlTextWriter writer) base.AddAttributesToRender (writer); writer.AddAttribute(HtmlTextWriterAttribute.onclick, Page.GetPostBackEventReference(this)); } } In my other control I have the following: protected override void Render(HtmlTextWriter output) { MyLabel ML=new MyLabel(); ML.Text = "TEST"; ML.Click += new System.EventHandler(this.ML_Click); base.Controls.Add(ML); base.Render(output); } private void ML_Click(object sender, EventArgs e) { Console.WriteLine(e.ToString()); } When I click on MyLabel in the main control it fires but it never gets into ML_Click and AddAttributesToRender but not anywhere else, what am I missing? any ideas?

    M 1 Reply Last reply
    0
    • V VickyC

      I am new to asp and I want a clickable label within another control public class MyLabel : Label, IPostBackEventHandler{ public event EventHandler Click; protected virtual void onclick(EventArgs e){ if(this.Click != null){ this.Click(this, e); } } public void RaisePostBackEvent(string eventArgument){ onclick(new EventArgs()); } protected override void AddAttributesToRender(HtmlTextWriter writer) base.AddAttributesToRender (writer); writer.AddAttribute(HtmlTextWriterAttribute.onclick, Page.GetPostBackEventReference(this)); } } In my other control I have the following: protected override void Render(HtmlTextWriter output) { MyLabel ML=new MyLabel(); ML.Text = "TEST"; ML.Click += new System.EventHandler(this.ML_Click); base.Controls.Add(ML); base.Render(output); } private void ML_Click(object sender, EventArgs e) { Console.WriteLine(e.ToString()); } When I click on MyLabel in the main control it fires but it never gets into ML_Click and AddAttributesToRender but not anywhere else, what am I missing? any ideas?

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, What you are missing is the order of the events in the control life cycle[^]. Basically, the "Render" phase occurs after the "Handle postback events", so when the Click event gets fired you haven't registered the event handler for this event (the Render method still hasn't run yet), and as a result the event handler never gets called. In this case, instead of overriding the Render method, you should override the CreateChildControls method where you'd normally use to create the child controls for a web custom control. The sample code looks something like:

      protected override void CreateChildControls()
      {
      MyLabel ML = new MyLabel();
      ML.Text = "TEST";
      ML.Click += new System.EventHandler(this.ML_Click);

           Controls.Add(ML);                 
      

      }

      For more information, you can see Developing ASP.NET Server Controls[^]

      V 2 Replies Last reply
      0
      • M minhpc_bk

        Hi there, What you are missing is the order of the events in the control life cycle[^]. Basically, the "Render" phase occurs after the "Handle postback events", so when the Click event gets fired you haven't registered the event handler for this event (the Render method still hasn't run yet), and as a result the event handler never gets called. In this case, instead of overriding the Render method, you should override the CreateChildControls method where you'd normally use to create the child controls for a web custom control. The sample code looks something like:

        protected override void CreateChildControls()
        {
        MyLabel ML = new MyLabel();
        ML.Text = "TEST";
        ML.Click += new System.EventHandler(this.ML_Click);

             Controls.Add(ML);                 
        

        }

        For more information, you can see Developing ASP.NET Server Controls[^]

        V Offline
        V Offline
        VickyC
        wrote on last edited by
        #3

        True. I made this change and still does the same thing. I still need to use the Render method. How can the event gets registered before the render.

        1 Reply Last reply
        0
        • M minhpc_bk

          Hi there, What you are missing is the order of the events in the control life cycle[^]. Basically, the "Render" phase occurs after the "Handle postback events", so when the Click event gets fired you haven't registered the event handler for this event (the Render method still hasn't run yet), and as a result the event handler never gets called. In this case, instead of overriding the Render method, you should override the CreateChildControls method where you'd normally use to create the child controls for a web custom control. The sample code looks something like:

          protected override void CreateChildControls()
          {
          MyLabel ML = new MyLabel();
          ML.Text = "TEST";
          ML.Click += new System.EventHandler(this.ML_Click);

               Controls.Add(ML);                 
          

          }

          For more information, you can see Developing ASP.NET Server Controls[^]

          V Offline
          V Offline
          VickyC
          wrote on last edited by
          #4

          May be the answer is called INamingContainer...oof. But it still fires the main control from the begining. I would think that it would just call ML_Click.

          M 1 Reply Last reply
          0
          • V VickyC

            May be the answer is called INamingContainer...oof. But it still fires the main control from the begining. I would think that it would just call ML_Click.

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            Yes, you need to make sure that your parent custom control implement the INamingContainer interface so that it can route the postback event to the its child control MyLabel. Also, you should remember that you'd nomally override the Render method when you want to control the interface of the custom control, that's not where you create a child control which needs to process a post back event. For more information, you can see Developing a Composite Control[^]

            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