1

I have this javascript function:

<script type="text/javascript">
    function DisplayTime() {
        var d = new Date();
        document.getElementById('<%=Label1.ClientID %>').innerHTML = d.localeFormat("yyyy-mm-dd HH:mm:ss");
    }
</script>

I have this label:

<asp:Label ID="Label1" runat="server" Text ="labelText" ></asp:Label>

In CodeBehind I do this:

Page.ClientScript.RegisterStartupScript(GetType(), "DisplayTime", "DisplayTime();", true);
string text = Label1.Text;

The DisplayTime() function is working fine.

The Label1 text that is displayed in the browser when I run it is: 2015-21-08 17:21:36 <- this is fine.

But when I try to read the text property of Label1 from CodeBehind I get the original text value: "labelText"

Does anyone has a idea why?

Regards rubenc

ruben
  • 109
  • 2
  • 10
  • 2
    the asp Label tag uses an attribute called Text to build the innerHTML of an html label tag. Javascript is setting the innerHTML of the html label element created by asp, but does not have access to the asp objects directly. Your CodeBehind is getting the attribute Text from the asp Label. (there's a separation of server side client side logic) – user2782001 May 08 '15 at 23:44
  • You should be using a Hidden control not a label, is what user2782001 is forgotting to say. Also http://stackoverflow.com/questions/274826/how-to-get-client-date-and-time-in-asp-net – misha130 May 08 '15 at 23:49
  • Thanks to all for your replies – ruben May 09 '15 at 18:11

2 Answers2

1

Does anyone has a idea why?

Short Answer

The string text = Label1.Text; statement runs on the server-side. The DisplayTime() function runs on the client-side. Since the server-side code runs before the client-side code does, the server does not know what changes DisplayTime() is going to make. You will need to send the changes back to the server.

Further Explanation

In ASP.NET web pages, the server-side (server) and the client-side (client) communicate through HTTP. The client is usually a web browser.

  1. The client sends a request to the server.
  2. In response, ASP.NET code on the server goes through the page life cycle to generate an HTML page.
  3. The server sends that page via HTTP back to the client.
  4. The client renders the HTML and optionally changes it with JavaScript.

When your code calls RegisterStartupScript, the HTML page is still on the server (step 2). Importantly, the client hasn't invoked the DislayTime() function yet!

When your code calls string text = Label1.Text;, the HTML page is still on the server-side too. Again, the client hasn't invoked the DisplayTime() function. Result: Label1 will have its original value.

// DisplayTime() only goes live once it reaches the client.
Page.ClientScript.RegisterStartupScript(
    GetType(), "DisplayTime", "DisplayTime();", true);

// The client-side hasn't called `DisplayTime()` yet.
string text = Label1.Text;

Possible Options

If we want the server-side to be aware of the JavaScript manipulations (or any other client-side changes), then we must use HTTP to send those changes back to the server. This can be a POST or another HTTP verb. You can POST back the whole page or you can use AJAX. That's probably beyond the scope of this answer.

Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
1

I found a solution here: Retrieve Client's Today's Date using ASP.NET

ruben
  • 109
  • 2
  • 10
  • Cool. In your link, there is a point at which the JavaScript says: `// re-load the page, so that the cookie value is picked up by the server and client's time is calculated`. This of course is what sends the changes back to the server. – Shaun Luttin May 09 '15 at 22:20