DropDownList的OnSelectedIndexChanged事件时停止加看似无关的用户控制工作 [英] DropDownList OnSelectedIndexChanged event stops working when seemingly unrelated User Control is added

查看:124
本文介绍了DropDownList的OnSelectedIndexChanged事件时停止加看似无关的用户控制工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个中继器的页面。该中继器的每一个项目里面是更新的面板,每片含控制:

I have a page with a repeater. Inside each item of that repeater are update panels, each containing a control:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Property1") %>' />
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostback="true"
                    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Text="1" Value="1" />
                    <asp:ListItem Text="2" Value="2" />
                    <asp:ListItem Text="3" Value="3" />
                    <asp:ListItem Text="4" Value="4" />
                </asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
    </ItemTemplate>
</asp:Repeater>

在code后面提供数据源:

The code behind supplies the data source:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            List<Class1> Source = new List<Class1>();
            Source.Add(new Class1("Test1"));
            Source.Add(new Class1("Test2"));
            Source.Add(new Class1("Test3"));
            Repeater1.DataSource = Source;
            Repeater1.DataBind();
        }
    }

和这里的类被绑定:

public class Class1
{
    public string Property1 { get; set; }
    public string Property2 { get { return "Property 2"; } }
    public Class1() { Property1 = string.Empty; }
    public Class1(string P1) { Property1 = P1; }
}

当在下拉列表中选择指标发生变化,这是事件背后的code类火灾:

And when the drop down list selected index is changed, this is the event that fires in the code behind:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl = (DropDownList)sender;
        RepeaterItem ri = (RepeaterItem)ddl.NamingContainer;
        Label l = (Label)ri.FindControl("Label1");

        l.Text = ddl.SelectedValue;

        UpdatePanel up = (UpdatePanel)ri.FindControl("UpdatePanel1");
        up.Update();
    }

这一切code的工作就好了。当选定的指数发生变化时,标签文本进行更新以匹配,一切都是幸福的。

All of this code works just fine. When the selected index is changed, the label text is updated to match, and everything is happy.

问题是,当我添加一个用户控件的中继器。这似乎并不重要用户控件做什么,但这里是一个简单的例子,我曾经打破了这个样本code:

The problem is when I add a user control to the repeater. It doesn't seem to matter what the user control does, but here is a simple example I used to break this sample code:

前:

<asp:Label ID="Label1" runat="server" />

备份:

public partial class WebUserControl1 : System.Web.UI.UserControl
{
    private Class1 _Item = null;
    public Class1 Item { get { return _Item; } set { _Item = value; } }

    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = Item.Property1;
    }
}

在我注册页面上的这个用户控件:

When I register this user control on the page:

<%@ Register Src="WebUserControl1.ascx" TagName="Control1" TagPrefix="uc1" %>

,并将其放置中继里面自己的更新面板:

And place it inside the repeater in its own update panel:

<asp:Repeater ID="Repeater1" runat="server">
    <ItemTemplate>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("Property1") %>' />
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <uc1:Control1 ID="Control1" runat="server" Item='<%# ((IDataItemContainer)Container).DataItem %>' />
            </ContentTemplate>
        </asp:UpdatePanel>
        <asp:UpdatePanel ID="UpdatePanel3" runat="server" UpdateMode="Conditional">
            <ContentTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server" AutoPostback="true"
                    OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
                    <asp:ListItem Text="1" Value="1" />
                    <asp:ListItem Text="2" Value="2" />
                    <asp:ListItem Text="3" Value="3" />
                    <asp:ListItem Text="4" Value="4" />
                </asp:DropDownList>
            </ContentTemplate>
        </asp:UpdatePanel>
    </ItemTemplate>
</asp:Repeater>

用户控制涂料正确(暗示它是工作),但OnSelectedIndexChanged事件停止击发。即使插入System.Diagnostics.Debugger.Break()内不会触发事件。其他一切(事件code,页面加载,应用类)都保持不变。唯一的区别是增加这个用户控件的页面和中继器。

The user control paints correctly (suggesting it is working), but the OnSelectedIndexChanged event stops firing. Even inserting System.Diagnostics.Debugger.Break() inside the event doesn't fire. Everything else (the event code, the page load, the class) all remain the same. The only difference is adding this user control to the page and repeater.

谁能告诉我为什么要引入这个(看似无关的)用户控件似乎打破DropDownList的行为?

Can anyone tell me why the introduction of this (seemingly unrelated) user control seems to break the DropDownList behavior?

感谢您的帮助!

编辑:回答。我只是缺少WebUserControl1的Page_Load事件一Page.IsPostBack测试。愚蠢的错误。

推荐答案

是啊,我也有类似的具体问题,如果你不检查页面加载在!的IsPostBack 属性,那么该控件是再次反弹有FORE里面的其他控件,如DropDownList中也出现反弹,并重新初始化,因此该事件不会触发

Yeah I had similar exact issue, if you dont check the !IsPostback property on the page load , then the control is rebound again and there fore the other controls inside like the dropdownlist is also rebound and reinitialized and thus the event is not fired

这篇关于DropDownList的OnSelectedIndexChanged事件时停止加看似无关的用户控制工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆