GridView用法详解
发布时间:2020-12-16 08:59:32 所属栏目:asp.Net 来源:网络整理
导读:前台页面: Default.aspx 1 %@ Page Language= " C# " AutoEventWireup= true " CodeFile= Default.aspx.cs " Inherits= _Default " % 2 3 !DOCTYPE html 4 5 html xmlns= http://www.w3.org/1999/xhtml " 6 head runat= server 7 meta http-equiv= Content-
1 <%@ Page Language="C#" AutoEventWireup=true" CodeFile=Default.aspx.cs" Inherits=_Default" %> 2 3 <!DOCTYPE html> 4 5 <html xmlns=http://www.w3.org/1999/xhtml"> 6 <head runat=server 7 <meta http-equiv=Content-Type" content=text/html; charset=utf-8"/> 8 <title>GridView用法</title> 9 </head> 10 <body> 11 <form id=form1" runat=12 <div> 13 <asp:GridView ID=gvUserInfo" AllowPaging=True" PageSize=4" OnSorting=gvUserInfo_Sorting" AllowSorting=" AutoGenerateEditButton=" OnRowDataBound=gvUserInfo_RowDataBound" OnRowEditing=gvUserInfo_RowEditing" OnRowUpdating=gvUserInfo_RowUpdating" OnRowCancelingEdit=gvUserInfo_RowCancelingEdit" OnPageIndexChanging=gvUserInfo_PageIndexChanging" OnRowDeleting=gvUserInfo_RowDeleting" EnableModelValidation=" CellPadding=" ForeColor=#333333" GridLines=None" > 14 <AlternatingRowStyle BackColor=White#284775" /> 15 <Columns> 16 <%-- !!! DataNavigateUrlFields属性是获取或设置数据源中字段的名称,用于为其超链接构造URL,其字段名称应为GridView中的数据字段名 17 --%> 18 19 <asp:HyperLinkField NavigateUrl=Info.aspx" DataNavigateUrlFields=用户编号" DataNavigateUrlFormatString=Info.aspx?userId={0}" Target=_blank" Text=详细信息20 <asp:TemplateField> 21 <ItemTemplate> 22 <asp:Button ID=btnDelete" CommandName=Delete" Text=删除" CausesValidation=false" OnClientClick=return confirm('确定删除?')" > 23 </asp:Button> 24 </ItemTemplate> 25 </asp:TemplateField> 26 </Columns> 27 <EditRowStyle BackColor=#99999928 <FooterStyle BackColor=#5D7B9D" Font-Bold=29 <HeaderStyle BackColor=30 <PagerStyle BackColor=" HorizontalAlign=Center31 <RowStyle BackColor=#F7F6F332 <SelectedRowStyle BackColor=#E2DED633 </asp:GridView> 34 </div> 35 </form> 36 </body> 37 </html> ? ? info.aspx info.aspx.csinfo 8 <title>用户详细信息</title> 13 <asp:Table runat=" Caption=用户信息14 <asp:TableRow> 15 <asp:TableCell>用户编号:</asp:TableCell> 16 <asp:TableCell> 17 <asp:Label ID=lblUserId""></asp:Label></asp:TableCell> 18 </asp:TableRow> 19 <asp:TableRow> 20 <asp:TableCell>性别:</asp:TableCell> 21 <asp:TableCell><asp:Label ID=lblSex22 </asp:TableRow> 23 <asp:TableRow> 24 <asp:TableCell>邮箱:</asp:TableCell> 25 <asp:TableCell><asp:Label ID=lblMail26 </asp:TableRow> 27 </asp:Table> 28 <asp:Button ID=btnExit关闭窗口" OnClientClick=javascript:window.opener=null;window.close();29 </div> 30 </form> 31 </body> 32 </html> ? ?
1 using System; 2 System.Collections.Generic; 3 System.Web; 4 System.Web.UI; 5 System.Web.UI.WebControls; 6 System.Data; 7 System.Data.SqlClient; 8 9 public partial class _Default : System.Web.UI.Page 10 { 11 protected void Page_Load(object sender,EventArgs e) 12 { 13 if (!IsPostBack) 14 { 15 ViewState[SortOrder"] = "; 16 ViewState[OrderDirDESC 17 dataBind(); 18 } 19 } 20 21 /// <summary> 22 /// 绑定数据库中的数据到GridView控件中 23 </summary> 24 void dataBind() 25 26 string conStr = System.Configuration.ConfigurationManager.ConnectionStrings[ConStr].ToString(); 27 SqlConnection conn = new SqlConnection(conStr); 28 if (conn.State == ConnectionState.Closed) 29 30 conn.Open(); 31 32 // string strSql = "select userId,userName from tabUserInfo"; 33 string strSql = select userId as 用户编号,userName as 用户名 from tabUserInfo 34 SqlDataAdapter da = SqlDataAdapter(strSql,conn); 35 DataSet ds = DataSet(); 36 da.Fill(ds,tabUserInfo); 37 38 string sort = (string)ViewState["] + " " + (]; 39 DataView view = ds.Tables[].DefaultView; 40 view.Sort = sort; 41 42 gvUserInfo.DataSource = view; 43 gvUserInfo.DataKeyNames = new string[]{}; 44 gvUserInfo.DataBind(); 45 46 对特定数据用特定的显示方式 47 for (int i = 0; i < gvUserInfo.Rows.Count; i++) 48 49 DataRowView myDrv = ds.Tables[].DefaultView[i]; 50 string id = myDrv[ 51 if (Convert.ToInt32(id) < 5 52 { 53 gvUserInfo.Rows[i].Cells[4].BackColor = System.Drawing.Color.Red; 54 } 55 56 ConnectionState.Open) 57 58 conn.Close(); 59 60 61 62 63 实现分页功能 64 65 <param name="sender"></param> 66 <param name="e"></param> 67 void gvUserInfo_PageIndexChanging( 68 69 gvUserInfo.PageIndex = e.NewPageIndex; 70 dataBind(); 71 72 73 74 75 删除GridView中数据 76 77 78 79 void gvUserInfo_RowDeleting( 80 81 SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[].ToString()); 82 delete from tabUserInfo where userId=" +gvUserInfo.DataKeys[e.RowIndex].Value.ToString()+ "" 83 conn.Open(); 84 SqlCommand cmd = SqlCommand(strSql,1)"> 85 if (cmd.ExecuteNonQuery() > 0 86 Response.Write(<script>alert('删除成功!')</script> 87 else 88 Response.Write(<script>alert('删除失败!')</script> 89 conn.Close(); 90 91 92 93 编辑GridView中的数据 94 95 96 97 void gvUserInfo_RowEditing( 98 99 gvUserInfo.EditIndex = e.NewEditIndex; 100 101 102 103 更改数据并提交到数据库 104 105 106 107 void gvUserInfo_RowUpdating(108 109 110 SqlConnection conn = 111 update tabUserInfo set userName='" + ((TextBox)(gvUserInfo.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString().Trim() + ' where userId=" + gvUserInfo.DataKeys[e.RowIndex].Value.ToString() + 112 // 113 114 SqlCommand cmd = 115 cmd.ExecuteNonQuery(); 116 Response.Write(<script>alert('更改成功!')</script>117 118 gvUserInfo.EditIndex = -1119 120 121 122 取消对GridView中数据的编辑 123 124 125 126 void gvUserInfo_RowCancelingEdit(127 128 gvUserInfo.EditIndex = -129 130 131 132 RowDataBound事件 133 134 135 136 void gvUserInfo_RowDataBound(137 138 高亮显示鼠标指定行数据 139 if (e.Row.RowType == DataControlRowType.DataRow) 140 141 e.Row.Attributes.Add(onMouSEOver",1)">Color=this.style.backgroundColor;this.style.backgroundColor='lightblue'142 e.Row.Attributes.Add(onMouSEOutthis.style.backgroundColor=Color;143 144 145 146 排序代码 147 148 149 150 void gvUserInfo_Sorting(151 152 string strPage = e.SortExpression; 153 if (ViewState["].ToString() == strPage) 154 155 "].ToString() == 156 157 ViewState[ASC158 159 160 161 ViewState[162 163 164 165 166 ViewState["] =167 168 169 170 } ? ? info.aspx.cs 1 2 3 4 5 6 7 8 9 info : System.Web.UI.Page 10 11 12 13 14 15 16 17 19 20 21 string conStr=System.Configuration.ConfigurationManager.ConnectionStrings[22 SqlConnection conn = 23 24 25 26 27 select * from tabUserInfo where userId="+Request[userId"].ToString()+;28 SqlDataAdapter da = 29 DataSet ds = 30 da.Fill(ds,1)">tabInfo31 DataRowView rowView = ds.Tables["].DefaultView[32 lblUserId.Text = Convert.ToString(rowView[]); 33 lblSex.Text = Convert.ToString(rowView[userSex34 lblMail.Text = Convert.ToString(rowView[userMail35 36 37 38 39 40 41 } ? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-core – 运行kpm restore时无法找到Microsoft.AspN
- asp.net – 未捕获TypeError:无法读取未定义的属性“UI”
- asp-classic – 通过object标签和Server.CreateObject声明连
- asp.net-mvc – Basic Umbraco 6.1.1 SurfaceController问题
- 如何在asp.net中使用Google Chart API创建图表?
- asp.net – 模型,ViewModels,MVC 3应用程序中的DTO
- ASP.NET:公开Web用户控件的控件
- ASP.NET Web.Config转换问题
- asp.net – 使用Azure redis.cache跨负载均衡服务存储MVC应
- asp.net-mvc – 在我正在运行的Visual Studio 2013中的另一