Home All Groups Group Topic Archive Search About

Defining server-side Click event for Label web contol ?

Author
13 Apr 2005 7:43 PM
Saket Mundra
I am using a label on my webform, wherein i want that on clicking the user
must be directed to next page(just like hyperlink).

I have added the onclick attribute to label contol as follows :
Label1.attributes.add("onclick","")

Now I want that whenever the user clicks the label a server side onlick
event be fired, just as in case of buttons. How should i do that ?

Pls Help. Any help is appreciated
Thank You.

Saket Mundra

Author
14 Apr 2005 2:53 AM
Alvin Bruney [MVP - ASP.NET]
a label is historically a piece of text on a form. if you need clickability,
use a button and set its border appropriately

--
Regards,
Alvin Bruney - ASP.NET MVP

[Shameless Author Plug]
The Microsoft Office Web Components Black Book with .NET
Now available @ www.lulu.com/owc
Show quoteHide quote
"Saket Mundra" <SaketMun***@discussions.microsoft.com> wrote in message
news:F5602604-00D0-4B48-804A-3AC00D3AF154@microsoft.com...
>I am using a label on my webform, wherein i want that on clicking the user
> must be directed to next page(just like hyperlink).
>
> I have added the onclick attribute to label contol as follows :
> Label1.attributes.add("onclick","")
>
> Now I want that whenever the user clicks the label a server side onlick
> event be fired, just as in case of buttons. How should i do that ?
>
> Pls Help. Any help is appreciated
> Thank You.
>
> Saket Mundra
Author
14 Apr 2005 4:34 PM
Teemu Keiski
Maybe you could have it easier by using a LinkButton, which's style you set
to be non-underlined (if having link-like style isn't an option).

And sure, you could develop a custom control for this task also if you
insist that it should be a Label, here's an example:

using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


namespace ClassLibrary2
{
/// <summary>
/// Summary description for ClickableLabel.
/// </summary>
public class ClickableLabel : Label,IPostBackEventHandler
{
  #region "Click event"
  private static readonly object EventClick=new object();

  public event EventHandler Click
  {
   add
   {
    Events.AddHandler(EventClick,value);
   }
   remove
   {
    Events.RemoveHandler(EventClick,value);
   }
  }

  protected void OnClick(EventArgs e)
  {
   EventHandler h=Events[EventClick] as EventHandler;
   if(h != null)
    h(this,e);
  }

  #endregion

  //When postback is caused by this control, raise the event
  void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
  {
   if(eventArgument =="lbl")
    OnClick(EventArgs.Empty);
  }

  //Add onclick attribute to cause a postback when Label is clicked
  protected override void AddAttributesToRender(HtmlTextWriter writer)
  {
   base.AddAttributesToRender (writer);
   writer.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(this,"lbl"));
  }



}
}


--
Teemu Keiski
ASP.NET MVP, AspInsider
Finland, EU


..
Show quoteHide quote
"Saket Mundra" <SaketMun***@discussions.microsoft.com> wrote in message
news:F5602604-00D0-4B48-804A-3AC00D3AF154@microsoft.com...
>I am using a label on my webform, wherein i want that on clicking the user
> must be directed to next page(just like hyperlink).
>
> I have added the onclick attribute to label contol as follows :
> Label1.attributes.add("onclick","")
>
> Now I want that whenever the user clicks the label a server side onlick
> event be fired, just as in case of buttons. How should i do that ?
>
> Pls Help. Any help is appreciated
> Thank You.
>
> Saket Mundra