您的位置:首页 > 移动开发 > Objective-C

GridView动态的绑定和显示功能

2012-03-26 22:17 288 查看
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 → 转载请注明文章出自雪頔网
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐