Solved

Javascript / C# problems with ASP.Net Master Pages

Posted on 2009-05-18
5
419 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 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