Click here to Skip to main content
15,902,635 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<script language="Javascript">
function country() {
           var country = "HI";
           var hiddenControl = '<%= inpHide.ClientID %>';
           document.getElementById(hiddenControl).value = country;
       }
</script>

<body>
XML
<asp:HiddenField ID="inpHide" runat="server" />
       <asp:Label ID="lblMsg" runat="server" />

</body>


C#
protected void Page_Load(object sender, EventArgs e)
    {
        lblMsg.Text = inpHide.Value;
    }
Posted
Comments
JoCodes 20-Jan-14 3:19am    
When or on what event you are calling the Country method?

Try like this

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script language="Javascript">
        function country() {
            var country = "HI";
            var hiddenControl = '<%= inpHide.ClientID %>';
            document.getElementById(hiddenControl).value = country;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:HiddenField ID="inpHide" runat="server" />
    <asp:Label ID="lblMsg" runat="server" />
    <asp:Button ID="btn" Text="some" runat="server" OnClientClick="country();"
        onclick="btn_Click" />
    </form>
</body>
</html>



C#
protected void btn_Click(object sender, EventArgs e)
      {
          lblMsg.Text = inpHide.Value;
      }
 
Share this answer
 
Comments
info2Ram 20-Jan-14 2:06am    
Hai karthik i want to display the lable text on page load it self..I dont need on click event..
Karthik_Mahalingam 20-Jan-14 2:11am    
then wat is the use of hidden field ???
info2Ram 20-Jan-14 2:15am    
Ok please just remove the hidden field.. and i need to print the value in lable only on page load.. Please adjust the code like that.
Karthik_Mahalingam 20-Jan-14 2:27am    
then try this

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
lblMsg.Text = inpHide.Value;
}
}
info2Ram 20-Jan-14 3:54am    
Hai karthik its showing an empty value. Please check it.. and let me know the correct answer
Replace
C#
lblMsg.Text = inpHide.Value;

with
C#
lblMsg.Text = hiddenControl .Value;

Refer:http://stackoverflow.com/questions/1465732/how-to-access-js-variable-in-c-sharp[^]
This may help.
 
Share this answer
 
v2
Comments
info2Ram 20-Jan-14 1:24am    
it is getting an error.
The name 'hiddenControl' does not exist in the current context
Gitanjali Singh 20-Jan-14 1:30am    
Replace
<asp:HiddenField ID="inpHide" runat="server" />
with
<input id="hiddenControl" type="hidden" runat="server" />

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