如何将 ASP.NET 数据绑定 DropDownList 中的 TimeZoneInfo 设置为用户的时区?
我有一个由 GoDaddy 托管的网络应用程序(因此网络服务器位于亚利桑那州 - 山地时间)。我的用户大部分位于中部时区,但也可能有一些来自其他时区。
我有一个使用 TimeZoneInfo 的带有数据绑定 dropDownList 的网页,我想将此 dropDownList 的选定值设置为用户所在的任何时区。这是我目前的代码:
protected void Page_Load(object sender, EventArgs e)
{
DropDownListTimeZone.DataSource = TimeZoneInfo.GetSystemTimeZones();
DropDownListTimeZone.DataBind();
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DropDownListTimeZone.SelectedValue = cst.DisplayName;
}
没关系,但我希望它对于那些用户来说更智能不在 CST 中。我希望有某种方法可以从 Page.Request 之类的东西中获取用户的 TimeZoneInfo。?但我不明白。
有一个简单的解决方案吗?
I have a web app hosted with GoDaddy (so the web server is in Arizona - Mountain Time). My users are mostly in Central Time Zone, but I could have some from other time zones.
I have a web page with a databound dropDownList using TimeZoneInfo, and I want to set the selected value of this dropDownList to whatever timeZone the user is in. Here's my code currently:
protected void Page_Load(object sender, EventArgs e)
{
DropDownListTimeZone.DataSource = TimeZoneInfo.GetSystemTimeZones();
DropDownListTimeZone.DataBind();
TimeZoneInfo cst = TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time");
DropDownListTimeZone.SelectedValue = cst.DisplayName;
}
It's ok, but I want it to be smarter for those users who aren't in CST. I was hoping there is some way to grab the user's TimeZoneInfo from something like Page.Request.?? but I can't figure it out.
Is there an easy solution to this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
但没有。您可以使用 JavaScript 从客户端猜测 - 请参阅 这个问题——但这只是一个猜测。
没有界面可以从浏览器获取明确的客户端时区信息。因此,您肯定会希望提供用户选择的设置,并使用 JS 猜测作为默认值。
There is not. You can guess from the client side using JavaScript—see this question—but it is just a guess.
There's no interface to get definitive client timezone information from a browser. So you'll definitely want to provide the user-selected setting, using the JS guess as a default value.
我认为这需要一些 JavaScript;由于它在用户的浏览器上运行,因此它可以选择计算机设置的任何时区,并相应地设置下拉列表中的所选项目。
http://www.onlineaspect。 com/2007/06/08/auto-detect-a-time-zone-with-javascript/
I think this would require a bit of javascript; since it's running on the user's browser, it could pick up whatever time zone the computer is set to and set the selected item in the dropdown accordingly.
http://www.onlineaspect.com/2007/06/08/auto-detect-a-time-zone-with-javascript/