ASP.NET用户自定义控件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ASP.NET用户自定义控件相关的知识,希望对你有一定的参考价值。
ASP.NET(VB.NET)创建一个动态控件,然后我想根据数据库某个字段的值来决定要动态添加多少个用户控件(.ASCX)
比如:创建一个test.ascx控件,然后在内容页的LOAD事件获取到数据值为I,如何在PLACEHOLDER1动态添加进I个test.ascx,并且想改变test.ascx里面label1控件的text属性
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 --><:Repeater ID=\"Rep_list\" runat=\"server\" EnableViewState=\"false\" DisplayPageing=\"Always\"> <ItemTemplate> </ItemTemplate> </: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
set
public enum MyEnum
Always = 1,
Auto = 2,
Never = 3
public MyEnumLanguage Language
get
set
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 \na.CssPage:hover\n</style>\n";
i_pagerString_ENG = i_pagerString_ENG + "<style type=\"text/css\">\n.CssPage \na.CssPage:hover\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));
另外,站长团上有产品团购,便宜有保证 参考技术B test.ascx:
public partial class Controls_test. : System.Web.UI.UserControl
private string _text;
public string text
get return _text;
set _text = value;
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
//Response.Writr(text);
//.....
----------
aspx:
protected void Page_Load(object sender, EventArgs e)
int i=5;
string [] v ="aa","bb","cc","dd","ee","ff";
for(i;i<5;i++)
UserControl myControl = (UserControl)this.Page.LoadControl("test.ascx");
myControl.Attributes..Add("Text",v[i]);
LACEHOLDER1.Controls.Add(myControl);
追问
能不能给些注释,有些地方不懂,加上自己用的是VB.NET,看起来有些吃力,改成VB.NET的代码有些地方出错了,可以的话给我发个项目,280681273@qq.com
追答用户控件属性的修饰符可以是任何访问控制符,还可以被定义为静态。get和set是一种特定的方法,get用来从对象中读取数据,而set用来向字段写入数据,将外界的数据写入字段时,c#使用value表示输入的数据,value可以说是一个准关键字
public string text
get return _text; //读取
set _text = value; //写入
if (!Page.IsPostBack)
label1.text = text;
-------
UserControl myControl = (UserControl)this.Page.LoadControl("test.ascx");//定义一个新web用户控件实例,来源于test.ascx文件
myControl.Attributes..Add("Text",v[i]);//给这个web控件添加一个属性,属性名Text,并赋值
LACEHOLDER1.Controls.Add(myControl);//控件名为LACEHOLDER1 的控件内加入刚才定义的控件。
以上是关于ASP.NET用户自定义控件的主要内容,如果未能解决你的问题,请参考以下文章
asp.net怎么获取页面动态加载进来的用户自定义控件里面的值。。