ASP.NET 中 DropDownList 的 SelectedIndexChanged 以错误的顺序触发

发布于 2024-09-16 00:51:31 字数 1031 浏览 5 评论 0原文

我的代码中有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沉鱼一梦 2024-09-23 00:51:31

您无法更改页面生命周期。您可以做的是检查是否 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!

清眉祭 2024-09-23 00:51:31

这是你将调用的方法

protected void override void CreateChildControls()
{
    base.CreateChildControls();
    dropDown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
}

This is you will call method

protected void override void CreateChildControls()
{
    base.CreateChildControls();
    dropDown.SelectedIndexChanged += new EventHandler(DropDown_SelectedIndexChanged);
}
咽泪装欢 2024-09-23 00:51:31

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.

属性 2024-09-23 00:51:31

无法从 CreateChildControls() 访问回发值;您必须等到 OnLoad 事件发生,才能将回发的私有视图状态加载到控件中。

OnInt事件中调用EnsureChildControls();然后,回发的值可在 OnLoad 事件的控件中使用 - 注意:EnsureChildControls() 调用 CreateChildControls()

Posted-back values are not accessible from CreateChildControls(); you must wait until the OnLoad event for the posted back private view state to be loaded into controls.

In the OnInt event call EnsureChildControls(); the posted-back values are then available in the controls in the OnLoad event – note: EnsureChildControls() invokes CreateChildControls().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文