user1659510
user1659510

Reputation: 283

How to Get HiddenField value using javascript

I am new to javascript. I am going to show visitor counter of website. I have made c# code to enter the values of the counter into the database. After that I fetch count values from the database. This count value from the database is my hit count for the website. I want to show it using javascript with the asp control hidden field. But it doesn't display anything. How do I solve this problem? I have tried with following code:

//Hitcount.aspx file

<!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">
        function getTotalCount() {
            alert(document.getElementById('hdTotalCount').value());
        }
    </script>
</head>
<body onload="getTotalCount();">
    <form id="form1" runat="server">
        <div>
            <asp:HiddenField runat="server" ID="hdTotalCount" />
        </div>
    </form>
</body>
</html>

//Server Side code

if (!IsPostBack == true)
{
    //SQL Query to fetch total count
    hdTotalCount.Value = 21;  //21 is the SQL query result
}

Upvotes: 0

Views: 5687

Answers (1)

Satpal
Satpal

Reputation: 133453

You need to use Control.ClientID, it gets the control ID for HTML markup that is generated by ASP.NET.

Also value is a property not a function.

Use

 alert(document.getElementById('<%= hdTotalCount.ClientID %>').value);

Upvotes: 2

Related Questions