ibcadmin 发表于 2012-12-23 14:28:51

asp.net创建用户控件实例

上节课讲解了asp.net用户控件及自定义控件的概述,这节课讲解用户控件的代码实例
前台页面代码如下

<%@ 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;
      }
使用用户控件在页面中注册用户控件
<%@ RegisterSrc="~/Help/Content.ascx" TagName="UserContorl" TagPrefix="USContent" %>

调用用户控件:
<UC:UserContorl runat="server" ID="LeftProServer1" />
服务端代码:

   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;
             }
         }
页: [1]
查看完整版本: asp.net创建用户控件实例