1

我有一个 ASPX 页面,它有一个 PlaceHolder,其中用户控件是动态加载的。HTML 标记如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html>

<html>
<head runat="server">
</head>
<body>
    <form id="form1" runat="server">

        <asp:Button ID="btnNumbers" Text="Select Numbers" OnClick="SelectNumbers" runat="server" />
        <br /><br />
        <asp:Button ID="btnLetters" Text="Select Letters" OnClick="SelectLetters" runat="server" />
        <br /><br />

        <asp:PlaceHolder ID="phContent" runat="server" />

    </form>
</body>
</html>

后面的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Test : System.Web.UI.Page
{
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
            HttpContext.Current.Session["control"] = "~/Controls/Numbers.ascx";
        LoadControl();        
    }

    protected void SelectNumbers(object sender, EventArgs e)
    {
        HttpContext.Current.Session["control"] = "~/Controls/Numbers.ascx";
        LoadControl();
    }

    protected void SelectLetters(object sender, EventArgs e)
    {
        HttpContext.Current.Session["control"] = "~/Controls/Letters.ascx";
        LoadControl();
    }

    private void LoadControl()
    {
        phContent.Controls.Clear();
        var control = Page.LoadControl((String)HttpContext.Current.Session["control"]);
        phContent.Controls.Add(control);
    }
}

第一个用户控件有一个 DropDownList 来选择数字。它的标记如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Numbers.ascx.cs" Inherits="Numbers" %>


<asp:DropDownList ID="ddlSelectNumbers"
                  AutoPostBack="true" runat="server"
                  OnSelectedIndexChanged="SelectNumbers_IndexChanged" >
    <asp:ListItem Text="One" Value="1" />
    <asp:ListItem Text="Two" Value="2" />
    <asp:ListItem Text="Three" Value="3" />
</asp:DropDownList>

<br /><br />

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

后面的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Superexpert.Controls;


public partial class Numbers : System.Web.UI.UserControl
{
    public override String ID
    {
        get { return "Numbers"; }
        set { }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SelectNumbers_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedNumber.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}

第二个用户控件有一个 DropDownList 来选择字母。它的标记如下:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Letters.ascx.cs" Inherits="Letters" %>



<asp:DropDownList ID="ddlSelectLetters"
                  AutoPostBack="true" runat="server"
                  OnSelectedIndexChanged="SelectLetters_IndexChanged" >
    <asp:ListItem Text="A" Value="a" />
    <asp:ListItem Text="B" Value="b" />
    <asp:ListItem Text="C" Value="c" />
</asp:DropDownList>

<br /><br />

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

后面的代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Superexpert.Controls;


public partial class Letters : System.Web.UI.UserControl
{
    public override String ID
    {
        get { return "Letters"; }
        set { }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void SelectLetters_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedLetter.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}

当页面第一次加载(!IsPostBack)时,一切正常。您可以从 DropDownList 中选择数字,然后触发 SelectedItemChanged 事件,更新 SelectedValue 并更新 Label。

当您单击其中一个按钮以动态加载其中一个用户控件时,就会出现问题。之后,第一次从 DropDownList 中选择某些内容时,SelectedItemChanged 事件不会触发,它的 SelectedValue 不会更新,因此 Label 不会更新。之后,一切正常。

是什么导致这种情况发生?

提前致谢!

4

1 回答 1

1

你非常封闭。您只需将 ID 分配给动态创建的控件。

基本上,如果要在回发时检索它们,则需要将 ID 分配给动态创建的控件。

<---下面代码中的3。

public partial class Test : System.Web.UI.Page
{
   ... Same as above

    private void LoadControl()
    {
        phContent.Controls.Clear();
        var control = Page.LoadControl((String)HttpContext.Current.Session["control"]);
        control.ID = "1"; <--- 1. Control ID is required. 
        phContent.Controls.Add(control);
    }
}

public partial class Numbers : System.Web.UI.UserControl
{
    // public override String ID ... <--- 2. Remove this

    protected void SelectNumbers_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedNumber.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}

public partial class Letters : System.Web.UI.UserControl
{
    // public override String ID ... <--- 3. Remove this

    protected void SelectLetters_IndexChanged(object sender, EventArgs e)
    {
        lblSelectedLetter.Text = ((DropDownList)sender).SelectedItem.Text;
    }
}
于 2014-09-16T22:37:47.823 回答