This is my child page inside a master page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="doctoryab_shokri.Default" masterpagefile="~/web.Master" %>
<asp:Content ID="my_content" runat="server" contentplaceholderid="ContentPlaceHolder1">
<asp:Panel ID="pnl_controls" runat="server">
</asp:Panel>
</asp:Content>
And this is the code behind this page:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
pnl_controls.Controls.Add(new sections.doctor_holder());
//sectios is a folder and doctor_holder is the user control
}
protected void Page_Init(object sender, EventArgs e)
{
pnl_controls.Controls.Add(new sections.doctor_holder());
//sectios is a folder and doctor_holder is the user control
}
}
I try to add this user control to my panel but nothing is loaded. I cant use LoadControl method because I need to set a property to user control before loading it.
ADDED:
This is doctor_holder.cs:
public partial class doctor_holder : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
And this is user control html code:
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="doctor-holder.ascx.cs" Inherits="doctoryab_shokri.sections.doctor_holder" %>
<div class="doc-holder col-md-6">
<div class="doc-img">
<asp:Image ID="img_doc_avatar" runat="server" Height="64px" Width="64px" ImageUrl="~/img/opt-doc-logo.png" />
</div>
<div class="doc-info">
<p class="doc-name">
<asp:Label ID="lbl_name_lname" runat="server" Text="#" CssClass="doc-name"></asp:Label>
</p>
<p class="doc-pro">
<asp:Label ID="lbl_proff" runat="server" Text="#"></asp:Label>
</p>
<p class="doc-addr">
<asp:Label ID="lbl_address" runat="server" Text="#"></asp:Label>
</p>
<div style="float:left;">
<p class="nobat-cap">امکان ثبت نوبت</p>
<p class="nobat-type">
<asp:LinkButton ID="lnk_type" runat="server" ForeColor="White">#</asp:LinkButton>
</p>
</div>
<br style="clear: both;">
</div>
You can directly load use controls like that you have use the LoadControl method. The LoadControl method reads the file and instantiates it as a control that can be added to the page.
Example:
<%@ Page Language="C#" %>
<%@ Reference Control="~/Controls/Spinner.ascx" %>
<script runat="server">
private ASP.Spinner Spinner1
protected void Page_Load(object sender, EventArgs e)
{
Spinner1 = (ASP.Spinner)LoadControl("~/Controls/Spinner.ascx");
}
protected void Button1_Click(object sender, EventArgs e)
{
PlaceHolder1.Controls.Add(Spinner1);
}
</script>
Refer the below links:
How to: Create Instances of ASP.NET User Controls Programmatically
Dynamic Loading of ASP.NET User Controls
Hope this help..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.