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:

iPhone Launch Screen Sizes

iPhone Portrait iOS 8 Retina HT 5.5 = 1242 X 2208 Retna HD 4.7 = 750 X 1134 iPhone Landscape iOS 8 Retina HD 5.5  2208 X 1242 iPho...