GridView用法.docx

上传人:b****6 文档编号:4409037 上传时间:2022-12-01 格式:DOCX 页数:11 大小:17.77KB
下载 相关 举报
GridView用法.docx_第1页
第1页 / 共11页
GridView用法.docx_第2页
第2页 / 共11页
GridView用法.docx_第3页
第3页 / 共11页
GridView用法.docx_第4页
第4页 / 共11页
GridView用法.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

GridView用法.docx

《GridView用法.docx》由会员分享,可在线阅读,更多相关《GridView用法.docx(11页珍藏版)》请在冰豆网上搜索。

GridView用法.docx

GridView用法

GridView用法详解

1.前台页面:

Default.aspx

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

DOCTYPEhtml>

//www.w3.org/1999/xhtml">

GridView用法

GridViewID="gvUserInfo"runat="server"AllowPaging="True"PageSize="4"OnSorting="gvUserInfo_Sorting"AllowSorting="true"AutoGenerateEditButton="true"OnRowDataBound="gvUserInfo_RowDataBound"OnRowEditing="gvUserInfo_RowEditing"OnRowUpdating="gvUserInfo_RowUpdating"OnRowCancelingEdit="gvUserInfo_RowCancelingEdit"OnPageIndexChanging="gvUserInfo_PageIndexChanging"OnRowDeleting="gvUserInfo_RowDeleting"EnableModelValidation="True"CellPadding="4"ForeColor="#333333"GridLines="None">

<%--!

DataNavigateUrlFields属性是获取或设置数据源中字段的名称,用于为其超链接构造URL,其字段名称应为GridView中的数据字段名

--%>

HyperLinkFieldNavigateUrl="Info.aspx"DataNavigateUrlFields="用户编号"DataNavigateUrlFormatString="Info.aspx?

userId={0}"Target="_blank"Text="详细信息"/>

TemplateField>

ButtonID="btnDelete"runat="server"CommandName="Delete"Text="删除"CausesValidation="false"OnClientClick="returnconfirm('确定删除?

')">

Button>

TemplateField>

GridView>

info.aspx

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="info.aspx.cs"Inherits="info"%>

DOCTYPEhtml>

//www.w3.org/1999/xhtml">

用户详细信息

Tablerunat="server"Caption="用户信息">

TableRow>

TableCell>用户编号:

TableCell>

TableCell>

LabelID="lblUserId"runat="server"Text="">

Label>

TableCell>

TableRow>

TableRow>

TableCell>性别:

TableCell>

TableCell>

LabelID="lblSex"runat="server"Text="">

Label>

TableCell>

TableRow>

TableRow>

TableCell>邮箱:

TableCell>

TableCell>

LabelID="lblMail"runat="server"Text="">

Label>

TableCell>

TableRow>

Table>

ButtonID="btnExit"runat="server"Text="关闭窗口"OnClientClick="javascript:

window.opener=null;window.close();"/>

2.后台页面:

Default.aspx.cs

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Web;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Data;

usingSystem.Data.SqlClient;

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

ViewState["SortOrder"]="用户编号";

ViewState["OrderDir"]="DESC";

dataBind();

}

}

///

///绑定数据库中的数据到GridView控件中

///

protectedvoiddataBind()

{

stringconStr=System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ToString();

SqlConnectionconn=newSqlConnection(conStr);

if(conn.State==ConnectionState.Closed)

{

conn.Open();

}

//stringstrSql="selectuserId,userNamefromtabUserInfo";

stringstrSql="selectuserIdas用户编号,userNameas用户名fromtabUserInfo";

SqlDataAdapterda=newSqlDataAdapter(strSql,conn);

DataSetds=newDataSet();

da.Fill(ds,"tabUserInfo");

stringsort=(string)ViewState["SortOrder"]+""+(string)ViewState["OrderDir"];

DataViewview=ds.Tables["tabUserInfo"].DefaultView;

view.Sort=sort;

gvUserInfo.DataSource=view;

gvUserInfo.DataKeyNames=newstring[]{"用户编号"};

gvUserInfo.DataBind();

//对特定数据用特定的显示方式

for(inti=0;i

{

DataRowViewmyDrv=ds.Tables["tabUserInfo"].DefaultView[i];

stringid=myDrv["用户编号"].ToString();

if(Convert.ToInt32(id)<5)

{

gvUserInfo.Rows[i].Cells[4].BackColor=System.Drawing.Color.Red;

}

}

if(conn.State==ConnectionState.Open)

{

conn.Close();

}

}

///

///实现分页功能

///

///

///

protectedvoidgvUserInfo_PageIndexChanging(objectsender,GridViewPageEventArgse)

{

gvUserInfo.PageIndex=e.NewPageIndex;

dataBind();

}

 

///

///删除GridView中数据

///

///

///

protectedvoidgvUserInfo_RowDeleting(objectsender,GridViewDeleteEventArgse)

{

SqlConnectionconn=newSqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ToString());

stringstrSql="deletefromtabUserInfowhereuserId="+gvUserInfo.DataKeys[e.RowIndex].Value.ToString()+"";

conn.Open();

SqlCommandcmd=newSqlCommand(strSql,conn);

if(cmd.ExecuteNonQuery()>0)

Response.Write("");

else

Response.Write("");

conn.Close();

dataBind();

}

///

///编辑GridView中的数据

///

///

///

protectedvoidgvUserInfo_RowEditing(objectsender,GridViewEditEventArgse)

{

gvUserInfo.EditIndex=e.NewEditIndex;

dataBind();

}

///

///更改数据并提交到数据库

///

///

///

protectedvoidgvUserInfo_RowUpdating(objectsender,GridViewUpdateEventArgse)

{

stringconStr=System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ToString();

SqlConnectionconn=newSqlConnection(conStr);

stringstrSql="updatetabUserInfosetuserName='"+((TextBox)(gvUserInfo.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim()+"'whereuserId="+gvUserInfo.DataKeys[e.RowIndex].Value.ToString()+"";

//

conn.Open();

SqlCommandcmd=newSqlCommand(strSql,conn);

cmd.ExecuteNonQuery();

Response.Write("");

conn.Close();

gvUserInfo.EditIndex=-1;

dataBind();

}

///

///取消对GridView中数据的编辑

///

///

///

protectedvoidgvUserInfo_RowCancelingEdit(objectsender,GridViewCancelEditEventArgse)

{

gvUserInfo.EditIndex=-1;

dataBind();

}

///

///RowDataBound事件

///

///

///

protectedvoidgvUserInfo_RowDataBound(objectsender,GridViewRowEventArgse)

{

//高亮显示鼠标指定行数据

if(e.Row.RowType==DataControlRowType.DataRow)

{

e.Row.Attributes.Add("onMouseOver","Color=this.style.backgroundColor;this.style.backgroundColor='lightblue'");

e.Row.Attributes.Add("onMouseOut","this.style.backgroundColor=Color;");

}

}

///

///排序代码

///

///

///

protectedvoidgvUserInfo_Sorting(objectsender,GridViewSortEventArgse)

{

stringstrPage=e.SortExpression;

if(ViewState["SortOrder"].ToString()==strPage)

{

if(ViewState["OrderDir"].ToString()=="DESC")

{

ViewState["OrderDir"]="ASC";

}

else

{

ViewState["OrderDir"]="DESC";

}

}

else

{

ViewState["SortOrder"]=e.SortExpression;

}

dataBind();

}

}

info.aspx.cs

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Web;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Data;

usingSystem.Data.SqlClient;

publicpartialclassinfo:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

dataBind();

}

}

protectedvoiddataBind()

{

stringconStr=System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ToString();

SqlConnectionconn=newSqlConnection(conStr);

if(conn.State==ConnectionState.Closed)

{

conn.Open();

}

stringstrSql="select*fromtabUserInfowhereuserId="+Request["userId"].ToString()+";";

SqlDataAdapterda=newSqlDataAdapter(strSql,conn);

DataSetds=newDataSet();

da.Fill(ds,"tabInfo");

DataRowViewrowView=ds.Tables["tabInfo"].DefaultView[0];

lblUserId.Text=Convert.ToString(rowView["userId"]);

lblSex.Text=Convert.ToString(rowView["userSex"]);

lblMail.Text=Convert.ToString(rowView["userMail"]);

if(conn.State==ConnectionState.Open)

{

conn.Close();

}

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 初中教育

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1