【ASP.NET】 GriedView 增删改查+ 自带分页
题外话:
自己平时的话 会经常在项目中用到GriedView 每次建立的时候就要拖控件,手工添加列比较复杂,在这做次笔记,方便以后快速使用
前台--->
View Code
<asp:GridView ID="gridProgram" runat="server" AutoGenerateColumns="false" AllowPaging="true" PageSize="10" OnPageIndexChanging="gridProgram_PageIndexChanging" onrowcommand="gridProgram_RowCommand"> <Columns> <asp:TemplateField HeaderText="序号" HeaderStyle-Wrap="false" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#Container.DataItemIndex+1 %> <%--<%# (this.AspNetPager1.CurrentPageIndex - 1) * this.AspNetPager1.PageSize + Container.DataItemIndex + 1%> --%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="项目名称" HeaderStyle-Wrap="false" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#Eval("ProgramName")%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="部门" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#Eval("ProgramSector")%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="创建日期" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#((DateTime)Eval("ProgramDate")).ToString("yyyy-MM-dd")%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="负责人" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#Eval("Charger")%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="项目阶段" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <%#DecideStep(Eval("Step").ToString()) %><%--DecideStep是后台写的方法用于外键的汉字显示 --%> </ItemTemplate> </asp:TemplateField> <%--<asp:TemplateField HeaderText="备注"> <ItemTemplate> <%#((DateTime)Eval("CreateTime")).ToString("yyyy-MM-dd HH:mm:ss")%> </ItemTemplate> </asp:TemplateField>--%> <asp:TemplateField HeaderText="操作" HeaderStyle-Wrap="false" ItemStyle-Wrap="false" ItemStyle-HorizontalAlign="Center"> <ItemTemplate> <asp:HiddenField ID="hidProgramId" Value="Eval('ProgramID')" runat="server" /> <asp:Button ID="btnRead" runat="server" CommandArgument='<%#Eval("ProgramID") %>' Text="读 取" CommandName="Read" /> <asp:Button ID="btnDelete" runat="server" Text="删 除" CommandArgument='<%#Eval("ProgramID") %>' CommandName="Del" /> <%--<asp:HyperLink ID="linkPreview" runat="server" Text="读取" NavigateUrl='<%#DealURL(Convet.Eval("ProgramID")%>'></asp:HyperLink>--%> <%--<asp:HyperLink ID="linkDelete" runat="server" Text="删除" NavigateUrl='<%#"DeleteNews.aspx?newsId="+Eval("ProgramID")%>'></asp:HyperLink>--%> </ItemTemplate> </asp:TemplateField> </Columns> <%--下面的分页基本不用修改 --%> <PagerTemplate> 当前第: <asp:Label ID="LabelCurrentPage" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageIndex + 1 %>"></asp:Label> 页/共: <asp:Label ID="LabelPageCount" runat="server" Text="<%# ((GridView)Container.NamingContainer).PageCount %>"></asp:Label> 页 <asp:LinkButton ID="LinkButtonFirstPage" runat="server" CommandArgument="First" CommandName="Page" Visible='<%#((GridView)Container.NamingContainer).PageIndex != 0 %>'>首页</asp:LinkButton> <asp:LinkButton ID="LinkButtonPreviousPage" runat="server" CommandArgument="Prev" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'>上一页</asp:LinkButton> <asp:LinkButton ID="LinkButtonNextPage" runat="server" CommandArgument="Next" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>下一页</asp:LinkButton> <asp:LinkButton ID="LinkButtonLastPage" runat="server" CommandArgument="Last" CommandName="Page" Visible='<%# ((GridView)Container.NamingContainer).PageIndex != ((GridView)Container.NamingContainer).PageCount - 1 %>'>尾页</asp:LinkButton> 转到第 <asp:TextBox ID="txtNewPageIndex" runat="server" Width="20px" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>' />页 <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-2" CommandName="Page" Text="GO" /> </PagerTemplate> </asp:GridView>
后台cs文件[部分核心代码]-->
View Code
//删除和跳转等(操作) protected void gridProgram_RowCommand(object sender, GridViewCommandEventArgs e) { //注:e.CommandArgument.ToString() 为每一行的索引号即ID switch (e.CommandName) { case "Read": //编写代码 break; case "Del": //删除代码 ClientScriptFunction.MessageBox("删除项目成功!"); //重新绑定数据 BindGrvData(); break; } } //后台对gridview分页进行设置 (机会不用改 只修改绑定数据) protected void gridProgram_PageIndexChanging(object sender, GridViewPageEventArgs e) { GridView theGrid = sender as GridView; int newPageIndex = 0; if (e.NewPageIndex == -3) { //点击了Go按钮 TextBox txtNewPageIndex = null; //GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow GridViewRow pagerRow = theGrid.BottomPagerRow; if (pagerRow != null) { //得到text控件 txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox; } if (txtNewPageIndex != null) { //得到索引 newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; } } else { //点击了其他的按钮 newPageIndex = e.NewPageIndex; } //防止新索引溢出 newPageIndex = newPageIndex < 0 ? 0 : newPageIndex; newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex; //得到新的值 theGrid.PageIndex = newPageIndex; //重新绑定 BindGrvData(); }
Ps:我在前台定义的是每页10个,这个可以自行修改
至于添加的话 很简单就不贴出来了、
Ending...
希望对大家有所帮助。
Write By--Ruicky
作者:ruicky
出处:http://www.cnblogs.com/ruicky/
欢迎任何形式的转载,未经作者同意,请保留此段声明!