1:自定义控件:是以.ascx结尾的页面文件,用户控件必须用页面加载才能显示。
事例如下:控件内容
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyMicroUserList.ascx.cs" Inherits="Cfsns.Applications.MicroBlog.Web.MicroBlog.Include.MyMicroUserList" ViewStateMode="Disabled" %>
<%@ Register Src="../Common/Pager.ascx" TagName="Pager" TagPrefix="uc1" %>
<%if (false)
{ %>
//样式和js文件使用if(false)的原因是当调试的时候可用也就是DEBUG模式下可用Release模式下不可用页面人和地方都可以这么用
<%} %>
<table id="divMicroBlogUserList" class="frame">
<colgroup>
<col class="padding_left_width" />
<col class="logo_user" />
<col />
<col style=" 60px;" />
</colgroup>
<tbody class="alter_row_color line_height_normal">
<asp:Repeater ID="DataList1" runat="server">
<ItemTemplate>
<tr keyvalue='<%# Eval("UserId") %>'>
<td rowspan="3">
</td>
<td class="logo_user" rowspan="3">
<asp:Image runat="server" userid='<%# Eval("UserId") %>' role="popUserInfo" ImageUrl='<%# Eval("Logo") %>' AlternateText='<%# Eval("RealName") %>' />
</td>
<td>
<asp:HyperLink runat="server" Text='<%# Eval("RealName") %>' NavigateUrl='<%# Cfsns.Applications.MicroBlog.WebConfigs.PagePath.PFollowedList.ToReplaceQueryString("MicroBlogUserType",MicroBlogUserType).ToReplaceQueryString("UserId",Eval("UserId"))%>' CssClass="text_weight" />
<asp:Literal runat="server" Text='<%# Eval("Title")%>' />
</td>
<td rowspan="3">
<span class="icon_ListenToEachOther" title="相互关注" runat="server" visible='<%#((int)Eval("SearchFollowedType")==(int)Cfsns.Types.MicroBlogSearchTypes.FollowedAndFans) %>'></span>
<div class="frame">
<a runat="server" href="javascript:void(0)" title="取消关注" onclick='<%# Eval("UserId","microBlogContent.toggleFollowed(this,\"{0}\")") %>'>
<%# (int)Eval("SearchFollowedType") >= (int)Cfsns.Types.MicroBlogSearchTypes.Followed ? "取消关注" : "关注"%></a>
</div>
<asp:HyperLink runat="server" NavigateUrl='<%# UrlRedirect.ToReplaceQueryString("UserId",Eval("UserId")).ToReplaceQueryString("OnlyUser",true)%>' Text="查看微博" />
</td>
</tr>
<tr keyvalue='<%# Eval("UserId") %>'>
<td>
<asp:Literal ID="Literal2" runat="server" Text='<%# Eval("CompanyName") %>' />
</td>
</tr>
<tr keyvalue='<%# Eval("UserId") %>'>
<td class="text_unimportant">
博文:<%# Eval("MicroBlogCount") %>| 粉丝:<%# Eval("MicroBlogFansCount") %></td>
</tr>
</ItemTemplate>
</asp:Repeater>
<tr id="divEmpty" runat="server">
<td class="text_empty" colspan="3">
没有找到任何用户
</td>
</tr>
</tbody>
</table>
控件前台页面
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cfsns.Interface;
using Cfsns.Common.Tools.DataExtension;
namespace Cfsns.Applications.MicroBlog.Web.MicroBlog.Include
{
public partial class MyMicroUserList : System.Web.UI.UserControl, IControlDataList
{
Cfsns.Types.MicroBlogSearchTypes microBlogUserType;
public Cfsns.Types.MicroBlogSearchTypes MicroBlogUserType
{
get { return microBlogUserType; }
set { microBlogUserType = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
initScript();
}
}
private void initScript()
{
#if DEBUG
Cfsns.Common.Tools.Web.JavaScript.RegistScript(this.Page, WebConfigs.ApplicationInfo.RootControlPath + "Source/JS/PathDebug.js");
#else
Cfsns.Common.Tools.Web.JavaScript.RegistScript(this.Page, WebConfigs.ApplicationInfo.RootControlPath + "Source/JS/PathRelease.js");
#endif
Cfsns.Common.Tools.Web.JavaScript.RegistScript(this.Page, "~/Source/JS/JOperateCommon.js");
Cfsns.Common.Tools.Web.JavaScript.RegistScript(this.Page, WebConfigs.ApplicationInfo.RootControlPath + "Source/JS/JMicroBlog.js");
}
#region IControlDataList 成员
public System.Data.DataSet DataSource
{
set
{
DataList1.DataSource = value.ToSourceTable();
Pager1.DataSource = value.ToPagerTable();
divEmpty.Visible = value.IsEmpty();
}
}
private bool editMode;
public bool EditMode
{
set { editMode = value; }
get { return editMode; }
}
#endregion
#region IControlDataBind 成员
public object ControlDataBind()
{
DataList1.DataBind();
Pager1.DataBind();
return true;
}
#endregion
#region IUrlRedirect 成员
private string urlRedirect = WebConfigs.PagePath.PDefault;
public string UrlRedirect
{
get { return urlRedirect; }
set { urlRedirect = value; }
}
#endregion
}
}
控件后台内容
下面是加载控件的代码:
try
{
con = Page.LoadControl(Cfsns.Applications.MicroBlog.WebConfigs.PagePath.PLoadMicroUseList);//这句话是把控件加载到内从中
}
catch (Exception ex)
{
Cfsns.Applications.Api.MasterPage.PopUpMessage(this.Page, ex.Message, true);
Cfsns.Bll.SiteLog.LogIt(ex, 10, null);
return;
}
PlaceHolder1.Controls.Add(con);//这里是把控件内容输出到页面上
//绑定控件数据
OperateSqlAnswer answer = Bll.MicroBlog.MicroBlogUserList(searchContent);
IControlDataList icontrolDataList = con as IControlDataList;
icontrolDataList.DataSource = answer.DataSource;
icontrolDataList.UrlRedirect = WebConfigs.PagePath.PDefault.AppendCurrentQueryString("OnlyUser").AppendCurrentQueryString("UserId").ToReplaceQueryString("ScrollPostionId", "PagerToScrollId").ToReplaceQueryString("p", 2);
icontrolDataList.DataSource = answer.DataSource;
icontrolDataList.ControlDataBind();