类似代码都差不多,记录一下,便于以后查看使用。

前台页面:

<asp:GridView ID="gdvList" runat="server" AutoGenerateColumns="False" CssClass="table table-striped table-bordered table-hover"
AllowPaging="true" PageSize="10" OnRowDeleting="gdvList_RowDeleting" OnRowCancelingEdit="gdvList_RowCancelingEdit"
OnRowEditing="gdvList_RowEditing" OnRowUpdating="gdvList_RowUpdating" OnRowDataBound="gdvList_RowDataBound"
DataKeyNames="ID" onpageindexchanging="gdvList_PageIndexChanging"
onrowcommand="gdvList_RowCommand">
<Columns>
<asp:BoundField HeaderText="ID" DataField="ID">
<FooterStyle CssClass="hidden" />
<HeaderStyle CssClass="hidden" />
<ItemStyle CssClass="hidden" />
</asp:BoundField>
<asp:TemplateField HeaderText="部门" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:Label ID="lblDeptName" runat="server" Text='<%#Eval("deptname")%>' Width="90%"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDeptName1" runat="server" Text='<%#Eval("deptname")%>' Width="90%"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="150px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="工号" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:Label ID="lblUserNo" runat="server" Text='<%#Eval("userNo")%>' Width="90%"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblUserNo1" runat="server" Text='<%#Eval("userNo")%>' Width="90%"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="150px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="邮箱号" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:Label ID="lblEmail" runat="server" Text='<%#Eval("email")%>' Width="90%"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblEmail1" runat="server" Text='<%#Eval("email")%>' Width="90%"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="150px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="姓名" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:Label ID="lblUserName" runat="server" Text='<%#Eval("userName")%>' Width="90%"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblUserName1" runat="server" Text='<%#Eval("userName")%>' Width="90%"></asp:Label>
</ItemTemplate>
<HeaderStyle Width="150px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="指标" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:TextBox ID="txtTarget" runat="server" Text='<%#Eval("target")%>' Width="90%" />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTarget" runat="server" Text='<%#Eval("target")%>' Width="90%" />
</ItemTemplate>
<HeaderStyle Width="150px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="管理" ItemStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" Text="编辑" CommandName="Edit"></asp:LinkButton>
&nbsp; &nbsp;
<asp:ImageButton ID="lbtnDel" runat="server" OnClientClick="return window.confirm('确认删除?');"
ImageUrl="../Images/action_delete.png" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnUpdate" runat="server" Text="保存" CommandName="Update" CommandArgument='<%#Eval("ID") %>'></asp:LinkButton>
&nbsp; &nbsp;
<asp:LinkButton ID="btnCancle" runat="server" Text="取消" CommandName="Cancel"></asp:LinkButton>
</EditItemTemplate>
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle Width="100px" />
</asp:TemplateField>
</Columns>
<PagerTemplate>
<br />
<asp:Label ID="lblPage" runat="server" Text='<%# "第" + (((GridView)Container.NamingContainer).PageIndex + 1) + "页/共" + (((GridView)Container.NamingContainer).PageCount) + "页" %> '></asp:Label>
<asp:LinkButton ID="lbnFirst" runat="Server" Text="首页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
CommandName="Page" CommandArgument="First"></asp:LinkButton>
<asp:LinkButton ID="lbnPrev" runat="server" Text="上一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != 0 %>'
CommandName="Page" CommandArgument="Prev"></asp:LinkButton>
<asp:LinkButton ID="lbnNext" runat="Server" Text="下一页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Next"></asp:LinkButton>
<asp:LinkButton ID="lbnLast" runat="Server" Text="尾页" Enabled='<%# ((GridView)Container.NamingContainer).PageIndex != (((GridView)Container.NamingContainer).PageCount - 1) %>'
CommandName="Page" CommandArgument="Last"></asp:LinkButton>
到第<asp:TextBox runat="server" ID="inPageNum"></asp:TextBox>页
<asp:LinkButton ID="LinkButton1" CommandName="go" runat="server">Go</asp:LinkButton>
<br />
</PagerTemplate>
</asp:GridView>

后台代码:

protected void gdvList_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gdvList.PageIndex = -1;
GridBind();
}

protected void gdvList_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
string id = this.gdvList.Rows[e.RowIndex].Cells[0].Text;
int n = userAccess.DeleteUserTarget(id);

GridBind();
}
catch
{
}
}

protected void gdvList_RowEditing(object sender, GridViewEditEventArgs e)
{
gdvList.EditIndex = e.NewEditIndex; //GridView编辑项索引等于单击行的索引

GridBind();
}

protected void gdvList_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
string id = (gdvList.Rows[e.RowIndex].FindControl("btnUpdate") as LinkButton).CommandArgument;

string target = ((TextBox)gdvList.Rows[e.RowIndex].FindControl("txtTarget")).Text;
int n = 0;
if (dataAccess.IsDecimal(target)) //更新
n = userAccess.UpdateUserTarget(id, decimal.Parse(target));
if (n > 0)
{
gdvList.EditIndex = -1;
GridBind();
}
}
catch (Exception ex)
{
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message + "')</script>");
}
}

protected void gdvList_RowDataBound(object sender, GridViewRowEventArgs e)
{

}

private void GridBind()
{
DataTable dt = new DataTable();
dt = userAccess.GetUserTarget("");

gdvList.DataSource = dt;
gdvList.DataBind();
}

涉及分页的代码:

protected void gdvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
gdvList.PageIndex = e.NewPageIndex;
GridBind();
TextBox tb = (TextBox)gdvList.BottomPagerRow.FindControl("inPageNum");
tb.Text = (gdvList.PageIndex + 1).ToString();
}
catch { }
}

protected void gdvList_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "go")
{
try
{
TextBox tb = (TextBox)gdvList.BottomPagerRow.FindControl("inPageNum");
int num = int.Parse(tb.Text);
GridViewPageEventArgs ea = new GridViewPageEventArgs(num - 1);
gdvList_PageIndexChanging(null, ea);
}
catch (Exception ex)
{
Response.Write("<script>alert('" + ex.Message + "')</script>");
}
}
}

如果编辑中,需要显示dropdownlist,则需要在gdvPlan_RowDataBound中编写

如:显示部门列表

<asp:TemplateField HeaderText="部门" ItemStyle-HorizontalAlign="Center">
<EditItemTemplate>
<asp:DropDownList ID="ddlDept" runat="server">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblDeptName" runat="server" Text='<%#Eval("deptName")%>' Width="90%"></asp:Label>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>

protected void gdvPlan_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
//保存当前行的deptname的值
string deptname = this.gdvPlan.DataKeys[e.Row.RowIndex]["deptname"].ToString();

//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[4].FindControl("ddlDept");
dropTemp.ClearSelection();

if (dropTemp != null)
{
DataTable dt = dataAccess.GetDeptList();

dropTemp.DataSource = dt;
dropTemp.DataTextField = "deptname";
dropTemp.DataValueField = "deptID";
dropTemp.DataBind();

//到DropDownList中根据au_id的值去找需要设置为选中状态的项目,将其设置为选中
ListItem item = dropTemp.Items.FindByText(deptname);
if (item != null)
{
item.Selected = true;
}

}

}

}
}

GridView 编辑、删除 、分页的更多相关文章

  1. GridView编辑删除操作

    第一种:使用DataSource数据源中自带的编辑删除方法,这样的不经常使用,在这里就不加说明了. 另外一种:使用GridView的三种事件:GridView1_RowEditing(编辑).Grid ...

  2. GridView编辑删除

    A前台代码 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.asp ...

  3. GridView总结二:GridView自带编辑删除更新

    GridView自带编辑删除更新逻辑很简单:操作完,重新绑定.总结总结,防止忘记... 效果图: 前台代码: <%@ Page Language="C#" AutoEvent ...

  4. php+redis实现注册、删除、编辑、分页、登录、关注等功能

    本文实例讲述了php+redis实现注册.删除.编辑.分页.登录.关注等功能.分享给大家供大家参考,具体如下: 主要界面 ​ 连接redis redis.php <?php //实例化 $red ...

  5. gridview 级联删除、dataset

    gridview编辑列(不使用控件绑定数据源)需要如下代码:<asp:GridView ID="GridView1" runat="server" Aut ...

  6. Repeater控件使用(含删除,分页功能)

    Repeater控件使用(含删除,分页功能) 摘自:http://www.cnblogs.com/alanliu/archive/2008/02/25/914779.html 前臺代碼 <%@ ...

  7. editplus批量删除重复行(编辑-删除-删除重复行)

    editplus快速删除重复数据 多行文本,有些行的文字或数据是重复的,该怎么删除重复部分,只留下不重复的部分?很多人对这个问题感到无比头疼,Editplus同样能快速帮你删除数据. 那么,editp ...

  8. 使用GridView自带分页的代码

    关于GridView分页页码的讨论 在GridView中实现分页的效果方法非常easy,仅仅须要在"GridView任务"对话框中进行设置就能够了.在"GridView任 ...

  9. GridView 控制默认分页页码间距 及字体大小

    GridView 控制默认分页页码间距 及字体大小 PagerCss TD A:hover { WIDTH: 20px; COLOR: black; padding-left: 4px; paddin ...

随机推荐

  1. Ice_cream’s world III--2122

    Ice_cream’s world III Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  2. Unity给力插件之Final IK

    Final IK细节: 1.Aim IK:设定一个目标,关节末端始终朝向该目标,一般用来做头部的朝向. 步骤: a.在模型头节点处添加Aim空物体并reset b.给模型添加Aim IK组件,并填上A ...

  3. JavaScript高级程序设计53.pdf

    共有的表单字段方法 每个表单字段都有两个方法:focus()和blur(),其中focus()用于将浏览器焦点设置到表单字段,激活表单字段.可以侦听页面的load事件 EventUtil.addHan ...

  4. yum puppet

    config.gem: Unpacked gem factory_girl-1.3.3 in vendor/gems has no specification file. Run 'rake gems ...

  5. spider爬站极度损耗站点流量

    或许部分站长遇到过这样的情况,Baiduspider对一个网站的抓取频率要远高于新内容产出速度,造成了N多的流量被蜘蛛占用. 这样的情况一般是针对小站,因为大站访问量很大,蜘蛛对服务器的频繁访问不会有 ...

  6. UIAlertController:弹框4步走

    对于大多数的App来说,弹框这种交互方式基本上是必不可少的.在新的iOS系统中(具体版本我忘了),以前的UIAlertView被弃用,而换成了现在的UIAlertController. UIAlite ...

  7. mysql查询随机几条数据(速度快)

    MySql查询随机几条数据 想到了 Max RAND 这几个函数 用以下2种办法都可以实现查询. 速度还行. 几十万数据左右, 没有什么问题. SELECT * FROM `news` WHERE i ...

  8. tcpreplay安装使用

    #Author: ypguo#Data: 2010.4.23#Version:  1.2 增加了修改VLAN tag内容.                 1.1 修改了cygwin下安装的内容    ...

  9. Linux模式设计系列( 内核与应用关联思考)

    http://blog.chinaunix.net/uid/20608849/cid-25333-list-2.html

  10. careercup-数组和字符串1.3

    1.3 给定两个字符串,请编写程序,确定其中一个字符串的字符重新排序后,能否变成另一个字符串. C++实现代码: #include<iostream> #include<map> ...