代码之家  ›  专栏  ›  技术社区  ›  user834850

从动态加载的用户控件中单击Fire按钮

  •  0
  • user834850  · 技术社区  · 9 年前

    母版页上有占位符。 当选择菜单项时,它会动态加载用户控件ascx。

    PlaceHolder1.Controls.Clear();
    PlaceHolder1.Controls.Add("pages/MyControl.ascx");
    

    但MyControl上有一个按钮,它的源中有一条记录

     <asp:Button ID="reg" runat="server" OnClick="reg_Click" Text="Reg" Width="207px" />
    

    此外,回调位于MyControl的代码隐藏中。

    public partial class RegInit : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
    
        protected void reg_Click(object sender, EventArgs e)
        {
            // never comes here
        }
    }
    

    但是reg_Click从来没有被调用过。我看到的只是在单击“reg”按钮时重新加载主页面。

    2 回复  |  直到 9 年前
        1
  •  1
  •   user834850 user834850    9 年前

    我还必须发布我的解决方案。

    至于我,我认为我找到了更好的解决办法。

    用户控件的概念是能够在面板上放置多个基本控件,并将它们作为一个控件使用,第二个控件是基本控件中的值,无论用户输入什么,都应在切换用户控件集时保留这些值。

    现在是解决方案。 我刚刚创建了所有控件,但没有使用PlaceHolder,而是显式地将它们放在主页的同一位置,但属性“Visible”设置为False。 然后,在选择菜单项时,我只将相应控件的Visible属性设置为True。 因此,它应该起作用。。

    要放置页面的控件,请使用标记Register: 在我的示例中,有两个控件About和RegInit 主页.aspx :

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="MainPage.aspx.cs" Inherits="MyApp.MainPage" %>
    <%@ Register TagPrefix="uc" TagName="About" Src="~/controls/About.ascx" %>
    <%@ Register TagPrefix="uc" TagName="RegInit" Src="~/controls/RegInit.ascx" %>
    

    下面是他们在代码中的位置:

            <uc:About id="about" runat="server" Visible="true" />
            <uc:RegInit id="regInit" runat="server" Visible="false" />
    

    而且在 主页.aspx.cs

    protected void MainMenu_ItemClick(object sender, MenuEventArgs e)
    {
        if (e.Item.Text == "ABOUT")
        {
            about.Visible = true;
            regInit.Visible = false;
        }
        if (e.Item.Text == "REGISTER")
        {
            regInit.Visible = true;
            about.Visible = false;
        }
    }
    

    这要好得多,因为输入的所有用户数据都会保留下来,用户可以通过切换控件继续使用这些数据。

    我会勾选康纳的解决方案,但我决定去掉占位符。。。

        2
  •  0
  •   ConnorsFan    9 年前

    选择菜单命令后,必须在每次回发时将用户控件加载到PlaceHolder中。如果选择了菜单命令,则可以在会话变量中记住。

    // Property giving the show/hide status of the user control
    private bool ShouldLoadMyControl
    {
        get { return Session["LoadMyControl"] != null && (bool)Session["LoadMyControl"]; }
        set { Session["LoadMyControl"] = value; }
    }
    
    // Load the control on every postback (if option selected)
    protected void Page_Load(object sender, EventArgs e)
    {
        if (ShouldLoadMyControl)
        {
            LoadMyControl();
        }
    }
    
    // Utility function to load the user control
    private void LoadMyControl()
    {
        PlaceHolder1.Controls.Add(LoadControl("pages/MyControl.ascx"));
    }
    
    // Event handler for the command to show the control
    protected void mnuLoadMyControl_Click(object sender, EventArgs e)
    {
        if (!ShouldLoadMyControl)
        {
            ShouldLoadMyControl= true;
            LoadMyControl();
        }
    }
    
    // Event handler for a command to hide the control
    protected void mnuHideMyControl_Click(object sender, EventArgs e)
    {
        ShouldLoadMyControl= false;
        PlaceHolder1.Controls.Clear();
    }