Javascript / C# problems with ASP.Net Master Pages

I'm hoping someone can PLEASE help me. I am going completely crazy with this one. I need to get the client side date so of course am using Javascript to get the date and store it in an Input field. I need to store this date in a database, so I am wanting to pass the value of this hidden field to a ASP:Label, and then parse it to a datetime value.
So in my page load I have registered the Javacsript and that all works fine. Then on my button click I can get the result and assign it to the asp control no problems. All works fine.
My drama is that when I go to use this in my project which uses Master pages it all turns to crap.
I've been over it and over it and while I realise its something to do with the Master Page I can't get it to work. I've put the code in my ASPX page that is using the MasterPage, and I've tried it in the MasterPage and I've tried it in both. My code for the .aspx pages that work fine is below. If someone can give me any idea of how this will work in a Master page setup I will be EXTREMELY grateful.
HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JSTest.aspx.cs" Inherits="JSTest" %>
<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="lblText" runat="server"></asp:Label>
        <input type="hidden" id="hdClient" runat="server" />
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </div>
    </form>
</body>
</html>
 
 
ASPX Code Behind:
protected void Page_Load(object sender, EventArgs e)
    {
        ClientScript.RegisterClientScriptBlock(this.GetType(), "1", "<script type='text/javascript'> function doDateCalcs(){var now = new Date();" +
            "var offset = now.getTimezoneOffset(); document.forms[0].hdClient.value = -offset} </script>");
 
        ClientScriptManager manager = Page.ClientScript;
        manager.RegisterStartupScript(this.GetType(), "SetDate", "doDateCalcs();", true);        
    }
 
    protected void Button1_Click(object sender, EventArgs e)
    {
        lblText.Text = DateTime.Now.ToUniversalTime().AddMinutes(double.Parse(Request.Form["hdClient"])).ToString();
        DateTime clientsDate = DateTime.Parse(lblText.Text);
    }

Open in new window

Jen_M66Asked:
Who is Participating?
 
Jen_M66Connect With a Mentor Author Commented:
I finally got it working by putting it on the master page and referenceing it from the aspx page. No matter what I did I could not get it to work from an apsx page. The other interesting thing is that I found I could assign the date value from Javascript to an asp Textbox, but not a label?? If anyone can explain that too me I would appreciate it. Also discovered that the text box has to be visible.
0
 
crazymanCommented:
Change to

ClientScript.RegisterClientScriptBlock(this.GetType(), "1", "<script type='text/javascript'> function doDateCalcs(){var now = new Date();var offset = now.getTimezoneOffset(); $get('" + this.hdClient.ClientID + "').value= -offset} </script>");
0
 
crazymanCommented:
May need to be this depending on version..

Change to

ClientScript.RegisterClientScriptBlock(this.GetType(), "1", "<script type='text/javascript'> function doDateCalcs(){var now = new Date();var offset = now.getTimezoneOffset(); document.getElementById('" + this.hdClient.ClientID + "').value= -offset} </script>");
0
 
Jen_M66Author Commented:
Thank you I will try that. I'm guessing that the "this.hdClient" is telling it to reference the control on this page so its not so generic?
0
 
Jen_M66Author Commented:
Nope doesn't solve the problem.
From what I can see, if I use the code on an aspx page that isnt' using a master page, all is good. But that was the case with my original code too.
When I look at the output, on the aspx page (not using master page) the script block is placed at the end of the <form> as you would expect using the RegisterStartupScript. BUT on the aspx page that uses the master page the script is placed at the begining of the form before the controls. Even if I change the "ClientScript.RegisterClientScriptBlock" to ClientsScript.RegisterStartupScript - the hdClient is still empty when I go to reference it!!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.