Monday, June 21, 2010

How to find Request date and time on a Postback - Asp.net

This article demonstrate how to find date and time of the client using javascript and hidden field. For the first page load it does not know the date and time but after that it hold the date and time of the client in the hidden field

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <script runat="server">
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (this.IsPostBack)
            {
                string strRequestTime = this.hdnTime.Value;
                DateTime requestTime = DateTime.Parse(strRequestTime);
                this.Response.Write("Request's Clents Time: " + requestTime.ToString());
            }
        }        
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox runat="server" ID="txtPromoCode" />
        <asp:Button runat="server" ID="btnPromotin" Text="Apply Promotion" />
        <asp:HiddenField runat="server" ID="hdnTime" />
    </div>
    </form>
    <script language="javascript" type="text/javascript">
        var currentTime = new Date()
        var month = currentTime.getMonth() + 1;
        var day = currentTime.getDate();
        var year = currentTime.getFullYear();
        var hour = currentTime.getHours();
        var minute = currentTime.getMinutes();
        var second = currentTime.getSeconds();
        var hdnTime = document.getElementById('<%= this.hdnTime.ClientID %>');
        hdnTime.value = day + "/" + month + "/" + year + " " + hour + ":" + minute + ":" + second;
    </script>
</body>
</html>
 

No comments:

Azure Storage Account Types

Defferent Types of Blobs Block blobs store text and binary data. Block blobs are made up of blocks of data that can be managed individually...