添加看似不相关的用户控件时,DropDownList OnSelectedIndexChanged事件停止工作



我有一个带有中继器的页面。中继器的每个部件内都有更新面板,每个面板都包含一个控件:

<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>

后面的代码提供数据源:

    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; }
}

当下拉列表选择的索引发生更改时,这是代码后面触发的事件:

    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();
    }

所有这些代码都很好用。当所选索引发生更改时,标签文本会更新为匹配,并且一切都很好。

问题是当我向中继器添加用户控件时。用户控件做什么似乎并不重要,但这里有一个我用来破坏这个示例代码的简单示例:

正面:

<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;
    }
}

当我在页面上注册此用户控件时:

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

并将其放置在中继器内自己的更新面板中:

<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事件停止激发。甚至插入系统。诊断。调试器。事件内部的Break()不会激发。其他一切(事件代码、页面加载、类)都保持不变。唯一的区别是将此用户控件添加到页面和中继器中。

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

谢谢你的帮助!

编辑:已回答。我只是错过了一页。WebUserControl1的Page_Load事件中的IsPostBack测试。愚蠢的错误

是的,如果你不检查,我也有类似的确切问题!页面加载上的IsPostback属性,然后控件再次反弹,因此内部的其他控件(如dropdownlist)也会反弹并重新初始化,因此事件不会触发

最新更新