如何获得客户端的日期和时间在ASP.NET?如何获得、客户端、日期、时间

2023-09-02 01:25:13 作者:背靠背ら聽海的聲音

我觉得这个问题说明了一切。如果我用DateTime.Now,然后我得到从服务器的角度来看的日期和时间。我听说JavaScript的解决方案,但我不知道是否有另一种解决方案。

I think the question says it all. If I use DateTime.Now, then I get the date and time from the server point of view. I've heard of JavaScript solution, but I wonder if there is another solution.

感谢

推荐答案

我会做的就是创建一个隐藏的输入字段,然后连接一个JavaScript例程的onsubmit事件的形式。这个程序将填充隐藏字段时在客户机上。

What I'd do is create a hidden input field and then wire a Javascript routine to the onsubmit event for the form. This routine would populate the hidden field with the time on the client machine.

隐藏字段可以与ASP.NET使用HTML控件HtmlInputHidden课堂上使用。你只要给你的输入控制=服务器属性,像任何其他的服务器端控制。

The hidden field can used with ASP.NET by using the HTML control "HtmlInputHidden" class. You just give you input control a runat="server" attribute like any other server side control.

该服务器可以再这个时候读出时的形式回发。你甚至可以换这在一个服务器控件,如果你需要做这在许多地方。

The server can then read out this time when the form posts back. You could even wrap this up in a server control if you need to do this in a number of places.

另外,你可以使用AJAX做到这一点,但实施将取决于你使用的库。

Alternatively, you could do this with AJAX but the implementation will depend on which library you use.