ASP.NET GridView 直接进行数据的修改和分页的实现

ASP.NET GridView的使用,前两天在一个项目中有这样的一个要求,就是GridView加载完数据之后,用户可以直接在页面上进行每一行数据的修改,在修改完之后点击每一行的保存按钮,进行数据的保存。之前也做过,但是已经是很早以前的事了,有点忘记了,于是在网上找寻了一通,最后又做了一个小的DEMO,下面分享给大家,省的以后又忘记了。

之前只是简单的实现了数据的修改,下面修改一下补充一下实现分页的功能:

前台代码:

<asp:GridView
 1 <asp:GridView ID="gd_result" runat="server" AutoGenerateColumns="false" 
 2             AllowPaging="true" PageSize="10" onrowcommand="gd_result_RowCommand" 
 3             DataKeyNames="id" onpageindexchanging="gd_result_PageIndexChanging">
 4             <Columns>
 5                 <asp:TemplateField ShowHeader="false">
 6                     <ItemTemplate>
 7                         <asp:Button ID="btn_save" runat="server" Text="保存数据" CommandArgument='<%# Container.DataItemIndex%>' CommandName="getrow" />
 8                     </ItemTemplate>
 9                 </asp:TemplateField>
10                 <asp:TemplateField HeaderText="ID" Visible="false">
11                     <ItemTemplate>
12                         <asp:TextBox ID="txt_id" runat="server" Text='<%# Eval("id") %>'></asp:TextBox>
13                     </ItemTemplate>
14                 </asp:TemplateField>
15                 <asp:TemplateField HeaderText="Name">
16                     <ItemTemplate>
17                         <asp:TextBox ID="txt_name" runat="server" Text='<%# Eval("name") %>' TextMode="MultiLine" Height="50px"></asp:TextBox>
18                     </ItemTemplate>
19                 </asp:TemplateField>
20                 <asp:TemplateField HeaderText="Age">
21                     <ItemTemplate>
22                         <asp:TextBox ID="txt_age" runat="server" Text='<%# Eval("age") %>'></asp:TextBox>
23                     </ItemTemplate>
24                 </asp:TemplateField>
25             </Columns>
26             <PagerTemplate>
27                 <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label>
28                 页/共:
29                 <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label>
30 31                 <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page"
32                     Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton>
33                 <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev"
34                     CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton>
35                 <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page"
36                     Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton>
37                 <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page"
38                     Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton>
39                 转到第
40                 <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />41                 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2"
42                     CommandName="Page" Text="GO" />
43             </PagerTemplate>
44         </asp:GridView>

说明:在这里GridView中的每一列我们都是用的<asp:Template>。保存按钮列也是,但是在这里需要注意的是需要指定CommandArgument CommandName 两个属性。

CommandArgument的作用是在GridView的|RowCommand事件中来来获取所当前行所对的下标。

CommandName属性主要是在RowCommand的事件中来判断当前触发RowCommand事件的对象是哪个,如果你在GridView中有多个Button Template列的话,那么这个就可以判断你点击的是那列上的Button。

后台代码:

GridView
  1 using System;
  2 using System.Collections;
  3 using System.Configuration;
  4 using System.Data;
  5 using System.Linq;
  6 using System.Web;
  7 using System.Web.Security;
  8 using System.Web.UI;
  9 using System.Web.UI.HtmlControls;
 10 using System.Web.UI.WebControls;
 11 using System.Web.UI.WebControls.WebParts;
 12 using System.Xml.Linq;
 13 using GridViewTest.LocalWebServices;
 14 
 15 namespace GridViewTest
 16 {
 17     public partial class _Default : System.Web.UI.Page
 18     {
 19         protected void Page_Load(object sender, EventArgs e)
 20         {
 21             if (!IsPostBack)
 22             {
 23                 BindData();
 24                 TestWebServices();
 25             }
 26         }
 27 
 28         /// <gd_result_rowCommand>
 29         /// gd_result_rowCommand
 30         /// </gd_result_rowCommand>
 31         /// <param name="sender"></param>
 32         /// <param name="e"></param>
 33         protected void gd_result_RowCommand(object sender, GridViewCommandEventArgs e)
 34         {
 35             if (e.CommandName == "getrow")
 36             {
 37                 Response.Write("当前行号:" + e.CommandArgument);
 38                 string aa= string.Empty;
 39                 int indexnum = Convert.ToInt32(e.CommandArgument);
 40                 if (indexnum >= 10)
 41                 {
 42                     aa = indexnum.ToString().Substring(1);
 43                 }
 44                 else
 45                 {
 46                     aa = indexnum.ToString();
 47                 }
 48                 GridViewRow row = gd_result.Rows[Convert.ToInt32(aa)];
 49                 string id = gd_result.DataKeys[Convert.ToInt32(aa)].Value.ToString();
 50                 string name = ((TextBox)row.FindControl("txt_name")).Text.Trim();
 51                 string age = ((TextBox)row.FindControl("txt_age")).Text.Trim();
 52                 Response.Write("id: " + id + "\n");
 53                 Response.Write("name: " + name+"\n");
 54                 Response.Write("age: " + age+"\n");
 55             }
 56         }
 57 
 58         /// <GetDataTable>
 59         /// GetDataTable
 60         /// </GetDataTable>
 61         /// <returns></returns>
 62         private DataTable GetDataTable()
 63         {
 64             DataTable dt = new DataTable();
 65             dt.Columns.Add("id");
 66             dt.Columns.Add("name");
 67             dt.Columns.Add("age");
 68             for (int i = 0; i < 50; i++)
 69             {
 70                 DataRow row = dt.NewRow();
 71                 row["id"] = i.ToString();
 72                 row["name"] = "name" + i.ToString();
 73                 row["age"] = "age" + i.ToString();
 74                 dt.Rows.Add(row);
 75             }
 76             return dt;
 77         }
 78 
 79         /// <BindData>
 80         /// BindData
 81         /// </BindData>
 82         private void BindData()
 83         {
 84             this.gd_result.DataSource = GetDataTable();
 85             this.gd_result.DataBind();
 86         }
 87 
 88         /// <分页事件>
 89         /// gd_result_PageIndexChanging
 90         /// </分页事件>
 91         /// <param name="sender"></param>
 92         /// <param name="e"></param>
 93         protected void gd_result_PageIndexChanging(object sender, GridViewPageEventArgs e)
 94         {
 95             // 得到该控件
 96             GridView theGrid = sender as GridView;
 97             int newPageIndex = 0;
 98             if (e.NewPageIndex == -3)
 99             {
100                 //点击了Go按钮
101                 TextBox txtNewPageIndex = null;
102 
103                 //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
104                 GridViewRow pagerRow = theGrid.BottomPagerRow;
105 
106                 if (pagerRow != null)
107                 {
108                     //得到text控件
109                     txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
110                 }
111                 if (txtNewPageIndex != null)
112                 {
113                     //得到索引
114                     newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
115                 }
116             }
117             else
118             {
119                 //点击了其他的按钮
120                 newPageIndex = e.NewPageIndex;
121             }
122             //防止新索引溢出
123             newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
124             newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;
125             //得到新的值
126             theGrid.PageIndex = newPageIndex;
127             //重新绑定
128             BindData();
129         }

 

 

posted @ 2012-08-06 23:22  yx_bolg  阅读(2253)  评论(0编辑  收藏  举报