Solved

Javascript / C# problems with ASP.Net Master Pages

Posted on 2009-05-18
5
414 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:Jen_M66
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:crazyman
ID: 24411440
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
 
LVL 13

Expert Comment

by:crazyman
ID: 24411445
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
 

Author Comment

by:Jen_M66
ID: 24417846
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
 

Author Comment

by:Jen_M66
ID: 24427665
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
 

Accepted Solution

by:
Jen_M66 earned 0 total points
ID: 24455396
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

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now