2011-05-05 16 views
0

我沒有使用任何Java腳本。 我的代碼是:錯誤的時區信息的C#代碼

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> TimeZoneColl = TimeZoneInfo.GetSystemTimeZones(); 
     DropDownList2.DataSource = TimeZoneColl; 
     DropDownList2.DataBind(); 
    } 


} 




protected void Button1_Click(object sender, EventArgs e) 
{ 

     string d = DateTime.Now.ToString(); 
     string sel =DropDownList2.SelectedValue; 

     Label1.Text = d; 
     TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById("Norway Standard Time"); 

     Label1.Text = tst.ToString(); 
     //TimeZoneInfo timeinfo = TimeZoneInfo.FindSystemTimeZoneById(sel); 
     //Label3.Text =timeinfo.ToString(); 
     try 
     { 
      DateTime tstTime = TimeZoneInfo.ConvertTime(DateTime.Now, TimeZoneInfo.Local, tst); 
      Label3.Text = tstTime.ToLongTimeString(); 
     } 
     catch (Exception E) 
     { 
      Console.WriteLine("Error" + E); 
     } 
    } 

但通過ID發現getzone區選擇錯誤。 這裏的區域可以選擇格式(東京標準時間) 但我想從下拉列表中選擇它。 所以下拉列表中包含其他格式。

+0

如何將區域綁定到下拉列表?什麼'TimeZoneInfo'屬性分配給值和顯示文本? – mmix 2011-05-05 09:28:33

+0

你會得到哪些例外? – 2011-05-05 09:29:17

+0

綁定工作正常。但錯誤在TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(「挪威標準時間」);這裏在參數中我想通過下拉列表選擇它。像TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(dropdown.selecteditem); – Manish 2011-05-05 09:31:42

回答

2

您可能從組合框中檢索到錯誤的值。 TimeZoneInfo.FindSystemTimeZoneById方法需要與存儲在註冊表中的ID完全匹配。

嘗試將您的組合框綁定到TimeZoneInfo.GetSystemTimeZones返回的值。將TimeZoneInfo對象的DisplayName成員綁定到顯示成員,將ID屬性綁定到值成員。

現在,您從組合框中獲得的選定值應該是您需要的ID。

編輯:
更改Page_Load方法下面:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) 
    { 
     System.Collections.ObjectModel.ReadOnlyCollection<TimeZoneInfo> TimeZoneColl = TimeZoneInfo.GetSystemTimeZones(); 
     DropDownList2.DataSource = TimeZoneColl; 
     DropDownList2.DataTextField = "DisplayName"; 
     DropDownList2.DataValueField = "Id";  
     DropDownList2.DataBind(); 
    } 
} 

現在,你應該能夠使用SelectedValue屬性來設置你想要的時區。

+0

請你給我你正在談論的代碼? – Manish 2011-05-05 09:47:09

+0

非常感謝你:) – Manish 2011-05-05 10:19:20