Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a ASP Label on my page, and a hidden input field following my label.
My label has 'onclick' event attached with a javascript function which takes a single argument.
Now I want to assign/pass value of hidden field into javascript function while "Asp:Label" definition.

I'm just trying something like this:
XML
<asp:Label ID="lbl1" runat="server" Text="" Visible="true" onclick="changePage('<%=hid1201.Value%>')" meta:resourcekey="lbl1Resource1"></asp:Label>
                                                    <asp:HiddenField  runat="server" ID="hid1201" />


I'm unable to get the result, can anybody help me out.

Thanks in advance.

Sri.
Posted

As you already noticed the asp:Label does not have "onClick" client side event.
So the best way to handle this is to use JQuery, which has already become the standard JavaScript framework for many companies including Microsoft.


Try this code, it works.

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!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></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="lbl1" runat="server" Text="My Label Text"></asp:Label>
    <asp:HiddenField runat="server" ID="hid1201" Value="My Hidden Value" />
    </form>

    <script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $("#<%=lbl1.ClientID %>").click(function () {
                alert($("#<%=hid1201.ClientID %>").val());
            });
        });
</script>
</body>
</html>


Good luck.
 
Share this answer
 
Hi Sri,
Please try the below code.

XML
<asp:HiddenField  runat="server" ID="hid1201"/>
  <asp:Label ID="lbl1" runat="server" Text="" Visible="true" onclick="changePage('hid1201')" meta:resourcekey="lbl1Resource1">Label</asp:Label>



XML
<script language="javascript" type="text/javascript">
      function changePage(hiddenFieldID) {
          alert(document.getElementById(hiddenFieldID).value);
      }
  </script

>
 
Share this answer
 
Comments
Brij 15-Nov-10 7:21am    
it will be better if access the client id as <%= hid1201.ClientID %>
I think your page will be a content page which is using masterpage.
XML
<asp:Label ID="lbl1" runat="server" Text="" Visible="true" onclick="changePage('<%= hid1201.ClientID %>')"></asp:Label>
<asp:HiddenField  runat="server" ID="hid1201" />

XML
<script language="javascript" type="text/javascript">
      function changePage(PageID) 
      {
          alert(document.getElementById(PageID).value);
      }
</script>

Careful with IDs & take a look at this post[^]
 
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