Click here to Skip to main content
15,907,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi dear my Friends
i want to set Onclick for This label:
ASP.NET
<asp:Label ID="Label1" runat="server" style="cursor:pointer;" CommandName="test" CommandArgument=<%# Eval("Id") %>  >Test</asp:Label>


i want when User Click Label Event Execute and in CodeBehind Label1_Click(object sender , Event e){} Execute

tanks very Much
Posted
Comments
AmitGajjar 27-Aug-12 4:27am    
what you want to do in label click event ?
Seyed Ahmad Mirzaee 27-Aug-12 4:33am    
i want check CommandArgument
beacuse i use this Label in TemplateFiled in GridView
AmitGajjar 27-Aug-12 4:34am    
but what this label will do ? can't you use LinkButton instead of label and design it to look like label.
Seyed Ahmad Mirzaee 27-Aug-12 4:47am    
Thanks
prashant patil 4987 27-Aug-12 4:49am    
hey seyed ahmad Mirzaee, why you not use Linkbutton instead of Label. On LnkButton U will get click event easily and link button works like Lable also..

hi try like this


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

<!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>Untitled Page</title>
    <script src="js/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
    function hi()
    {
        __doPostBack('<%=Button1.ClientID%>', '');
    }

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    <asp:UpdatePanel runat="server">
    <ContentTemplate>
        <asp:Label ID="Label3" runat="server" style="cursor:pointer;" onclick="hi();">Test</asp:Label>
        </ContentTemplate>
        </asp:UpdatePanel>
        <asp:Button ID="Button1" style="display:none"  runat="server" Text="Button"
            onclick="Button1_Click" />
    </div>
    </form>
</body>
</html>

add this in Code behind file:

C#
protected void Button1_Click(object sender, EventArgs e)
    {
        Label3.Text = "Cliked";
    }




regards
sarva
 
Share this answer
 
v3
on Page_Init event set this property
C#
sampleLabel.Attributes["onClick"] = "ShowMessage();";


JavaScript

XML
<script type="text/javascript">
       function ShowMessage() {
           __doPostBack("sampleLabel", "I am changed")
       }
   </script>


On page_load event of page check the source of post back
C#
if (IsPostBack)
            {
                if (Request["__EVENTTARGET"] == "sampleLabel")
                {
                    sampleLabel.Text = Request["__EVENTARGUMENT"];
                }
            }


The __doPostBack method usually generate by asp.net framework.
 
Share this answer
 
Hi,
Why don't you try this with link button? Although we are having many alternatives, but you are using label. Using Label is not a good idea. Try other alternatives and use css class to make it look like label.

--Amit
 
Share this answer
 
<asp:label id="Label1" runat="server" onclick="Label1_click" style="cursor:pointer;" commandname="test" commandargument="<%#" xmlns:asp="#unknown"> >Test


and server side
protected void Label1_click(object sender,eventsargs e)
{

}

Try this, It works in link and anchor...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900