asp.net 中未显示的参数动态加载的用户控件

User control loaded dynamically with parameters not showing in asp.net

我已经为这个问题搜索了很多,但没有任何结果。

我的问题如下:我有一个主视图,当我点击一个按钮时,我想在其中加载一个带参数的用户控件,但用户控件不会显示。调用用户控件的构造函数并设置参数,甚至调用用户控件的页面加载事件。我做错了什么?

Main.aspx:

  <%@ Page Title="Main page" Language="C#" AutoEventWireup="true" CodeBehind="Main.aspx.cs" Inherits="MainApp.Main" MasterPageFile="~/Site.master" %>
   <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"></asp:Content>

   <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <%-- some other fields which i send to the user control on click --%>
                <div class="col-lg-1 col-sm-12  col-md-1">
                    <asp:Button runat="server" CssClass="btn btn-primary" CausesValidation="false" ID="generateData" OnClick="generateData_Click" Text="View info" />
                </div>
   <div runat="server" id="contentDiv">
        <p>No info available atm.</p>
    </div>
</asp:Content>

Main.aspx.cs按钮点击事件:

protected void generateData_Click(object sender, EventArgs e)
{
    UserControl1 uc = (UserControl1 )this.LoadControl(typeof(UserControl1 ), new object[] { param1, param2, param3});
    contentDiv.Controls.Clear();
    contentDiv.Controls.Add(uc);
}

UserControl1.aspx.cs:

public partial class UserControl1: System.Web.UI.UserControl
{
        public string Param3 { get; set; }
        public string Param1 { get; set; }
        public string Param2 { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.DataBind();
        }

        public UserControl1(string param1, string param2, string param3)
        {
            Param1 = param1;
            Param2 = param2;
            Param3 = param3;
        }
}

UserControl1.ascx:

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="UserControl1.ascx.cs" Inherits="MainApp.UserControls.UserControl1" %>
<div>
    <p style="color: red">Under construction</p>
</div>

用户控件在主页上不可见,我不知道为什么。

PS: 我知道我可以发送参数,如下所示,但我不明白为什么我不能使用上述方法。

UserControl1 uc = (UserControl1)this.LoadControl("~/UserControls/UserControl1.ascx");
uc.Param1 = "val1";
...

以下是为什么第二种方法按类型加载控制不起作用的完整解释:Asp.net Usercontrol LoadControl Issue

The reason there is a difference is because in the second instance you are bypassing the binding mechanism of the the two partial classes.

When you include the ascx control in the page as a normal declaration, the controls declared within it (the label in your example) are automatically instantiated by the framework as the class is instantiated. This is a binding mechnism that reads the "front-end" ascx file and instantiates objects in the class.

When you use LoadControl(string) - you are doing exactly the same thing.. it uses a virtual path to get the "front-end" file and ensure controls within it are instantiated.

However, when you use the new (to 2.0) LoadControl(type, object) version, there is no virtual path available and it rightly so makes no assumptions (as it cannot ensure that a type has a front end file).