首页 > 代码库 > asp.net中的DataGridView增删改查加分页

asp.net中的DataGridView增删改查加分页

前台页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StaffManager.aspx.cs" Inherits="adminmaster_StaffManager"
EnableEventValidation="true" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>平台员工允许统计采购数据采购管理</title>
<link href="http://www.mamicode.com/Theme/base.css" rel="stylesheet" type="text/css" />
<link href="http://www.mamicode.com/Theme/style.css" rel="stylesheet" type="text/css" />
<link href="http://www.mamicode.com/Theme/uncontrol.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="main">
<fieldset class="fieldset_ts">
<legend class="legends">
<img src="http://www.mamicode.com/images/gif_57_007.gif" /><span id="PayTitle" runat="server">平台员工允许统计采购数据采购管理</span></legend>
<div id="divShow1" runat="server" style="width: 100%">
<table style="width: 90%;">
<tr>
<td align="center" valign="middle" style="height: 50px">
<span>采购商或供应商帐号:</span>
<asp:TextBox ID="txtSel" runat="server"></asp:TextBox>
<asp:Button runat="server" Text="查询" CssClass="btn_2_3" ID="btnSearch" OnClick="btnSearch_Click" />
<asp:Button runat="server" Text="添加" CssClass="btn_2_3" ID="btnInsert" OnClick="btnInsert_Click" />
</td>
</tr>
</table>
</div>
</fieldset>
<div id="divShow2" runat="server" style="width: 100%">
<asp:GridView ID="gvInfo" runat="server" AutoGenerateColumns="False" Width="100%"
EmptyDataText="没有符合条件的数据!" CssClass="GridViewStyle">
<FooterStyle ForeColor="White" />
<RowStyle CssClass="RowStyle" />
<Columns>
<asp:TemplateField HeaderText="ID" Visible="false">
<ItemTemplate>
<span title=‘<%# DataBinder.Eval(Container.DataItem, "ID").ToString()%>‘></span>
</ItemTemplate>
<ItemStyle Width="6%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="员工账号">
<ItemTemplate>
<span title=‘<%# DataBinder.Eval(Container.DataItem, "StaffNmae").ToString()%>‘>
<%# this.SubStr(Eval("StaffNmae").ToString(), 20)%></span>
</ItemTemplate>
<ItemStyle Width="6%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="平台账号">
<ItemTemplate>
<span title=‘<%# this.GetNameByGuid(Eval("[StaffNo]").ToString())%>‘></span>
</ItemTemplate>
<ItemStyle Width="6%" />
</asp:TemplateField>

<asp:TemplateField HeaderText="维护人">
<ItemTemplate>
<span title=‘<%# DataBinder.Eval(Container.DataItem, "StaffUse").ToString()%>
<%# this.SubStr(Eval("StaffUse").ToString(), 20)%>‘></span>
</ItemTemplate>
<ItemStyle Width="6%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="日期">
<ItemTemplate>
<span title=‘<%# DataBinder.Eval(Container.DataItem, "Time").ToString()%>‘>
<%# this.SubStr(Eval("Time").ToString(), 20)%></span>
</ItemTemplate>
<ItemStyle Width="6%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<asp:LinkButton ID="lbtnDel" runat="server" OnCommand="lbtnDel_Command" CommandArgument=‘<%# Bind("ID") %>‘>删除</asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="6%" />
</asp:TemplateField>
</Columns>
<PagerStyle CssClass="PagerStyle" />
<SelectedRowStyle CssClass="SelectedRowStyle" />
<HeaderStyle CssClass="HeaderStyle" />
<EditRowStyle CssClass="EditRowStyle" />
<AlternatingRowStyle CssClass="AltRowStyle" />
</asp:GridView>

<div id="divPage" style="display: block; text-align: center; width: 95%" runat="server">
<span>共 <span id="count" runat="server" class="red">5</span>条信息 <span id="counts"
class="red">10</span> 条/页 当前 <span id="index" class="red" runat="server">1</span>
/ <span id="indexCount" class="red" runat="server">1</span> 页</span>
<asp:Button runat="server" ID="btnFrist" Text="首页" CssClass="PagerStyle_btn" Width="50px" OnClick="btnPage_Click" />
<asp:Button runat="server" ID="btnUp" Text="上一页" CssClass="PagerStyle_btn" OnClick="btnPage_Click" Width="50px" />
<asp:Button runat="server" ID="btnNext" Text="下一页" CssClass="PagerStyle_btn" OnClick="btnPage_Click" Width="50px" />
<asp:Button runat="server" ID="btnBlack" Text="尾页" CssClass="PagerStyle_btn" OnClick="btnPage_Click" Width="50px" />

</div>
</div>
<div id="show3" runat="server" visible="false">
员工账号:<asp:TextBox type="text" ID="StaffNmae" runat="server" Enabled="false" ></asp:TextBox>
平台账号: <asp:TextBox ID="txtSetxtNamel" runat="server"></asp:TextBox>
<asp:Button runat="server" Text="保存" CssClass="btn_2_3" ID="btnAdd" OnClick="btnAdd_Click" />
</div>
</div>
<input id="Select_City_Hide" runat="server" type="hidden" value="" />
<input id="Select_City_Hide1" type="hidden" runat="server" value="" />
<input id="hidid" type="hidden" runat="server" value="" />
</form>
</body>

<script src="http://www.mamicode.com/js/policy/Select_Event.js" type="text/javascript" language="javascript"></script>
</html>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Collections;
using System.Data.SqlClient;
using System.Web.UI.HtmlControls;
using _8000yiLibrary.StaffManagerBusiness;

public partial class adminmaster_StaffManager : System.Web.UI.Page
{
private static int indexPage = 0;
//总页数
private static int pageCount = 0;
StaffManagerBusinessInfo staff = new StaffManagerBusinessInfo();
protected void Page_Load(object sender, EventArgs e)
{
//判断当前登陆的用户this.txtStaffPlatform.Value
if (Session["Account"] != null && ((int)((Account)Session["Account"]).Type == (int)Ao.UserType.AdminPlatForm || (int)((Account)Session["Account"]).Type == (int)Ao.UserType.AdminEmployee))
{
if (!IsPostBack)
{
BingdingData();
}
}
else
{
Response.Redirect("../login.aspx", false);
return;
}
}
/// <summary>
/// 通过guid查询 名称
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public string GetNameByGuid(string name)
{
string na = null;
try
{
na = staff.GetNameByGuid(name);
}
catch (Exception ex)
{
StaticErrLog.errlog.ErrContent = ex.ToString(); //错误信息
StaticErrLog.errlog.ErrTime = DateTime.Now; //错误发生时间
StaticErrLog.errlog.Methods = "lbtnDel_Command()"; //发生错误的方法
StaticErrLog.errlog.Page = "StaffManager.aspx"; // 发生错误的页面
int vv = StaticErrLog.errlog.ErrorRecord(); //记录错误信息的方法
}
return na;
}
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSearch_Click(object sender, EventArgs e)
{
this.show3.Visible = false;
this.divShow2.Visible = true;
//显示数据
BingdingData();
}
/// <summary>
/// 绑定数据
/// </summary>
private void BingdingData()
{
try
{
//获取总条数
int itmeCounts = staff.GetPageCounts(this.txtSel.Text.Trim(), 10, ref pageCount);
//总条数
this.count.InnerText = itmeCounts.ToString();
//当前页数
this.index.InnerText = (indexPage + 1).ToString();
//总页数
this.indexCount.InnerText = pageCount.ToString();

DataTable tb = staff.GetAllByName(10, indexPage, this.txtSel.Text.Trim());
gvInfo.DataSource = null;
gvInfo.DataSource = tb;
gvInfo.DataKeyNames = new string[] { "ID" };
gvInfo.DataBind();
}
catch (Exception ex)
{
StaticErrLog.errlog.ErrContent = ex.ToString(); //错误信息
StaticErrLog.errlog.ErrTime = DateTime.Now; //错误发生时间
StaticErrLog.errlog.Methods = "lbtnDel_Command()"; //发生错误的方法
StaticErrLog.errlog.Page = "StaffManager.aspx"; // 发生错误的页面
int vv = StaticErrLog.errlog.ErrorRecord(); //记录错误信息的方法
}
}
/// <summary>
/// 添加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnInsert_Click(object sender, EventArgs e)
{
this.show3.Visible = true;
}
/// <summary>
/// 提交保存
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAdd_Click(object sender, EventArgs e)
{
//判断当前登陆的用户this.txtStaffPlatform.Value
if (Session["Account"] != null && ((int)((Account)Session["Account"]).Type == (int)Ao.UserType.AdminPlatForm || (int)((Account)Session["Account"]).Type == (int)Ao.UserType.AdminEmployee))
{
try
{
//判断当前登陆的用户this.txtStaffPlatform.Value
if (string.IsNullOrEmpty(this.txtSetxtNamel.Text.Trim()) && string.IsNullOrEmpty(this.StaffNmae.Text.Trim()))
{
Page.RegisterStartupScript("delete", "<script language=‘javascript‘>alert(‘输入不能为空!‘)</script>");
return;
}
//获取该平台账号是否存在
if (staff.SearchStaffName(StaffNmae.Text.Trim()) <= 0)
{
Page.RegisterStartupScript("delete", "<script language=‘javascript‘>alert(‘暂无该员工账号任何信息!‘)</script>");
return;
}
//获取采购或供应商的guid
int guidBuyername = staff.SearchBuyersByname(this.txtSetxtNamel.Text.Trim());
int guidProname = staff.SearchproviderByname(this.txtSetxtNamel.Text.Trim());
if (guidBuyername <= 0 && guidProname <= 0)
{
Page.RegisterStartupScript("delete", "<script language=‘javascript‘>alert(‘暂无该平台账号任何信息!‘)</script>");
return;
}
string guidBuyer = staff.GetGuidByBuyername(this.txtSetxtNamel.Text.Trim());
string guidPro = staff.GetGuidByProname(this.txtSetxtNamel.Text.Trim());

string guid = string.Empty;
if (string.IsNullOrEmpty(guidBuyer))
guid = guidPro;
else
guid = guidBuyer;
//获取该供应商或采购商是否存在
int count = staff.getStaff(this.StaffNmae.Text.Trim(), guid);
if (count >= 1)
{
Page.RegisterStartupScript("delete", "<script language=‘javascript‘>alert(‘该信息已经存在!‘)</script>");
this.StaffNmae.Text = "";
this.txtSetxtNamel.Text = "";
return;
}
string loginName = (Session["Account"] as Account).UserAccount.ToString();
int num = staff.AddInfo(guid, this.StaffNmae.Text.Trim(), loginName);
if (num == 1)
{
BingdingData();
Page.RegisterStartupScript("delete", "<script language=‘javascript‘>alert(‘添加成功!‘)</script>");
this.show3.Visible = false;
this.divShow2.Visible = true;
}
}
catch (Exception ex)
{
StaticErrLog.errlog.ErrContent = ex.ToString(); //错误信息
StaticErrLog.errlog.ErrTime = DateTime.Now; //错误发生时间
StaticErrLog.errlog.Methods = "btnAdd_Click()"; //发生错误的方法
StaticErrLog.errlog.Page = "StaffManager.aspx"; // 发生错误的页面
int vv = StaticErrLog.errlog.ErrorRecord(); //记录错误信息的方法
}
}
else
{
Response.Redirect("../login.aspx", false);
return;
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void lbtnDel_Command(object sender, CommandEventArgs e)
{
try
{
int num = staff.DelInof(Convert.ToInt32(e.CommandArgument));
if (num != 0)
{
//删除成功 重新加载信息
BingdingData();
Page.RegisterStartupScript("delete", "<script language=‘javascript‘>alert(‘删除成功!‘)</script>");
}
}
catch (Exception ex)
{
StaticErrLog.errlog.ErrContent = ex.ToString(); //错误信息
StaticErrLog.errlog.ErrTime = DateTime.Now; //错误发生时间
StaticErrLog.errlog.Methods = "lbtnDel_Command()"; //发生错误的方法
StaticErrLog.errlog.Page = "StaffManager.aspx"; // 发生错误的页面
int vv = StaticErrLog.errlog.ErrorRecord(); //记录错误信息的方法
}
}
/// <summary>
/// 截取字符串,防止字符太多,把表格撑开
/// </summary>
/// <param name="tempStr">需要截取字符的字符串</param>
/// <param name="len">截取长度</param>
/// <returns></returns>
public string SubStr(string tempStr, int len)
{
if (tempStr != null && tempStr.Length > len)
{
return tempStr.Substring(0, len) + "......";
}
else
{
return tempStr;
}
}
/// <summary>
/// 首页 上一页 下一页 尾页
/// </summary>
/// <param name="sendr"></param>
/// <param name="e"></param>
protected void btnPage_Click(object sender, EventArgs e)
{
switch ((sender as Button).Text)
{
case "首页":
indexPage = 0;
break;
case "上一页":
if (indexPage >= 1)
{
indexPage--;
}
break;
case "下一页":
if (indexPage < pageCount - 1)
{
indexPage++;
}
break;
case "尾页":
if (indexPage <= pageCount)
{
indexPage = pageCount - 1;
}
break;
}
BingdingData();
}
}

asp.net中的DataGridView增删改查加分页