asp.net如何创建用户自定义控件

在asp.net中如何创建用户自定义控件,最重要的是如何创建自定义控件的属性,比如说我要创建一个repeater用户自定义控件!如何给它设置数据源属性性,麻烦给出详细代码... 在asp.net中如何创建用户自定义控件,最重要的是如何创建自定义控件的属性,比如说我要创建一个repeater用户自定义控件!如何给它设置数据源属性性,麻烦给出详细代码,谢谢! 展开
 我来答
百度网友2fa8b9d
推荐于2016-10-08 · TA获得超过810个赞
知道小有建树答主
回答量:559
采纳率:0%
帮助的人:636万
展开全部
1.打开VS2008-新建一个工程-选译ASP。NET服务器控件
2.自动生成一个文件,打代码拷进去,调试好程序
3.点击“生成菜单-生成XX”
4.到 工程目录\bin\Debug 找到dll文件把它拷到一个固定目录里如:DotNet_DLL里

============附带一个我重写Repeater的代码===================
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.OleDb;

namespace lwf_MVC.lwf_Controls
{
[DefaultProperty("pageIndex")]
[ToolboxData("<!--Repeater Begin --><{0}:Repeater ID=\"Rep_list\" runat=\"server\" EnableViewState=\"false\" DisplayPageing=\"Always\"> <ItemTemplate> </ItemTemplate> </{0}:Repeater><!--Repeater End -->")]
public class Repeater : System.Web.UI.WebControls.Repeater
{
private int _recordcount = 0;
private int _pageSize = 0;
private int _pageIndex = 1;
private string _PID = "";
private string _CssClass = "CssPage"; //输出数据流时设一个样式给它样式名为CssPage
private object _DataSourceList;
private PagedDataSource _pds = new PagedDataSource();
//public bool _DisplayPageing;
public MyEnum _DisplayPageing;
private MyEnumLanguage _Language;

//构造函数
public Repeater()
{

}

#region ____________控件属性:RecordCount、pageSize、pageIndex、PID、CssClass、DataSource1____________

[Bindable(true)]
[Category("data")]
[DefaultValue("1")]
[Localizable(true)]
[Description("记录总数")]
public int RecordCount
{
get
{
return _recordcount;
}
set
{
_recordcount = value;
}

}

[Bindable(true)]
[Category("data")]
[DefaultValue("15")]
[Localizable(true)]
[Description("单页记录数")]
public int pageSize
{
get
{
return _pageSize;
}

set
{
_pageSize = value;
}
}

[Bindable(true)]
[Category("data")]
[DefaultValue("1")]
[Localizable(true)]
[Description("当前页码")]
public int pageIndex
{
get
{
return _pageIndex;
}

set
{
_pageIndex = value;
}
}

[Bindable(true)]
[Category("data")]
[DefaultValue("")]
[Localizable(true)]
[Description("其它参数传递")]
public string PID
{
get
{
return _PID;
}

set
{
_PID = value;
}
}

[Bindable(true)]
[Category("data")]
[DefaultValue("CssPage")]
[Localizable(true)]
[Description("连接样式")]
public string CssClass
{
get
{
return _CssClass;
}

set
{
_CssClass = value;
}
}

[Bindable(true)]
[Category("data")]
[DefaultValue("")]
[Localizable(true)]
[Description("数据源,DataTable")]
public object DataSource1
{
get
{
return _pds;

}
set
{

switch (value.GetType().ToString())
{
case "System.Data.DataTable":
_recordcount = ((DataTable)value).Rows.Count;
_pds.DataSource = ((DataTable)value).DefaultView;
break;
case "System.Collections.Generic.List`1[System.Object]":
_recordcount = ((List<object>)value).Count;
_pds.DataSource = (List<object>)value;
break;
case "System.Data.SqlClient.SqlDataReader":
DataTable i_dt = Normal.ConvertDataReaderToDataTable((SqlDataReader)value);
_recordcount = i_dt.Rows.Count;
_pds.DataSource = i_dt.DefaultView;
break;
case "System.Data.SqlClient.OleDbDataReader":
DataTable i_dtOle = Normal.ConvertDataReaderToDataTable((OleDbDataReader)value);
_recordcount = i_dtOle.Rows.Count;
_pds.DataSource = i_dtOle.DefaultView;
break;
default:
_recordcount = ((DataTable)value).Rows.Count;
_pds.DataSource = ((DataTable)value).DefaultView;
break;

}

_pds.AllowPaging = true;
_pds.PageSize = _pageSize;
_pds.CurrentPageIndex = this._pageIndex - 1;
base.DataSource = _pds;
}

}

public MyEnum DisplayPageing
{

get { return _DisplayPageing; }
set { _DisplayPageing = value; }

}

public enum MyEnum
{
Always = 1,
Auto = 2,
Never = 3

}

public MyEnumLanguage Language
{

get { return _Language; }
set { _Language = value; }

}

public enum MyEnumLanguage
{
Chinese = 1,
English = 2

}

#endregion

//外部使用
public string ThePage()
{
string retStr = "";
retStr = this.ThePage(_recordcount, _pageSize, _pageIndex, _PID, _CssClass);
return retStr;

}

//分页函数,为输出显示流提供HTML代码

/// <summary>
/// 分页函数
/// </summary>
/// <param name="i_RecordCount">总记录数</param>
/// <param name="i_pageSize">每页记录数</param>
/// <param name="i_pageIndex">当前页数</param>
/// <param name="i_PID">其它参数</param>
/// <param name="i_CssClass">连接样式</param>
/// <returns>HTML代码</returns>
public string ThePage(int i_RecordCount, int i_pagesize, int i_pageIndex, string i_PID, string i_CssClass)
{
int i_Pcount = 0;
int i_StartPageNum;
int i_EndPageNum;
string i_pagerString = "";
string i_pagerString_ENG = "";

i_pagerString = i_pagerString + "<style type=\"text/css\">\n.CssPage {font-family: Arial, Helvetica, sans-serif;font-size: 12px;line-height: 25px;color: #333;text-decoration: none;}\na.CssPage:hover{font-family: Arial, Helvetica, sans-serif;font-size: 12px;line-height: 25px;color: #ff0000;text-decoration: underline;}\n</style>\n";
i_pagerString_ENG = i_pagerString_ENG + "<style type=\"text/css\">\n.CssPage {font-family: Arial, Helvetica, sans-serif;font-size: 12px;line-height: 25px;color: #333;text-decoration: none;}\na.CssPage:hover{font-family: Arial, Helvetica, sans-serif;font-size: 12px;line-height: 25px;color: #ff0000;text-decoration: underline;}\n</style>\n";

if (i_pagesize != 0)
{
i_Pcount = (i_RecordCount / i_pagesize);
i_Pcount = ((i_RecordCount % i_pagesize) != 0 ? i_Pcount + 1 : i_Pcount);
i_Pcount = (i_Pcount == 0 ? 1 : i_Pcount);
}

if (i_pageIndex <= 0) i_pageIndex = 1;
if (i_pageIndex > i_Pcount) i_pageIndex = i_Pcount;

i_StartPageNum = 1;
while (i_StartPageNum + 10 <= i_pageIndex)
{
i_StartPageNum = i_StartPageNum + 10;

}
i_EndPageNum = i_StartPageNum + 9;

if (i_EndPageNum > i_Pcount) i_EndPageNum = i_Pcount;

i_pagerString = i_pagerString + "<div class='" + i_CssClass + "' style='padding-left:20px;padding-right:20px'>共<b>" + i_Pcount + "</b>页 共有<b>" + i_RecordCount + "</b>记录 第<b>" + i_pageIndex + "</b>页 跳到第 [ ";
if (i_RecordCount <= Convert.ToInt32((i_pageIndex) * i_pagesize))
{
i_pagerString_ENG = i_pagerString_ENG + "<div class='" + i_CssClass + "' style='padding-left:20px;padding-right:20px;float:right'><div style='float:left;padding-right:60px'>Total " + i_RecordCount + " , " + ((i_pageIndex - 1) * i_pagesize + 1) + "-" + i_RecordCount + " of " + i_RecordCount + " Records </div><div style='float:left;padding-left:60px'>";

}
else
{
i_pagerString_ENG = i_pagerString_ENG + "<div class='" + i_CssClass + "' style='padding-left:20px;padding-right:20px;float:right'><div style='float:left;padding-right:60px'>Total " + i_RecordCount + " , " + ((i_pageIndex - 1) * i_pagesize + 1) + "-" + ((i_pageIndex) * i_pagesize) + " of " + i_RecordCount + " Records </div><div style='float:left;padding-left:60px'>";
}
if (i_pageIndex != 1)
{
//i_pagerString = i_pagerString + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=1'> 首页</a> <a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_pageIndex - 1).ToString() + "'>上一页</a>";
i_pagerString_ENG = i_pagerString_ENG + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=1'> First</a> <a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_pageIndex - 1).ToString() + "'>Prev</a> ";

}
i_pagerString_ENG = i_pagerString_ENG + " [ ";

if (i_StartPageNum > 1)
{
i_pagerString = i_pagerString + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_StartPageNum - 1).ToString() + "' title='前十页'><font face='webdings'>7</font></a>";
i_pagerString_ENG = i_pagerString_ENG + " <a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_StartPageNum - 1).ToString() + "' title='Prev Ten'><font face='webdings'>7</font></a>";
}

for (int i_i = i_StartPageNum; i_i <= i_EndPageNum; i_i++)
{
if (i_i != i_pageIndex)
{
i_pagerString = i_pagerString + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=" + i_i.ToString() + "'>" + i_i.ToString() + "</a> ";
i_pagerString_ENG = i_pagerString_ENG + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=" + i_i.ToString() + "'>" + i_i.ToString() + "</a> ";
}
else
{
i_pagerString = i_pagerString + "<b><font color=red>" + i_i.ToString() + "</font></b> ";
i_pagerString_ENG = i_pagerString_ENG + "<b><font color=red>" + i_i.ToString() + "</font></b> ";

}

}

if (i_EndPageNum < i_Pcount)
{
i_pagerString = i_pagerString + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_EndPageNum + 1).ToString() + "' title='下十页'><font face='webdings'>8</font></a>";
i_pagerString_ENG = i_pagerString_ENG + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_EndPageNum + 1).ToString() + "' title='Next Ten'><font face='webdings'>8</font></a>";
}
i_pagerString = i_pagerString + "] 页";
i_pagerString_ENG = i_pagerString_ENG + "] ";

if (i_pageIndex != 1)
{
i_pagerString = i_pagerString + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=1'> 首页</a> <a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_pageIndex - 1).ToString() + "'>上一页</a>";
//i_pagerString_ENG = i_pagerString_ENG + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=1'> 首页</a> <a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_pageIndex - 1).ToString() + "'>上一页</a>";

}

if (i_Pcount != i_pageIndex)
{
i_pagerString = i_pagerString + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_pageIndex + 1).ToString() + "'> 下一页</a> <a class='" + i_CssClass + "' href='?" + i_PID + "page=" + i_Pcount + "'>尾页</a>";
i_pagerString_ENG = i_pagerString_ENG + "<a class='" + i_CssClass + "' href='?" + i_PID + "page=" + (i_pageIndex + 1).ToString() + "'> Next</a> <a class='" + i_CssClass + "' href='?" + i_PID + "page=" + i_Pcount + "'>End</a>";
}

i_pagerString = i_pagerString + "</div>";
i_pagerString_ENG = i_pagerString_ENG + "</div></div>";

if (_Language == MyEnumLanguage.English)
{
return i_pagerString_ENG;
}
else
{
return i_pagerString;
}

}

//将HTML代码加入输出流
protected override void Render(HtmlTextWriter output)
{
base.Render(output);

switch (_DisplayPageing)
{
case MyEnum.Always:
output.WriteLine(ThePage(_recordcount, _pageSize, _pageIndex, _PID, _CssClass));
break;

case MyEnum.Auto:
if (_pageSize < _recordcount)
{
output.WriteLine(ThePage(_recordcount, _pageSize, _pageIndex, _PID, _CssClass));
}
else
{
output.WriteLine("");
}
break;

case MyEnum.Never:
output.WriteLine("");
break;
}

//if (_DisplayPageing == true) output.WriteLine(ThePage(_recordcount, _pageSize, _pageIndex, _PID, _CssClass));
// output.WriteLine(ThePage(_recordcount, _pageSize, _pageIndex, _PID, _CssClass));
}
}
}
tanzongyao233
2010-07-02 · TA获得超过288个赞
知道小有建树答主
回答量:595
采纳率:100%
帮助的人:211万
展开全部
右键web项目
添加新项 有个用户控件直接创建·
WebUserControl1.ascx

用户控件只是你很多页面都需要使用它 在用用户控件··
其实好比一个母版页一样·
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
2008_knowledge
2010-07-02 · TA获得超过126个赞
知道小有建树答主
回答量:348
采纳率:0%
帮助的人:151万
展开全部
你说的是重写某个控件吧,这个有点难度!
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
怀特肖
2010-07-03
知道答主
回答量:13
采纳率:0%
帮助的人:2.2万
展开全部
直接新建用户控件
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式