类似代码都差不多,记录一下,便于以后查看使用。
前台页面:
<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> <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> <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; }}
}
}
}