上节课讲解了asp.net用户控件及自定义控件的概述,这节课讲解用户控件的代码实例
前台页面代码如下
[C#] 纯文本查看 复制代码
<%@ 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>
服务器端代码:
[C#] 纯文本查看 复制代码 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="UserContorl" TagPrefix="USContent" %>
调用用户控件:
<UC:UserContorl runat="server" ID="LeftProServer1" />
服务端代码:
[C#] 纯文本查看 复制代码
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;
}
}
|