Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Friendz,

I am just stuck up with a small problem. I want to retrieve the client side time of the user. I have written following code for it.But some how its not showing the time.

C#
function DisplayDateTime()
    {
        var DateTime = new Date();
        var strYear= DateTime.getYear();
        var strMonth= DateTime.getMonth() +1;
        var strDay = DateTime.getDate();
        var strHours = DateTime.getHours();
        var strMinutes = DateTime.getMinutes();
        var strSeconds = DateTime.getSeconds();
        var dateTime=document.getElementById("lblDateTime");
        dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
    }


I have called the function on the click of the button.

VB
<asp:Button ID="btnHitMe" runat="server" Text="Hit Me!!" OnClientClick="DisplayDateTime()"OnClick="btnHitMe_Click" /
>

Can somebody tell me where am I going wrong??:confused:

[moved from answer]:
Code:
XML
<%@ Page Language="C#" MasterPageFile="~/masterpage1.master" AutoEventWireup="true"
    CodeFile="pgLogin.aspx.cs" Inherits="pgLogin" Title="Login Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="middleContent" runat="Server">
    <script type="text/javascript" src="script/md5-paj.js"></script>
    <script language="javascript" type="text/javascript">
    function EncryptMD5()
    {
         var Pass = document.getElementById("<%=txtPassword.ClientID%>").value;
            // Encrypte the password
             if (Pass.length > 0 )
             {
                var MD5Hash = hex_md5(Pass);
                var salt = document.getElementById("<%=SaltStr.ClientID%>").value;
                MD5Hash = hex_md5(salt+MD5Hash);
                document.getElementById("<%=HashPass.ClientID%>").value = MD5Hash;
                document.getElementById("<%=txtPassword.ClientID%>").value = MD5Hash;
             }
    }
    function DisplayDateTime()
    {
        var DateTime = new Date();
        var strYear= DateTime.getYear();
        var strMonth= DateTime.getMonth() +1;
        var strDay = DateTime.getDate();
        var strHours = DateTime.getHours();
        var strMinutes = DateTime.getMinutes();
        var strSeconds = DateTime.getSeconds();
        var dateTime=document.getElementById("lblDateTime");
        dateTime.innerHTML = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
    }
    </script>
    <table border="0" cellpadding="0" cellspacing="0" class="tblalign" style="width: 100%;
        height: 300px" title="LMS Login Page">
        <tr>
            <td align="center" colspan="5" rowspan="1">
                <hr style="width: 100%; color: #0099ff; height: 2px" />
            </td>
        </tr>
        <tr>
            <td align="center" colspan="4" rowspan="1" style="height: 13px; background-color: #dedede">
            </td>
            <td align="center" colspan="4" rowspan="1" style="height: 13px; background-color: #dedede">
            </td>
        </tr>
        <tr>
            <td align="center" colspan="4" valign="top">
                <table id="tblLogin" bgcolor="#f3f3f3" border="0" cellpadding="0" cellspacing="0"
                    style="border-right: gray 2px solid; border-top: gray 2px solid; border-left: gray 2px solid;
                    width: 300px; border-bottom: gray 2px solid" title="LMS Login Page">
                    <tbody>
                        <tr>
                            <td align="middle" class="Panelmedblack" colspan="1" rowspan="5">
                                <img src="images/Transport%20Vehical%201.jpg" />
                            </td>
                            <td align="middle" class="Panelmedblack" colspan="4" style="height: 20px">
                                Login</td>
                        </tr>
                        <tr>
                            <td class="medblack" colspan="4" height="5">
                                <asp:Label ID="lblDateTime" runat="server"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td class="medblack" style="height: 20px" width="2%">
                            </td>
                            <td align="center" class="Arial11" style="font-size: 9pt; font-family: Arial; height: 20px"
                                width="35%">
                                <strong><span style="color: #003399">Username:</span></strong></td>
                            <td align="left" class="medblack" style="width: 170px; height: 20px">
                                <asp:TextBox ID="txtLoginName" runat="server" CssClass="TextBoxClass" TabIndex="1"
                                    ToolTip="Enter user name" Width="145px">
                                </asp:TextBox></td>
                            <td class="medblack" style="height: 20px" width="2%">
                                <asp:RequiredFieldValidator ID="rfvLoginName" runat="server" ControlToValidate="txtLoginName"
                                    Font-Bold="True" ValidationGroup="vg" Width="120px">Enter user name</asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td class="medblack" colspan="4" height="5">
                            </td>
                        </tr>
                        <tr>
                            <td class="medblack" style="height: 24px">
                            </td>
                            <td class="Arial11" style="font-size: 9pt; font-family: Arial; height: 24px">
                                <div ms_positioning="FlowLayout" style="display: inline; width: 7px; height: 15px">
                                </div>
                                <strong><span style="color: #333399">Password:</span></strong></td>
                            <td align="left" class="medblack" style="width: 170px; height: 24px">
                                <asp:TextBox ID="txtPassword" runat="server" CssClass="TextBoxClass" EnableTheming="True"
                                    EnableViewState="False" TabIndex="2" TextMode="Password" ToolTip="Enter password"
                                    Width="145px">
                                </asp:TextBox></td>
                            <td class="medblack" style="height: 24px">
                                <asp:RequiredFieldValidator ID="rfvPassword" runat="server" ControlToValidate="txtPassword"
                                    ErrorMessage="RequiredFieldValidator" Font-Bold="True" ValidationGroup="vg" Width="120px">Enter password</asp:RequiredFieldValidator></td>
                        </tr>
                        <tr>
                            <td class="medblack" height="20">
                            </td>
                            <td class="medblack" height="20">
                            </td>
                            <td class="medblack">
                            </td>
                            <td class="medblack" style="width: 170px">
                                <asp:Button ID="cmdLogin" runat="server" CssClass="ButtonClass" Font-Bold="True"
                                    Font-Names="Verdana" Font-Size="Small" Height="23px" OnClick="cmdLogin_Click"
                                    OnClientClick="EncryptMD5()" TabIndex="4" Text="Login" ValidationGroup="vg" Width="53px"
                                    style="cursor: hand" ToolTip="user Login " />
                                <asp:Button ID="cmdCancel" runat="server" CssClass="ButtonClass" Font-Bold="True"
                                    Font-Names="Verdana" Font-Size="Small" Height="23px" OnClick="cmdCancel_Click"
                                    TabIndex="5" Text="Cancel" Width="61px" ToolTip="Cancel login" /></td>
                            <td class="medblack" style="text-align: left">
                            </td>
                        </tr>
                        <tr>
                            <td class="medblack" colspan="5" rowspan="2">
                                <asp:Label ID="lblErrMsg" runat="server" ForeColor="Red" Text="Invalid Username or Password"
                                    Visible="False"></asp:Label>
                                <asp:LinkButton id="LinkButton2" runat="server" Font-Size="Small" ForeColor="Blue"
                                    PostBackUrl="~/pgChangePwd.aspx">Change Password</asp:LinkButton>
                                <hr color="gray" size="1" width="100%" />
                                <asp:HiddenField ID="SaltStr" runat="server" Visible="True" />
                                <asp:HiddenField ID="HashPass" runat="server" Visible="True" />
                                <asp:Label ID="lblMessage" runat="server" Font-Bold="True" ForeColor="Red" Height="17px"
                                    Width="339px"></asp:Label>
                            </td>
                        </tr>
                        <tr>
                            <td align="center">
                            </td>
                            <td>
                                <asp:Button ID="btnHitMe" runat="server" Text="Hit Me!!" OnClientClick="return DisplayDateTime()" OnClick="btnHitMe_Click" />
                            </td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
        <tr>
            <td align="right" colspan="5" rowspan="1" style="height: 29px">
                <hr style="width: 100%; color: #0099ff; height: 2px" />
            </td>
        </tr>
    </table>
</asp:Content>



Thanking You,

Swapnil
Posted
Updated 23-Sep-10 0:52am
v2

Ok I got your problem,

Make sure that you Have lable control on the page with ID lblDateTime ( I think this will be. )

But The Actual Problem Here is First Your Java script event fires and you are getting Right values ( you can varify it by putting debugger; to the first line of your JS Function ) to set to your label, but then page Fires server Side event and Page is Getting rendered again and Displays by default Value Applied to Lable.

[Addition to Solution]

Ok As per you Told me that you only want to fire client Side Javascript.

Swapnil 2009 wrote:
OnClientClick="DisplayDateTime()"


Instead Write
C#
OnClientClick = "return DisplayDateTime()"


Swapnil 2009 wrote:
function DisplayDateTime()
{
var DateTime = new Date();
var strYear= DateTime.getYear();
var strMonth= DateTime.getMonth() +1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();
var dateTime=document.getElementById("lblDateTime");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
}



Instead Write ( Just Appended return false to the end of function )

C#
function DisplayDateTime() {
            var DateTime = new Date();
            var strYear = DateTime.getYear();
            var strMonth = DateTime.getMonth() + 1;
            var strDay = DateTime.getDate();
            var strHours = DateTime.getHours();
            var strMinutes = DateTime.getMinutes();
            var strSeconds = DateTime.getSeconds();
            var dateTime = document.getElementById("lblDateTime");
            dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;
            return false;
       }




Vote and Mark as a Answer if it helped.
 
Share this answer
 
v4
Comments
Hiren solanki 23-Sep-10 4:30am    
tell me you want to fire both clientside and serverside event with buttonclick or only client side. ?
Swapnil 2009 23-Sep-10 5:06am    
No I want to fire only client side event. I have not written any other code on server side.
Hiren solanki 23-Sep-10 5:14am    
see my modified answer. It will solve your problem
Swapnil 2009 23-Sep-10 5:21am    
Sorry yaar..That didn't solve the problem either..may be some other solution.
Hiren solanki 23-Sep-10 5:24am    
it is running in my pc that way. check if any other minor problem you may be having there.
Can you post your ALL code of page?
 
Share this answer
 
Comments
Swapnil 2009 23-Sep-10 6:30am    
I have posted the code..Can u plz tell me where I am going wrong??:(
MalikRizwan 23-Sep-10 8:22am    
Buddy, i mean paste your code here so i can see what are you doing wrong
Swapnil 2009 23-Sep-10 8:27am    
thnks dude I solved that problem..but can u tell me how can I call two javascript function on one click?

Calling one function inside other doesnt seem to work.



function EncryptMD5()
{
var Pass = document.getElementById("<%=txtPassword.ClientID%>").value;

// Encrypte the password
if (Pass.length > 0 )
{
var MD5Hash = hex_md5(Pass);
var salt = document.getElementById("<%=SaltStr.ClientID%>").value;
MD5Hash = hex_md5(salt+MD5Hash);
document.getElementById("<%=HashPass.ClientID%>").value = MD5Hash;
document.getElementById("<%=txtPassword.ClientID%>").value = MD5Hash;
}
DisplayDateTime()
}

function DisplayDateTime()
{
var DateTime = new Date();
var strYear= DateTime.getYear();
var strMonth= DateTime.getMonth() +1;
var strDay = DateTime.getDate();
var strHours = DateTime.getHours();
var strMinutes = DateTime.getMinutes();
var strSeconds = DateTime.getSeconds();

var dateTime=document.getElementById("hiddenfield1");
dateTime.innerText = strDay + "/" + strMonth + "/" + strYear + " " + strHours + ":" + strMinutes + ":" + strSeconds;

}
MalikRizwan 24-Sep-10 7:50am    
you are missing semicolon in on last line of first function

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