在页面加载时进行数据绑定期间,DropDownList 不会触发 Selected Index Changed 事件
我有一个下拉列表(System.Web.UI.WebControls.DropDownList),我在页面加载期间绑定它。
try
{
if (!Page.IsPostBack)
{
DataTable dt = new StaticNoticeController().GetStaticNoticeNames();
this.HeadingsDropDownList.AutoPostBack = true;
this.HeadingsDropDownList.DataSource = new StaticNoticeController().GetStaticNoticeNames();
this.HeadingsDropDownList.DataTextField = "NoticeName";
this.HeadingsDropDownList.DataValueField = "NoticeId";
this.HeadingsDropDownList.DataBind();
}
}
catch (SystemException ex)
{
//ToDo: Log the Exception
}
我希望当我将下拉列表绑定到组合框(在 Winforms 中的组合框的情况下经常发生)时触发 SelectedIndex 事件,以便页面加载与下拉列表中的初始值相对应的值。
我什至尝试过
这个。HeadingsDropDownList.selectedIndex = -1;
在数据绑定后的页面加载中。但徒劳无功。
当我通过浏览器更改下拉列表的索引时,该事件会像往常一样被触发。 我是否错过了什么,或者我是否试图执行一些不可能的事情?
请帮忙。 提前致谢
I have a Dropdownlist (System.Web.UI.WebControls.DropDownList) which I Bind during the pageload.
try
{
if (!Page.IsPostBack)
{
DataTable dt = new StaticNoticeController().GetStaticNoticeNames();
this.HeadingsDropDownList.AutoPostBack = true;
this.HeadingsDropDownList.DataSource = new StaticNoticeController().GetStaticNoticeNames();
this.HeadingsDropDownList.DataTextField = "NoticeName";
this.HeadingsDropDownList.DataValueField = "NoticeId";
this.HeadingsDropDownList.DataBind();
}
}
catch (SystemException ex)
{
//ToDo: Log the Exception
}
I expect the SelectedIndex event to get fired when I bind the dropdown to a combobox ( which use to happen in case of Comboboxes in Winforms ) so that the page is loaded with values corresponding to the initial value in the dropdown list.
I even tried
this.HeadingsDropDownList.selectedIndex = -1;
in pageload after databind. But in vain .
When I change the index of the dropdown through the browser , the event gets fired as usual.
Am I missing something or am I trying to perform something which is not possible ?
Please help.
Thanks in Advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这本质上意味着当您加载站点时它会直接回发。它会陷入循环。
如果它是一个初始化值,那么回发将执行的操作应该在 init 中完成。
It would essentially mean that when you load the site it would directly postback. It would be stuck in a loop.
If its an inited value, then what the postback would do should be done in the init.
为什么要触发 SelectedIndexChanged 事件?正如您已经提到的,如果用户在客户端手动更改所选索引,该索引将会更改。这是期望的行为。如果您最初想在服务器端调用一个函数,当选定的索引更改时也会调用该函数,请手动调用它!
Why do you want to fire the SelectedIndexChanged-Event?? As you have already mentioned, the selected index will change if the user changes it manually on client-side. This is the desired behaviour. If you initially want to call a function on serverside that would be also called when the selected index changes, call it manually!