ASP.NET 中 DropDownList 的 SelectedIndexChanged 以错误的顺序触发
我的代码中有以下 DropDownList 以错误的顺序触发:
public class MyWebpart : WebPart
{
private DropDownList dropDown = new DropDownList();
private string selectedValue;
public Webpart()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dropDown.AutoPostBack = true;
dropDown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnsureChildControls();
}
protected void DropDown_SelectedIndexChanged(Object sender, EventArgs e)
{
selectedValue - dropDown.SelectedValue;
}
protected void override void CreateChildControls()
{
base.CreateChildControls();
// create some stuff here
}
我期望当下拉选择更改时, DropDown_SelectedIndexChanged 将首先被调用,但它经历了从 OnInit、OnLoad、CreateChildControls 的整个生命周期,然后是 DropDown_SelectedIndexChanged。
我错过了什么吗?如何首先调用 DropDown_SelectedIndexChanged ?
I've got the following DropDownList in my code that is firing in the wrong order:
public class MyWebpart : WebPart
{
private DropDownList dropDown = new DropDownList();
private string selectedValue;
public Webpart()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
dropDown.AutoPostBack = true;
dropDown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.EnsureChildControls();
}
protected void DropDown_SelectedIndexChanged(Object sender, EventArgs e)
{
selectedValue - dropDown.SelectedValue;
}
protected void override void CreateChildControls()
{
base.CreateChildControls();
// create some stuff here
}
I was expecting when the drop down selection changes, the DropDown_SelectedIndexChanged will get called first, but instead it went through the entire lifecycle going from OnInit, OnLoad, CreateChildControls, then DropDown_SelectedIndexChanged.
Am I missing something? How can I get DropDown_SelectedIndexChanged call first?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您无法更改页面生命周期。您可以做的是检查是否 Page.IsPostBack 并仅在第一次加载时执行适当的操作,或者您可以创建一个 Web 服务并从 javascript 调用该 Web 服务以在 js 中执行您的 selectedindexchanged 操作,而不是发回整个页面。
祝你好运!
You can't change the page lifecycle. What you can do is either check if Page.IsPostBack and do something appropriate only on first load OR you can create a webservice and call that webservice from javascript to execute your selectedindexchanged actions in js rather than posting back the whole page.
Good luck!
这是你将调用的方法
This is you will call method
dropDown.AutoPostBack = true;
属性会触发回发,从而使其经历页面生命周期事件。最好的办法是至少检查 OnLoad 方法,以过滤掉您不希望在回发时发生的事件。
您可以编写一些 Javascript 函数并将其应用于下拉列表的“onchange”事件。但是您需要关闭自动回发,这将发生在客户端而不是服务器上。
The
dropDown.AutoPostBack = true;
property triggers a postback which causes it to go through the page lifecycle events.Your best bet is to put
if(!IsPostBack) { }
check in at least the OnLoad method to filter out events that you didn't want happening on the postback.You could write some Javascript function and apply it to the 'onchange' event of the dropdownlist. But you'll need to turn off autopostback and it would happen on the client-side rather than on server.
无法从 CreateChildControls() 访问回发值;您必须等到
OnLoad
事件发生,才能将回发的私有视图状态加载到控件中。在
OnInt
事件中调用EnsureChildControls()
;然后,回发的值可在OnLoad
事件的控件中使用 - 注意:EnsureChildControls()
调用CreateChildControls()
。Posted-back values are not accessible from
CreateChildControls()
; you must wait until theOnLoad
event for the posted back private view state to be loaded into controls.In the
OnInt
event callEnsureChildControls()
; the posted-back values are then available in the controls in theOnLoad
event – note:EnsureChildControls()
invokesCreateChildControls()
.