GridView动态的绑定和显示功能


Default.aspx页面 代码

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script language ="javascript" type ="text/javascript" >
function GetAllCheckBox(CheckAll)
{
var items=document.getElementsByTagName ("input");
for(i=0;i<items.length;i++)
{
if(items[i].type=="checkbox")
{
items[i].checked= CheckAll.checked;
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="gvLogin" runat="server" AllowPaging="True" AllowSorting="True"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" Height="110px"
PageSize="2" Style="position: relative"
Width="376px" OnPageIndexChanging="gvLogin_PageIndexChanging" OnRowDataBound="gvLogin_RowDataBound">
<FooterStyle BackColor="White" ForeColor="#000066" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#000066" HorizontalAlign="Left" BackColor="White" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerSettings FirstPageText="首页" LastPageText="末页" Mode="NextPreviousFirstLast"
NextPageText="下一页" PreviousPageText="前一页" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<input id="chkAll" style="position: relative" type="checkbox" onclick ="GetAllCheckBox(this)" />全选
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Style="position: relative" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

</div>
</form>
</body>
</html>

后台对应类Default.aspx.cs代码

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
private static DataTable dt;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//GridView的动态绑定
GridViewDataBind();

//设置GridView密码列的值
SetPwdValue();

//设置GridView标题内容
SetGridViewHeaderText();
}
}

//GridView的动态绑定
private void GridViewDataBind()
{
//构建数据源

//***************************************
dt = new DataTable("login");
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("pwd", typeof(string));

dt.Rows.Add(new object[] { 1, "mike", "mike@163.com" });
dt.Rows.Add(new object[] { 2, "meimei", "meimei@163.com" });
dt.Rows.Add(new object[] { 3, "jacky", "jacky@163.com" });
dt.Rows.Add(new object[] { 4, "nannan", "nannan@163.com" });
dt.Rows.Add(new object[] { 5, "lili", "lili@163.com" });
//*******************************

//星号内可以替换成其他数据获得方式,这里只是为了例子而编写的假数据
//绑定GridView
this.gvLogin.DataSource = dt;
this.gvLogin.DataBind();
}

//设置GridView标题内容
private void SetGridViewHeaderText()
{
this.gvLogin.HeaderRow.Cells[1].Text = "编号";
this.gvLogin.HeaderRow.Cells[2].Text = "姓名";
this.gvLogin.HeaderRow.Cells[3].Text = "密码";
}

//设置GridView密码列的值
private void SetPwdValue()
{
for (int i = 0; i < this.gvLogin.Rows.Count; i++)
{
this.gvLogin.Rows[i].Cells[3].Text = "******";
}
}

//GridView的分页处理
protected void gvLogin_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
this.gvLogin .PageIndex =  e.NewPageIndex;
//GridView的动态绑定
GridViewDataBind();
//设置GridView密码列的值
SetPwdValue();
//设置GridView标题内容
SetGridViewHeaderText();
}

//GridView的光棒效果
protected void gvLogin_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row .RowType ==DataControlRowType .DataRow )
{
e.Row .Attributes.Add ("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
}
}
}


本文链接: http://www.snowdi.com/627.html → 转载请注明文章出自 雪頔网
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值