王朝网络
分享
 
 
 

自己做的数据绑定控件

王朝other·作者佚名  2006-01-09
宽屏版  字体: |||超大  

很久都没有写一点东西了,最近一直在学习.net,前两天看到椰子林写的一篇《ASP.NET分页组件学与用》,于是自己就跟着做了一遍,一次成功,在此向他表示感谢,也向他那种共享的精神致敬!可是后来我发觉这个组件用起来有点麻烦,在Page_Load里面不但要得到记录集,还要写SQL语句分页,最后还要自己写代码将包含数据的<table></table>输出到客户端,于是我就想呀要是可以像DataGrid那样只是简单的绑定一下就可以用就好,分页,显示数据呀这些都交给组件去完成,正是这灵光一现,我就开始冲动了,没办法程序就是有这么大的魅力!结果,我昨天晚上失眠了,哎!冲动的惩罚呀!今天早上,我带着红肿的眼睛走进了机房,开始实现我昨天晚上梦见的那个东东,幸运的是--我实现了,呵呵,一个字爽!忘了告诉大家,我还是一个学生,做出来我很高兴了,技巧谈不上,高手们看了莫怪。下面我把基本的做法说一下!

如何做自定义控件,以及如何实现分页在这里我就不多说了,大家可以看一下椰子林写的相关文章,我要说说我的做法:

首先定义一个DataSource属性,如下:

private DataTable dt; //数据源

/// <summary>

/// 数据源

/// </summary>

public DataTable DataSource

{

get

{

return dt;

}

set

{

if (value == null)

throw new Exception("数据源不可为空");

else

dt = value;

}

}

该属性用于接受前端传进来的DataTable对象。我们可以在前端使用ADO.NET获得数据并将数据填充到一个DataTable中,再将此包含数据的DataTable赋于组件的DataSource属性,接下来的工作就是由组件向客户端输出包含数据的<table></table>标签了,怎么样简单吧!其实没有做多少改进,只是简单的扩展了一下,如下:

/// <summary>

/// 创建数据表

/// </summary>

/// <returns>表格的Html表示字符串</returns>

protected string CreateDataTable()

{

string table = null; //表格,显示了所有的字段和记录

string tableHead = null; // 表头用于显示字段名的<tr>

string tdHead = null; // 表头用于显示字段名的<td>包含在tableHead中

string tableBody = null; // 表主体用于显示记录的<tr>

// 为了实现分页定义min和max两个变量

// min代表从那一条记录开始显示

// max代表到那一条记录结束

// 如果当前是第2页每页显示10条记录的话,那么min的值就是10,max的值就是20

int min = 0;

int max = 0;

// for循环用于产生表头,即字段名

for (int i = 0; i < this.dt.Columns.Count; i++)

{

tdHead = tdHead + "<td>" + this.dt.Columns[i].ColumnName + "</td>";

}

// 判断当前页是否最后一页

if (this.currentpage == this.PageCount)

{

min = (this.currentpage - 1) * this.count;

max = this.dt.Rows.Count;

}

else

{

min = (this.currentpage - 1) * this.count;

max = this.currentpage * this.count;

}

// for循环用于产生表主体,即提取记录

for (int j = min; j < max; j++)

{

tableBody = tableBody + "<tr bgcolor='#FFFFFF'>";

for (int k = 0; k < this.dt.Columns.Count; k++)

{

tableBody = tableBody + "<td>" + this.dt.Rows[j][k].ToString() + "</td>";

}

tableBody = tableBody + "</tr>";

}

tableHead = "<tr bgcolor='#FFFFFF'>" + tdHead + "</tr>";

table = "<table border='0' cellpadding='0' cellspacing='1' width='100%' height='100%' bgcolor='#000000' style='font-size:9pt'>" + tableHead + tableBody + "</table>";

return table;

}

这样就得到了包含数据的表格的Html字符串,跟着就是做分页部分,最后重写Render(HtmlTextWriter output)方法将整个组件输出到客户端!这里我不一一详述步骤,我把代码帖出来,有兴趣的可以将代码拷贝到本机上运行一下!

组件源代码如下:(调试通过)

public class MyDataGrid : System.Web.UI.WebControls.WebControl

{

private int currentpage; //当前页

private int count; //每页显示的记录条数

private int navigcount; //导航连接的个数

private DataTable dt; //数据源

private const string SCRIPTSTRING = "<script language='javascript'>\n" +

" function go(ctrl,max)\n" +

"{\n" +

"if(ctrl.value >= 1 && ctrl.value <= max)\n" +

"{\n" +

"var url;\n" +

"var index;\n" +

"url = location.href;\n" +

"index = url.indexOf('?');\n" +

"if(index == -1)\n" +

"{\n" +

"}\n" +

"else\n" +

"{\n" +

"url = url.substring(0,index);\n" +

"}\n" +

"location.href = url + '?CurrentPage=' + ctrl.value;\n" +

"}\n" +

"else\n" +

"{\n" +

"alert('您输入的页码必须是符合页面要求的数字,最大值是:' + max);\n" +

"return false;\n" +

"}\n" +

"}\n" +

"</script>\n";

/// <summary>

/// 当前页

/// </summary>

public int CurrentPage

{

get

{

return currentpage;

}

set

{

if (value <= 0)

currentpage = 1;

else

currentpage = value;

}

}

/// <summary>

/// 每页显示的记录条数

/// </summary>

public int Count

{

get

{

return count;

}

set

{

if (value <= 0)

count = 10;

else

count = value;

}

}

/// <summary>

/// 导航连接的个数

/// </summary>

public int NavigCount

{

get

{

return navigcount;

}

set

{

if (value <= 0)

navigcount = 10;

else

navigcount = value;

}

}

/// <summary>

/// 总页数

/// </summary>

public int PageCount

{

get

{

if (this.dt.Rows.Count % count > 0)

return ((int)this.dt.Rows.Count / count) + 1;

else

return ((int)this.dt.Rows.Count / count);

}

}

/// <summary>

/// 数据源

/// </summary>

public DataTable DataSource

{

get

{

return dt;

}

set

{

if (value == null)

throw new Exception("数据源不可为空");

else

dt = value;

}

}

protected override void Render(HtmlTextWriter output)

{

string Table = CreateDataTable();

string PageControl = CreatePageControl();

string Result = string.Format("<table border='0' cellpadding='0' cellspacing='1' width='724' height='93' bgcolor='#000000' style='font-size: 9pt'>\n" +

"<tr bgcolor='#FFFFFF'>\n" + "<td width='724' height='68' valign='top'>{0}</td>\n" + "</tr>\n" +

"<tr bgcolor='#FFFFFF'>\n" + "<td width='724' height='25' valign='top'>{1}</td>\n" + "</tr>\n" + "</table>\n",Table.ToString(),PageControl.ToString());

output.Write(Result.ToString());

}

/// <summary>

/// 创建数据表

/// </summary>

/// <returns>表格的Html表示字符串</returns>

protected string CreateDataTable()

{

string table = null; //表格,显示了所有的字段和记录

string tableHead = null; // 表头用于显示字段名的<tr>

string tdHead = null; // 表头用于显示字段名的<td>包含在tableHead中

string tableBody = null; // 表主体用于显示记录的<tr>

// 为了实现分页定义min和max两个变量

// min代表从那一条记录开始显示

// max代表到那一条记录结束

// 如果当前是第2页每页显示10条记录的话,那么min的值就是10,max的值就是20

int min = 0;

int max = 0;

// for循环用于产生表头,即字段名

for (int i = 0; i < this.dt.Columns.Count; i++)

{

tdHead = tdHead + "<td>" + this.dt.Columns[i].ColumnName + "</td>";

}

// 判断当前页是否最后一页

if (this.currentpage == this.PageCount)

{

min = (this.currentpage - 1) * this.count;

max = this.dt.Rows.Count;

}

else

{

min = (this.currentpage - 1) * this.count;

max = this.currentpage * this.count;

}

// for循环用于产生表主体,即提取记录

for (int j = min; j < max; j++)

{

tableBody = tableBody + "<tr bgcolor='#FFFFFF'>";

for (int k = 0; k < this.dt.Columns.Count; k++)

{

tableBody = tableBody + "<td>" + this.dt.Rows[j][k].ToString() + "</td>";

}

tableBody = tableBody + "</tr>";

}

tableHead = "<tr bgcolor='#FFFFFF'>" + tdHead + "</tr>";

table = "<table border='0' cellpadding='0' cellspacing='1' width='100%' height='100%' bgcolor='#000000' style='font-size:9pt'>" + tableHead + tableBody + "</table>";

return table;

}

/// <summary>

/// 创建分页控件

/// </summary>

/// <returns>返回分页控件的Html表示字符串</returns>

protected string CreatePageControl()

{

string leftInfo = string.Format("页:{0}/{1}"+"&nbsp;&nbsp"+"每页{2}条"+"&nbsp;&nbsp"+"共{3}条",

this.CurrentPage.ToString(),this.PageCount.ToString(),this.Count.ToString(),this.dt.Rows.Count.ToString());

int min = 0;

int max = 0;

if (this.CurrentPage > this.PageCount)

{

this.CurrentPage = this.PageCount;

}

if (this.CurrentPage % this.Count == 0)

{

min = this.CurrentPage + 1;

max = this.CurrentPage + this.Count;

}

else if (this.CurrentPage % this.Count == 1&& this.CurrentPage > this.Count)

{

min = (((int)this.CurrentPage / this.Count) - 1) * this.Count + 1;

max = this.CurrentPage - 1;

}

else

{

min = ((int)this.CurrentPage / this.Count) * this.Count + 1;

max = (((int)this.CurrentPage / this.Count) + 1) * this.Count;

}

string numberStr = "&nbsp;";

string url = this.Context.Request.Url.ToString();

if (url.IndexOf("?") == -1)

{

}

else

{

url = url.Substring(0,url.IndexOf("?"));

}

for (int i = min; i <= max; i++)

{

if (i <= this.PageCount)

{

if (i == this.CurrentPage)

{

numberStr = numberStr + "<a href=" + url + "?CurrentPage=" + i.ToString() + ">" + "<I style='color:red'>" + i.ToString() + "</I>" +"</a>" + "\n";

}

else

{

numberStr = numberStr + "<a href=" + url + "?CurrentPage=" + i.ToString() + ">" + i.ToString() +"</a>" + "\n";

}

}

}

string first,prev,next,last;

first = url + "?CurrentPage=" + 1;

if (this.CurrentPage == 1)

{

prev = url + "?CurrentPage=1";

}

else

{

prev = url + "?CurrentPage=" + (this.CurrentPage - 1).ToString();

}

if (this.CurrentPage == this.PageCount)

{

next = url + "?CurrentPage="+ this.PageCount;

}

else

{

next = url + "?CurrentPage=" + (this.CurrentPage + 1).ToString();

}

last = url + "?CurrentPage=" + this.PageCount;

string centerInfo = string.Format("<font face='Webdings' style='font-size:14px'><a href={0}>7</a><a href={1}>3</a></font>{2}<font face='Webdings' style='font-size:14px'><a href={3}>4</a><a href={4}>8</a></font>",first,prev,numberStr,next,last);

string result = string.Format("<table border='0' cellpadding='0' cellspacing='0' width='100%' style='font-size:9pt'>\n" +

"<tr><td width='25%' align='left'>{0}</td>\n" +

"<td width='61%' align='right'>{1}</td>" +

"<td width='14%' align='right'><input type='text' name='T1' size='4' style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;'> \n <input type='button' name='B1' size='6' value=go style='border-bottom:solid 1pt gray;border-top :solid 1pt gray; border-left:solid 1pt gray;border-right:solid 1pt gray;' onclick='go(T1,{2})'></td>\n" +

"</tr></table>",leftInfo,centerInfo,this.PageCount);

return result;

}

protected override void OnPreRender(EventArgs e)

{

base.OnPreRender (e);

if(!Page.IsClientScriptBlockRegistered("WEREW-332DFAF-FDAFDSFDSAFD"))

{

Page.RegisterClientScriptBlock("WEREW-332DFAF-FDAFDSFDSAFD",SCRIPTSTRING);

}

}

}

测试工程代码:

public class TestMyDataGrid : System.Web.UI.Page

{

protected myControlLibrary.MyDataGrid MyDataGrid1;

private void Page_Load(object sender, System.EventArgs e)

{

// 在此处放置用户代码以初始化页面

int currentpage;

if (this.Request.Params["CurrentPage"] == null)

{

currentpage = 1;

}

else

{

currentpage = Convert.ToInt32(this.Request.Params["CurrentPage"].ToString());

}

this.MyDataGrid1.CurrentPage = currentpage;

this.MyDataGrid1.Count = 10;

this.MyDataGrid1.NavigCount = 10;

SqlConnection conn = new SqlConnection("server=localhost; database=NorthWind; uid=sa");

SqlCommand cmd = new SqlCommand("select * from [Order Details]",conn);

SqlDataAdapter da = new SqlDataAdapter();

da.SelectCommand = cmd;

DataSet ds = new DataSet();

conn.Open();

da.Fill(ds,"table");

conn.Close();

this.MyDataGrid1.DataSource = ds.Tables["table"];

}

#region Web Form Designer generated code

override protected void OnInit(EventArgs e)

{

//

// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。

//

InitializeComponent();

base.OnInit(e);

}

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

this.Load += new System.EventHandler(this.Page_Load);

}

#endregion

}

页面版排的不好,只好劳动一下你,呵呵!

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
>>返回首页<<
推荐阅读
 
 
频道精选
 
静静地坐在废墟上,四周的荒凉一望无际,忽然觉得,凄凉也很美
© 2005- 王朝网络 版权所有