error in c# code of time zone info
i am not using any java script. my code is:
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);
}
}
but there is an error in selection of zone in getzone found by id. here the zone can be selected in format of(tokyo standard time) but i want to select it from drop down list. so the drop down list contains the Other format.
You are probably retrieving the wrong value from your combo box. The TimeZoneInfo.FindSystemTimeZoneById method needs an exact match to the ID stored in the registry.
Try to bind your combobox to the values that TimeZoneInfo.GetSystemTimeZones returns. Bind the DisplayName member of the TimeZoneInfo object to the display member and the ID property to the value member.
Now the selected value you get from the combobox should be the ID that you need.
Edit:
Change your Page_Load method to the following:
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();
}
}
Now you should be able to use the SelectedValue property to set the time zone you want.
精彩评论