2

I've search a lot for this issue but nothing came up.

My problem is the following: I have a main view in which I want to load a user control with parameters when I click on a button but the user control won't show. The constructor of the user control is called and the parameters are set, even the page load event from the user control is called. What am i doing wrong?

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 button click event:

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>

The user control isn't visible on the main page and I don't know why.

PS: I know that i can send parameters as seen below but I don't understand why I cannot use the method described above.

UserControl1 uc = (UserControl1)this.LoadControl("~/UserControls/UserControl1.ascx");
uc.Param1 = "val1";
...
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Zippy
  • 1,804
  • 5
  • 27
  • 36
  • Please take a look at [this](http://geekswithblogs.net/shahed/archive/2006/03/23/73096.aspx) – Bikee Mar 23 '16 at 09:16
  • @BikashSinghMaharjan It's working, but why use some extra method instead of built in functionality? – Zippy Mar 23 '16 at 09:38

1 Answers1

2

Here is the full explanation of why second method with LoadControl by type will not work: 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).

Community
  • 1
  • 1
Lesmian
  • 3,932
  • 1
  • 17
  • 28
  • 1
    Thanks for the explanation. It seems weird that Microsoft added a method which cannot be used as expected.. – Zippy Mar 23 '16 at 10:20