您的位置:首页 - 教程 - ASP.NET - 正文
ASP.NET用户控件的使用

创建用户控件如下:

前台页面代码如下:

View Code
 <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Content.ascx.cs" Inherits="WLaw.Vip.Help.Content" %>


<div >
<p

      <span>
  <asp:Label ID="lblTitle" runat="server" Text=""> </asp:Label>

      </span>
</p>
<asp:Label ID="lblFullContent" runat="server" Text=""> </asp:Label>
</div>

 

 服务器端代码:

   

       public HelpInfo hif;
public HelpContentInfo HelpContent;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PageInit();
}

}
private void PageInit()
{
if (hif == null)
{
return;
}
lblTitle.Text = hif.Title;
lblFullContent.Text = HelpContent.FullContent;
}

 

使用用户控件

在页面中注册用户控件

<%@ Register  Src="~/Help/Content.ascx" TagName="UserControl" TagPrefix="Hovertree" %>

调用用户控件:

 <Hovertree:UserControl runat="server" ID="LeftProServer1" />

服务端代码:

View Code
   protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int HelpId = Convert.ToInt32(Request["hId"]);
HelpInfo hp = HelpBiz.Get(HelpId);
HelpContentInfo HelpContent = HelpContentBiz.Get(HelpId);
USContent1.hif = hp;
USContent1.HelpContent = HelpContent;

_title.InnerText = hp.Title;
}
}

评论: