位置:海鸟网 > IT > ASP.NET >

自己做的数据绑定控件

很久都没有写一点东西了,最近一直在学习.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 cellpadding='0' cellspacing='1' bgcolor='#000000'>" + 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 cellpadding='0' cellspacing='1' bgcolor='#000000'>\n" +
"<tr bgcolor='#FFFFFF'>\n" + "<td valign='top'>{0}</td>\n" + "</tr>\n" +
"<tr bgcolor='#FFFFFF'>\n" + "<td 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 cellpadding='0' cellspacing='1' bgcolor='#000000'>" + tableHead + tableBody + "</table>";

return table;
}


/// <summary>
/// 创建分页控件
/// </summary>
/// <returns>返回分页控件的Html表示字符串</returns>
protected string CreatePageControl()
{
string leftInfo = string.Format("页:{0}/{1}"+"  "+"每页{2}条"+"  "+"共{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 = " ";
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>" + 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'><a href={0}>7</a><a href={1}>3</a></font>{2}<font face='Webdings'><a href={3}>4</a><a href={4}>8</a></font>",first,prev,numberStr,next,last);

string result = string.Format("<table cellpadding='0' cellspacing='0'>\n" +
"<tr><td>{0}</td>\n" +
"<td>{1}</td>" +
"<td><input type='text' size='4'> \n <input type='button' size='6' value=go ></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
}


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