这是我在母版页中的子页面:
<%@ 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>
这是此页面背后的代码:
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
}
}
我尝试将此用户控件添加到我的面板但没有加载任何内容。 我无法使用LoadControl方法,因为我需要在加载之前将属性设置为用户控件。
增加:
这是doctor_holder.cs:
public partial class doctor_holder : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
这是用户控制html代码:
<%@ 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>
答案 0 :(得分:2)
您可以直接加载使用控件,就像使用LoadControl方法一样。 LoadControl方法读取文件并将其实例化为可添加到页面的控件 例如:
<%@ 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>
请参阅以下链接:
How to: Create Instances of ASP.NET User Controls Programmatically
Dynamic Loading of ASP.NET User Controls
希望这有帮助..