DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Adding a Click Event to a Label

  1. #1
    Join Date
    Feb 2004
    Posts
    111

    Adding a Click Event to a Label

    I have a situation where I'm displaying data in a datagrid. After all of the data is displayed ot the User they will be given the option of "clicking" on individual cells to see more data relating to the data displayed in the cell.

    My problem is that individual data grid cells don't have a "click" event. And, I'm not about to try and extend the datagrid to give them a click event. I'm thinking that that would take a little more time than I have and of course I don't want to steel Microsoft's Thunder by building a better grid.

    ;-)

    Anyway, what I thought to do was to extende a simple label control by giving it a click event and then loading this modified label into those cells where the User can click for more information.

    Unfortunately, my first attempt (implementing the IPostBackEventHandler interface) didn't work - at all. So, I hoping that someone here can point me in the right direction to extending (enhancing) a label control with a click event.

    Many thanks!

    Laurence -

  2. #2
    Join Date
    Mar 2005
    Posts
    71
    A label control with click event? Why not a button?

  3. #3
    Join Date
    Feb 2004
    Posts
    111
    A fair question. The reason I was thinking to enhance a label control is that it's look and feel and be configured so that, when displayed, it's indistinquishable from a table cell where as a button has a very distinct look and feel that I'm not sure can be overridden using a style sheet.

    Laurence -

  4. #4
    Join Date
    Mar 2005
    Posts
    71
    Change the 'border' and 'background-color' style of a button can make it totally look like a label.

    But if you really want a clickable label, just add the 'onclick' attribute to the label and assign the corresponding client-side script function to the event.

    For example

    Client code
    Code:
    <form id="Form1" ...
    ...
    <input id="ReturnValue" type="hidden"/>
    <asp:Label ... onclick="LabelPostBack('Hello')">return Hello</Lable>
    ...
    <script language="JavaScript">
    function LabelPostBack(value)
    {
    	document.getElementById("ReturnValue").value = value;
    	document.getElementById("Form1").submit();
    }
    </script>
    Server code
    Code:
    private void Page_Load(...
    {
    	if(Request.Form["ReturnValue"]!= null && Request.Form["ReturnValue"].Length>0)
    	{
    		Response.Write(Request.Form["ReturnValue"]);
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center