Solved

Javascript / C# problems with ASP.Net Master Pages

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

25 Experts available now in Live!

Get 1:1 Help Now