下拉OnSelectedIndexChanged没有发射 [英] Dropdown OnSelectedIndexChanged not firing
问题描述
的 OnSelectedIndexChanged
事件在不触发我的下拉框。我看过所有论坛告诉我要添加的AutoPostBack =真正的
,但这并没有改变结果。
The OnSelectedIndexChanged
event is not firing for my dropdown box. All forums I have looked at told me to add the AutoPostBack="true"
, but that didn't change the results.
HTML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Current Time: " /><br />
<asp:Label ID="lblCurrent" runat="server" Text="Label" /><br /><br />
<asp:DropDownList ID="cboSelectedLocation" runat="server" AutoPostBack="true" OnSelectedIndexChanged="cboSelectedLocation_SelectedIndexChanged" /><br /><br />
<asp:Label ID="lblSelectedTime" runat="server" Text="Label" />
</div>
</form>
</body>
</html>
背后code:
public partial class _Default : Page
{
string _sLocation = string.Empty;
string _sCurrentLoc = string.Empty;
TimeSpan _tsSelectedTime;
protected void Page_Load(object sender, EventArgs e)
{
AddTimeZones();
cboSelectedLocation.Focus();
lblCurrent.Text = "Currently in " + _sCurrentLoc + Environment.NewLine + DateTime.Now;
lblSelectedTime.Text = _sLocation + ":" + Environment.NewLine + DateTime.UtcNow.Add(_tsSelectedTime);
}
//adds all timezone displaynames to combobox
//defaults combo location to seoul, South Korea
//defaults current location to current location
private void AddTimeZones()
{
foreach(TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
{
string s = tz.DisplayName;
cboSelectedLocation.Items.Add(s);
if (tz.StandardName == "Korea Standard Time") cboSelectedLocation.Text = s;
if (tz.StandardName == System.TimeZone.CurrentTimeZone.StandardName) _sCurrentLoc = tz.StandardName;
}
}
//changes timezone name and time depending on what is selected in the cbobox.
protected void cboSelectedLocation_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (TimeZoneInfo tz in System.TimeZoneInfo.GetSystemTimeZones())
{
if (cboSelectedLocation.Text == tz.DisplayName)
{
_sLocation = tz.StandardName;
_tsSelectedTime = tz.GetUtcOffset(DateTime.UtcNow);
}
}
}
}
任何意见成如何寻找一个新人ASP codeR?
Any advice into what to look at for a rookie asp coder?
修改:增加了更多的$ C后面
EDIT: added more code behind
格雷厄姆·克拉克在需要的正确!Page.IsPostBack
,但现在已是事与我设置了全局变量。这code被拖行从C#项目下降了,所以我想有一些问题,全局变量和asp.net。时间让我做更多这方面的研究,以了解变量在一个独立的全球性如何不同,而不是一个Web程序。
Graham Clark was correct in needing the !Page.IsPostBack
, but it is now something with the global variables that I set. This code was dragged and dropped from a c# project, so I assume there is some issues with global variables and asp.net. Time for me to do more research on this to understand how global variables differ in a standalone as opposed to a web program.
推荐答案
您的数据绑定下拉列表中的每一趟回服务器,或者只是一个回传?如果你每次都这样做,这可能是因为服务器没有觉得什么被选中,因此该事件将不会触发。
Are you databinding your drop-down list every trip back to the server, or just on a postback? If you're doing it every time, it could be that the server doesn't think anything has been selected, therefore the event won't fire.
说你数据绑定在Page_Load事件下拉。要做到这一点是这样的:
Say you're databinding the drop-down in the Page_Load event. You want to do it like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// bind drop-down list here
}
}
这篇关于下拉OnSelectedIndexChanged没有发射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!