本人初学,其中参考了无锋不起浪之http://www.cnblogs.com/wf225/archive/2007/08/10/850218.html
不妥之处还望各位指正,谢谢!!
前台代码
BookInfo.aspx
<%
...
@ Page Language="C#" EnableEventValidation="false" AutoEventWireup="true" CodeFile="BookInfo.aspx.cs" Inherits="ZDWH_BookInfo"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
>
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
<
script
language
="javascript"
type
="text/javascript"
>
...
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
function $(s)...{return document.getElementById(s);}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
function isNull(_sVal)...{return (_sVal == "" || _sVal == null || _sVal == "undefined");}
//获取地址栏参数
function GetURL(name)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
var URLParams = new Array();
var aParams = document.location.search.substr(1).split('&');
for (i=0; i < aParams.length; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
var aParam = aParams.split( '=');
URLParams[aParam[0]] = aParam[1];
}
//取得传过来的name参数
return URLParams[name];
}
var tgs;
var tmp_background_val;
function tog(n,flags)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (tgs)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{tgs.style.background= tmp_background_val ;}
n.style.background= '#99ccff' ;
tmp_background_val=flags;
tgs=n;
}
function CheckAll(spanChk)//全选
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
var oItem = spanChk.children;
var theBox=(spanChk.type=="checkbox")?spanChk:spanChk.children.item[0];
xState=theBox.checked;
elm=theBox.form.elements;
for(i=0;i<elm.length;i++)
if(elm.type=="checkbox" && elm.id!=theBox.id)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if(elm.checked!=xState)
elm.click();
}
}
function GetRowIndex(obj)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
$('<%=HiddenID.ClientID %>').value = obj;
}
var HiddenID;
function checkSelect()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
HiddenID=$('<%=HiddenID.ClientID %>').value;
if(isNull(HiddenID))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
alert("请选择一条记录!");
}
return !isNull(HiddenID);
}
</
script
>
<
title
>
图书信息
</
title
>
<
link
href
="../Main.css"
type
="text/css"
rel
="stylesheet"
/>
</
head
>
<
body
>
<
form
id
="Form1"
runat
="server"
>
<
b
>
图书信息增加
</
b
>
<
asp:Button
class
="input_button"
id
="BtnClear"
Text
="清 空"
runat
="server"
CausesValidation
="False"
OnClick
="BtnClear_Click"
></
asp:Button
>
<
asp:button
class
="input_button"
id
="BtnSave"
runat
="server"
text
="保 存"
OnClick
="BtnSave_Click"
></
asp:button
>
<
asp:Label
ID
="LblCode1"
Text
="图书编号"
ForeColor
="Red"
runat
="server"
Visible
="false"
></
asp:Label
>
<
asp:Label
ID
="LblCode"
Text
=""
ForeColor
="Red"
runat
="server"
Visible
="false"
></
asp:Label
>
<
hr
size
="1"
/>
<
table
class
="table_1"
id
="search"
cellspacing
="1"
cellpadding
="0"
width
="95%"
align
="center"
>
<
tbody
>
<
tr
class
="tr2"
>
<
td
style
="PADDING-LEFT: 5px"
height
="25"
>
图书ISBN:
<
asp:TextBox
class
="input_text"
id
="TxtISBN"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
<
td
style
="PADDING-LEFT: 5px"
width
="47%"
height
="25"
>
出 版 社:
<
asp:DropDownList
class
="input_text"
id
="DdlSelPress"
runat
="server"
width
="200"
></
asp:DropDownList
>
</
td
>
</
tr
>
<
tr
class
="tr1"
>
<
td
style
="PADDING-LEFT: 5px"
colspan
="2"
height
="25"
>
图书名称:
<
asp:TextBox
class
="input_text"
id
="TxtName"
runat
="server"
width
="600"
></
asp:TextBox
>
<
asp:RequiredFieldValidator
id
="Requiredfieldvalidator1"
runat
="server"
ErrorMessage
="*"
ControlToValidate
="TxtName"
>
此项必填
</
asp:RequiredFieldValidator
>
</
td
>
</
tr
>
<
tr
class
="tr2"
>
<
td
style
="PADDING-LEFT: 5px"
height
="25"
>
定
价:
<
asp:TextBox
class
="input_text"
id
="TxtBookPrice"
runat
="server"
width
="200"
></
asp:TextBox
>
<
asp:RequiredFieldValidator
id
="Requiredfieldvalidator2"
runat
="server"
ErrorMessage
="*"
ControlToValidate
="TxtBookPrice"
>
此项必填
</
asp:RequiredFieldValidator
>
</
td
>
<
td
style
="PADDING-LEFT: 5px"
width
="47%"
height
="25"
>
图书源码:
<
asp:TextBox
class
="input_text"
id
="TxtSourceCode"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
</
tr
>
<
tr
class
="tr1"
>
<
td
style
="PADDING-LEFT: 5px"
height
="25"
>
自编代码:
<
asp:TextBox
class
="input_text"
id
="TxtBookSelfCode"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
<
td
style
="PADDING-LEFT: 5px"
width
="47%"
height
="25"
>
出版年度:
<
asp:TextBox
class
="input_text"
id
="TxtCbyYear"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
</
tr
>
<
tr
class
="tr2"
>
<
td
style
="PADDING-LEFT: 5px"
height
="25"
>
版
次:
<
asp:TextBox
class
="input_text"
id
="TxtBc"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
<
td
style
="PADDING-LEFT: 5px"
width
="47%"
height
="25"
>
作
者:
<
asp:TextBox
class
="input_text"
id
="TxtBookAuthor"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
</
tr
>
<
tr
class
="tr1"
>
<
td
style
="PADDING-LEFT: 5px"
height
="25"
>
架 位 号:
<
asp:TextBox
class
="input_text"
id
="TxtLocateCode"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
<
td
style
="PADDING-LEFT: 5px"
width
="47%"
height
="25"
>
备
注:
<
asp:TextBox
class
="input_text"
id
="TxtMemo"
runat
="server"
width
="200"
></
asp:TextBox
>
</
td
>
</
tr
>
</
tbody
>
</
table
>
<
br
/>
<
b
>
图书信息查询
</
b
>
<
hr
size
="1"
/>
<
table
class
=""
id
="Table1"
cellspacing
="1"
cellpadding
="0"
width
="95%"
align
="center"
border
="0"
>
<
tbody
>
<
tr
class
="tr2"
>
<
td
style
="PADDING-LEFT: 5px; height: 25px;"
>
检索条件:
<
asp:DropDownList
class
="input_text"
id
="DdlQuery_tj"
runat
="server"
width
="97"
>
<
asp:ListItem
Value
="name"
Selected
="True"
>
图书名称
</
asp:ListItem
>
<
asp:ListItem
Value
="ISBN"
>
图书ISBN
</
asp:ListItem
>
<
asp:ListItem
Value
="author"
>
作者
</
asp:ListItem
>
<
asp:ListItem
Value
="abbrname"
>
出版社名称
</
asp:ListItem
>
<
asp:ListItem
Value
="memo"
>
备注
</
asp:ListItem
>
</
asp:DropDownList
>
检索内容:
<
asp:TextBox
class
="input_text"
id
="TxtQuery_content"
runat
="server"
width
="120"
></
asp:TextBox
>
<
asp:button
class
="input_button"
id
="BtnQuery"
runat
="server"
text
="查 询"
CausesValidation
="False"
OnClick
="BtnQuery_Click"
></
asp:button
>
<
br
/>
<
b
><
asp:Label
id
="LblMsg"
runat
="server"
forecolor
="Red"
></
asp:Label
><
br
/>
<
asp:Button
ID
="BtnDelChecked"
runat
="server"
Height
="25px"
OnClick
="BtnDelChecked_Click"
Text
="删除选中"
Width
="100px"
CausesValidation
="false"
Visible
="false"
OnClientClick
="javascript:return ( checkSelect() && confirm('警告:删除将无法恢复!确认删除吗?') );"
/>
<
asp:Button
ID
="BtnToExcel"
runat
="server"
CausesValidation
="False"
Height
="25px"
OnClick
="BtnToExcel_Click"
Text
="导出到Excel"
Width
="100px"
/>
<
asp:Button
ID
="BtnToWord"
runat
="server"
CausesValidation
="False"
Height
="25px"
OnClick
="BtnToWord_Click"
Text
="导出到Word"
Width
="100px"
/></
b
>
<
input
type
="hidden"
id
="HiddenID"
name
="HiddenID"
runat
="server"
/>
</
td
>
</
tr
>
<
tr
>
<
td
style
="PADDING-LEFT: 5px"
height
="25"
>
<
asp:GridView
ID
="gridViewPublishers"
runat
="server"
CellPadding
="4"
ForeColor
="#333333"
GridLines
="None"
AllowPaging
="True"
AllowSorting
="True"
AutoGenerateColumns
="False"
OnPageIndexChanging
="GridViewPublishersPageChanged"
OnSorting
="GridViewPublishersSorting"
OnRowDeleting
="GridViewRowDelete"
DataKeyNames
="code"
OnRowEditing
="GridViewRowEdit"
OnRowDataBound
="gridViewPublishers_RowDataBound"
>
<
FooterStyle
BackColor
="#1C5E55"
Font-Bold
="True"
ForeColor
="White"
/>
<
RowStyle
BackColor
="#E3EAEB"
/>
<
EditRowStyle
BackColor
="#7C6F57"
/>
<
SelectedRowStyle
BackColor
="#C5BBAF"
Font-Bold
="True"
ForeColor
="#333333"
/>
<
PagerStyle
BackColor
="#666666"
ForeColor
="White"
HorizontalAlign
="Right"
/>
<
HeaderStyle
BackColor
="#1C5E55"
Font-Bold
="True"
ForeColor
="White"
/>
<
AlternatingRowStyle
BackColor
="White"
/>
<
Columns
>
<
asp:TemplateField
>
<
ItemTemplate
>
<
input
id
="check"
runat
="server"
type
="checkbox"
value
='<%#
Eval("Code") %
>
' />
</
ItemTemplate
>
<
HeaderTemplate
>
<
input
id
="checkAll"
runat
="server"
type
="checkbox"
onclick
="javascript:CheckAll(this);"
/>
</
HeaderTemplate
>
</
asp:TemplateField
>
<
asp:BoundField
DataField
="ID"
DataFormatString
="{0:d}"
HeaderText
="序號"
/>
<
asp:BoundField
DataField
="code"
HeaderText
="圖書編號"
SortExpression
="code"
/>
<
asp:BoundField
DataField
="name"
HeaderText
="圖書名稱 "
SortExpression
="name"
/>
<
asp:BoundField
DataField
="abbrname"
HeaderText
="版別"
SortExpression
="abbrname"
/>
<
asp:BoundField
DataField
="price"
DataFormatString
="{0:C}"
HtmlEncode
="false"
HeaderText
="定價"
SortExpression
="price"
/>
<
asp:BoundField
DataField
="Author"
HeaderText
="作者"
SortExpression
="author"
/>
<
asp:BoundField
DataField
="version"
HeaderText
="版次"
SortExpression
="version"
/>
<
asp:BoundField
DataField
="storeamountz"
HeaderText
="總店庫存"
SortExpression
="storeamountz"
/>
<
asp:BoundField
DataField
="storeamounta"
HeaderText
="門市庫存"
SortExpression
="storeamounta"
/>
<
asp:TemplateField
>
<
ItemTemplate
>
<
asp:Button
ID
="del"
runat
="server"
CausesValidation
="false"
Text
="删除"
CommandName
="Delete"
OnClientClick
="return confirm('您确认删除该记录吗?');"
CommandArgument
='<%#
Eval("Code") %
>
'>
</
asp:Button
>
</
ItemTemplate
>
<
HeaderTemplate
>
删除
</
HeaderTemplate
>
</
asp:TemplateField
>
<
asp:CommandField
ButtonType
="Button"
CausesValidation
="False"
HeaderText
="編輯"
ShowEditButton
="True"
/>
</
Columns
>
</
asp:GridView
>
</
td
>
</
tr
>
</
tbody
>
</
table
>
</
form
>
</
body
>
</
html
>
后台代码
BookInfo.aspx.cs
using
System;
using
System.Data;
using
System.Configuration;
using
System.Collections;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
partial
class
ZDWH_BookInfo : System.Web.UI.Page
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
Page_Load()#region Page_Load()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// Page_Load事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (!IsPostBack)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//判断用户登陆状态
//if (Session.Count = 0)
//{
// Response.Redirect("../index.aspx");
//}
//else
//{
// if (Session["CurrUser"].ToString == "")
// Response.Redirect("../index.aspx");
//}
DdlSelPressDataBind();
GridViewPublishersDataBind();
}
//i = 1;
//GridViewPublishersDataBind();
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
GetAlertScript(string str)#region GetAlertScript(string str)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 返回javascript字符串,str:彈出對話框所呈現的文字
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
protected string GetAlertScript(string str)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string theScript;
theScript = "<script language='javascript'>alert('" + str + "')</script>";
return theScript;
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
綁定出版社#region 綁定出版社
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 綁定出版社Ddl
/// </summary>
protected void DdlSelPressDataBind()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlConnection cnn;
SqlDataAdapter dataA;
string sql;
DataSet ds;
using (cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreConnectionString"].ToString()))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
sql = "SELECT Code,FullName FROM PressInfoSheet ORDER BY FullName";
dataA = new SqlDataAdapter(sql, cnn);
ds = new DataSet();
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dataA.Fill(ds, "PressInfoSheet");
DdlSelPress.DataSource = ds.Tables["PressInfoSheet"].DefaultView;
DdlSelPress.DataTextField = "FullName";
DdlSelPress.DataValueField = "Code";
DdlSelPress.DataBind();
}
catch(SqlException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("數據讀取錯:" + ex.Message);
}
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
属性#region 属性
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////
private string GridViewSortDirection
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return ViewState["SortDirection"] as string ?? "ASC"; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ ViewState["SortDirection"] = value; }
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
private string GridViewSortExpression
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
get ...{ return ViewState["SortExpression"] as string ?? string.Empty; }
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
set ...{ ViewState["SortExpression"] = value; }
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
方法#region 方法
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////
///
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
private方法#region private方法
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// GetDirection(),GetDataTable(SqlConnection cnn, string sql),ClearTextBox()
/// SetBackColor(GridView gridView,DataTable dt,string colName,int col,System.Drawing.Color color)
/// </summary>
/// <returns></returns>
private string GetDirection()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
switch(GridViewSortDirection)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
case"ASC":
GridViewSortDirection = "DESC";
break;
case"DESC":
GridViewSortDirection = "ASC";
break;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return GridViewSortDirection;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
获取DataTable#region 获取DataTable
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// cnn,连接对象;sql,连接字符串;
/// </summary>
/// <param name="cnn"></param>
/// <param name="sql"></param>
/// <returns></returns>
private DataTable GetDataTable(SqlConnection cnn, string sql)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlDataAdapter dataA;
DataTable dt;
dataA = new SqlDataAdapter(sql, cnn);
dt = new DataTable("gridViewPublishers");
dt.Columns.Add("ID");
//dt.Columns["ID"].AutoIncrement = true;
//dt.Columns["ID"].AutoIncrementSeed = 1;
//dt.Columns["ID"].AutoIncrementStep = 1;
ViewState["dataSource"] = dt;
dataA.Fill(dt);
return dt;
}
#endregion
//
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
设置突出显示#region 设置突出显示
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
///突出显示:gridView 要设置的GridView,dt 源DataTable,colName 列名,col 列序号 color 颜色
/// </summary>
//
private void SetBackColor(GridView gridView,DataTable dt,string colName,int col,System.Drawing.Color color)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
for (int i = 0; i <= gridView.Rows.Count - 1; i++)//设置突出显示单元格
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//DataRowView mydrv = dt.DefaultView;
if (Convert.ToInt32(gridView.Rows.Cells[col].Text) < 10)//当此列小于10时改变背景色
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
gridView.Rows.Cells[col].ForeColor = color;
gridView.Rows.Cells[col].Font.Bold = true;
gridView.Rows.Cells[col].Font.Size = 14;
//gridView.Rows.Cells[col].BorderColor = System.Drawing.Color.White;
//gridView.Rows.Cells[col].BorderWidth = 2;
}
//LblMsg.Text = mydrv[colName].ToString();
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
清空输入区域#region 清空输入区域
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
///
/// </summary>
private void ClearTextBox()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TxtISBN.Text = "";
TxtName.Text = "";
TxtBookPrice.Text = "";
TxtSourceCode.Text = "";
TxtBookSelfCode.Text = "";
TxtCbyYear.Text = "";
TxtBc.Text = "";
TxtBookAuthor.Text = "";
TxtLocateCode.Text = "";
TxtMemo.Text = "";
DdlSelPress.SelectedIndex = -1;
BtnClear.Text = "清 空";
BtnSave.Text = "保 存";
LblCode.Text = "";
LblCode.Visible = false;
LblCode1.Visible = false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
批量删除#region 批量删除
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////删除tableName表中索引为sID的行
///
private void DeleteRows(string tableName, string sID)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
sID = sID.Trim();
sID=sID.Substring(0,sID.Length-1);//去掉末尾","
System.Text.StringBuilder code = new System.Text.StringBuilder(sID);
code=code.Replace(",", " or code=");
using (SqlConnection cnn = GetSqlConnection())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string sql = "delete from "+tableName+" where code="+code.ToString();
using (SqlCommand cmd = new SqlCommand(sql, cnn))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("删除失败:" + ex.Message);
}
}
CloseSqlConnection(cnn);
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
protected方法#region protected方法
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 为转 excel 出问题而加上的过程(一定加!!)
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
// 为转 excel 出问题而加上的过程(一定加!!)// Confirms that an HtmlForm control is rendered for
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// fileName文件名称;fileType文件类型,只能为word/excel任一个
/// </summary>
/// <param name="fileName"></param>
/// <param name="fileType"></param>
protected void GridViewExport(string fileName, string fileType)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.ClearContent();
Response.Buffer = true;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (fileType.ToLower()== "excel")
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.AddHeader("content-disposition", "attachment; filename="" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".xls"");
Response.ContentType = "application/vnd.ms-excel";
}
else if (fileType.ToLower() == "word")
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.AddHeader("content-disposition", "attachment; filename="" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + ".doc"");
Response.ContentType = "application/vnd.ms-word";
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return ;
}
Response.Charset = "GB2312";
//GetEncoding("GB2312")容易引起乱码,所以建议实用UTF7/UTF8格式
//Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentEncoding = System.Text.Encoding.UTF7;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
gridViewPublishers.AllowPaging = false;//取消分页
gridViewPublishers.AllowSorting = false;//取消排序
GridViewPublishersDataBind();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//隐藏不要的列
gridViewPublishers.Columns[0].Visible = false;
gridViewPublishers.Columns[10].Visible = false;
gridViewPublishers.Columns[11].Visible = false;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
gridViewPublishers.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.Flush();
Response.End();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
gridViewPublishers.AllowPaging = true;//恢复分页
gridViewPublishers.AllowSorting = true;//恢复排序
GridViewPublishersDataBind();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//显示隐藏列
gridViewPublishers.Columns[0].Visible = true;
gridViewPublishers.Columns[10].Visible = true;
gridViewPublishers.Columns[11].Visible = true;
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
数据库联接操作#region 数据库联接操作
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////
//获取数据库联接对象
private SqlConnection GetSqlConnection()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["BookStoreConnectionString"].ToString());
cnn.Open();
return cnn;
}
catch (SqlException e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("数据库连接出错:" + e.Message);
return null;
}
}
//关闭数据库联接对象
private void CloseSqlConnection(SqlConnection cnn)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cnn.Close();
}
catch (SqlException e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("数据库连接不存在或已经关闭:" + e.Message);
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
GridView操作#region GridView操作
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
綁定GridView#region 綁定GridView
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 綁定GridViewPublishers
/// </summary>
protected void GridViewPublishersDataBind()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlConnection cnn;
SqlCommand cmd;
SqlDataReader dataR;
//SqlDataAdapter dataA;
DataTable dt;
//DataSet ds;
//DataColumn dc;
string sql;
int maxPage;//最大頁數
cnn = GetSqlConnection();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
sql = "SELECT COUNT(*) as nums,SUM(storeamounta) as mskc,SUM(storeamountz) as zdkc,SUM(price*storeamounta) as msmy,SUM(price*storeamountz) as zdmy FROM BookInfoSheet,PressInfoSheet,BookStoreSheetA,BookStoreSheetZ where BookInfoSheet.presscode=PressInfoSheet.code and BookInfoSheet.code=BookStoreSheetA.bookcode and BookInfoSheet.code=BookStoreSheetZ.bookcode ";
if (TxtQuery_content.Text.Trim() != "")
sql += " AND " + DdlQuery_tj.SelectedItem.Value + " LIKE '%" + TxtQuery_content.Text.Trim() + "%'";
cmd = new SqlCommand(sql, cnn);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dataR = cmd.ExecuteReader();
if (dataR.Read())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (dataR["nums"].ToString() == "0")
LblMsg.Text = "";
else
LblMsg.Text = "合計:記錄數 " + dataR["nums"].ToString() + " 總店庫存 " + dataR["zdkc"].ToString() + " 門市庫存 " + dataR["mskc"].ToString() + " 總店碼洋 " + dataR["zdmy"].ToString() + " 門市碼洋 " + dataR["msmy"].ToString();
}
dataR.Close();
}
catch (SqlException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("數據讀取出錯:" + ex.Message);
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////取表头信息结束
///綁定gridViewPublishers
sql = "SELECT BookInfoSheet.*,abbrname,storeamounta,storeamountz FROM BookInfoSheet,PressInfoSheet,BookStoreSheetA,BookStoreSheetZ where BookInfoSheet.presscode=PressInfoSheet.code and BookInfoSheet.code=BookStoreSheetA.bookcode and BookInfoSheet.code=BookStoreSheetZ.bookcode ";
if (TxtQuery_content.Text.Trim() != "")
sql += " AND " + DdlQuery_tj.SelectedItem.Value + " LIKE '%" + TxtQuery_content.Text.Trim() + "%'";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
sql += " ORDER BY BookInfoSheet.name DESC";
//dataA = new SqlDataAdapter(sql, cnn);
//dt = new DataTable("gridViewPublishers");
//dt.Columns.Add("ID");
//dt.Columns["ID"].AutoIncrement = true;
dt = GetDataTable(cnn, sql);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//dataA.Fill(dt);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (dt.DefaultView.Count % gridViewPublishers.PageSize > 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
maxPage = dt.DefaultView.Count / gridViewPublishers.PageSize + 1;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
maxPage = dt.DefaultView.Count / gridViewPublishers.PageSize;
}
while (gridViewPublishers.PageIndex > 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (maxPage < gridViewPublishers.PageIndex + 1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
gridViewPublishers.PageIndex = gridViewPublishers.PageIndex - 1;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
break;
}
}
gridViewPublishers.DataSource = dt;
gridViewPublishers.DataBind();
CloseSqlConnection(cnn);
SetBackColor(gridViewPublishers, dt, "storeamounta", 9, System.Drawing.Color.Orange);
SetBackColor(gridViewPublishers, dt, "storeamountz", 8, System.Drawing.Color.OrangeRed);
BtnDelChecked.Visible = true;
}
catch (SqlException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("數據讀取錯:" + ex.Message);
}
CloseSqlConnection(cnn);
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
排序#region 排序
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////
///
///
protected void GridViewPublishersSorting(Object sender, GridViewSortEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
GridViewSortExpression = e.SortExpression;
int pageIndex = gridViewPublishers.PageIndex;
//if (dt != null)
//LblMsg.Text = e.SortExpression;
gridViewPublishers.DataSource = SortDataTable(ViewState["dataSource"] as DataTable, false);
gridViewPublishers.DataBind();
SetBackColor(gridViewPublishers, ViewState["dataSource"] as DataTable, "storeamountz",8, System.Drawing.Color.OrangeRed);
SetBackColor(gridViewPublishers, ViewState["dataSource"] as DataTable, "storeamounta",9,System.Drawing.Color.Orange);
gridViewPublishers.PageIndex = pageIndex;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//对DataTable排序操作
private DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (dataTable != null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
if (isPageIndexChanging)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dataView.Sort = String.Format("{0} {1}", GridViewSortExpression, GetDirection());
}
}
return dataView;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return new DataView();
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
删除#region 删除
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////
///
///
//
protected void GridViewRowDelete(Object sender, GridViewDeleteEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int strCode = Convert.ToInt32(gridViewPublishers.DataKeys[e.RowIndex].Value);
//string strCode=delete
int zdkc = Convert.ToInt32(gridViewPublishers.Rows[e.RowIndex].Cells[8].Text);
int mskc = Convert.ToInt32(gridViewPublishers.Rows[e.RowIndex].Cells[9].Text);
if (zdkc != 0 || mskc != 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", GetAlertScript("该书存在库存,无法删除!"));
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
using (SqlConnection cnn = GetSqlConnection())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
string sql = "delete from bookinfosheet where code=@code";
using (SqlCommand cmd = new SqlCommand(sql, cnn))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlParameter ParaCode = new SqlParameter("code", SqlDbType.Int);
ParaCode.Value = strCode;
cmd.Parameters.Add(ParaCode);
//gridViewPublishers.DeleteRow(e.RowIndex);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(),"success",GetAlertScript("删除成功!"));
}
catch (SqlException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("删除失败:" + ex.Message);
}
}
}
GridViewPublishersDataBind();
ClearTextBox();
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
编辑#region 编辑
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**////
///
protected void GridViewRowEdit(Object sender, GridViewEditEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlConnection cnn;
SqlCommand cmd;
SqlDataReader dataR;
string sql;
LblCode.Text = gridViewPublishers.Rows[e.NewEditIndex].Cells[2].Text;//保存Code值
LblCode.Visible = true;
LblCode1.Visible = true;
BtnSave.Text = "保存修改";
BtnClear.Text = "取消";
//gridViewPublishers.Rows[e.NewEditIndex].Enabled = false;
using (cnn = GetSqlConnection())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
sql = "SELECT * FROM BookInfoSheet WHERE Code=@Code";
using (cmd = new SqlCommand(sql, cnn))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlParameter ParaCode = new SqlParameter("Code", SqlDbType.Int);
ParaCode.Value = LblCode.Text;
cmd.Parameters.Add(ParaCode);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
dataR = cmd.ExecuteReader();
while (dataR.Read())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
TxtISBN.Text = dataR["ISBN"].ToString();
TxtName.Text = dataR["Name"].ToString();
TxtBookPrice.Text = dataR["Price"].ToString();
TxtSourceCode.Text = dataR["SourceCode"].ToString();
TxtBookSelfCode.Text = dataR["SelfCode"].ToString();
TxtCbyYear.Text = dataR["YYear"].ToString();
TxtBc.Text = dataR["Version"].ToString();
TxtBookAuthor.Text = dataR["Author"].ToString();
TxtLocateCode.Text = dataR["LocCode"].ToString();
TxtMemo.Text = dataR["Memo"].ToString();
DdlSelPress.SelectedIndex = DdlSelPress.Items.IndexOf(DdlSelPress.Items.FindByValue(dataR["PressCode"].ToString()));
}
}
catch (SqlException ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Response.Write("数据读取错:" + ex.Message);
}
}
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
翻頁#region 翻頁
protected void GridViewPublishersPageChanged(Object sender, GridViewPageEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
gridViewPublishers.PageIndex = e.NewPageIndex;
GridViewPublishersDataBind();
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
RowDataBound事件#region RowDataBound事件
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 鼠标移动时改变背景样式,和自增列
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void gridViewPublishers_RowDataBound(object sender, GridViewRowEventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//判断是否数据行
if (e.Row.RowType == DataControlRowType.DataRow)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//当鼠标停留时更改背景色
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#6699cc'");
e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//
System.Web.UI.HtmlControls.HtmlInputCheckBox check = (System.Web.UI.HtmlControls.HtmlInputCheckBox)e.Row.FindControl("check");
string sid = check.Value;
if (e.Row.RowIndex % 2 != 0)
e.Row.Attributes.Add("onclick", "GetRowIndex('" + sid + "'),tog(this,'#EFF3FB')");
else
e.Row.Attributes.Add("onclick", "GetRowIndex('" + sid + "'),tog(this,'#ffffff')");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//设置序号列
if (e.Row.RowIndex != -1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int id = (e.Row.RowIndex + 1)+gridViewPublishers.PageSize*gridViewPublishers.PageIndex;
e.Row.Cells[1].Text = id.ToString();
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
事件#region 事件
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
清空输入#region 清空输入
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnClear_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ClearTextBox();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
查詢事件#region 查詢事件
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 查詢事件BtnQuery_Click()
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnQuery_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
GridViewPublishersDataBind();
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
保存用戶輸入#region 保存用戶輸入
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// BtnSave事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnSave_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
string bookCode, sql;
SqlConnection cnn;
SqlCommand cmd;
SqlDataReader dataR;
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cnn = GetSqlConnection();
if (LblCode.Text == "")
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
添加状态#region 添加状态
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
sql = "SELECT Code FROM BookInfoSheet WHERE PressCode=@PressCode ORDER BY Code DESC";
//计算新的Code值
using (cmd = new SqlCommand(sql, cnn))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlParameter ParaPressCode1 = new SqlParameter("PressCode", SqlDbType.Char, 3);
ParaPressCode1.Value = DdlSelPress.SelectedValue;
cmd.Parameters.Add(ParaPressCode1);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
dataR = cmd.ExecuteReader();
if (dataR.Read())
bookCode = DdlSelPress.SelectedValue + String.Format("0000", (Convert.ToInt32(dataR["Code"].ToString().Substring(dataR["Code"].ToString().Length - 4)) + 1));
else
bookCode = DdlSelPress.SelectedValue + "0001";
dataR.Close();
}
sql = "INSERT INTO BookInfoSheet(Code,Name,SourceCode,SelfCode,ISBN,YYear,Version,Author,Price,PressCode,LocCode,Memo) VALUES(@Code,@Name,@SourceCode,@SelfCode,@ISBN,@YYear,@Version,@Author,@Price,@PressCode,@LocCode,@Memo)";
//sql = "UPDATE BookInfoSheet Set Name=@Name,SourceCode=@SourceCode,SelfCode=@SelfCode,ISBN=@ISBN,YYear=@YYear,Version=@Version,Price=@Price,PressCode=@PressCode,LocCode=@LocCode,Memo=@Memo WHERE Code=" + LblCode.Text;
using (cmd = new SqlCommand(sql, cnn))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlParameter ParaCode, ParaName, ParaSourceCode, ParaSelfCode, ParaISBN, ParaYYear, ParaVersion, ParaAuthor, ParaPrice, ParaPressCode, ParaLocCode, ParaMemo;
ParaCode = new SqlParameter("Code", bookCode);
ParaName = new SqlParameter("Name", TxtName.Text);
ParaSourceCode = new SqlParameter("SourceCode", TxtSourceCode.Text);
ParaSelfCode = new SqlParameter("SelfCode", TxtBookSelfCode.Text);
ParaISBN = new SqlParameter("ISBN", TxtISBN.Text);
ParaYYear = new SqlParameter("YYear", TxtCbyYear.Text);
ParaVersion = new SqlParameter("Version", TxtBc.Text);
ParaAuthor = new SqlParameter("Author", TxtBookAuthor.Text);
ParaPrice = new SqlParameter("Price", TxtBookPrice.Text);
ParaPressCode = new SqlParameter("PressCode", DdlSelPress.SelectedValue);
ParaLocCode = new SqlParameter("LocCode", TxtLocateCode.Text);
ParaMemo = new SqlParameter("Memo", TxtMemo.Text);
cmd.Parameters.Add(ParaCode);
cmd.Parameters.Add(ParaAuthor);
cmd.Parameters.Add(ParaISBN);
cmd.Parameters.Add(ParaLocCode);
cmd.Parameters.Add(ParaMemo);
cmd.Parameters.Add(ParaName);
cmd.Parameters.Add(ParaPressCode);
cmd.Parameters.Add(ParaPrice);
cmd.Parameters.Add(ParaSelfCode);
cmd.Parameters.Add(ParaSourceCode);
cmd.Parameters.Add(ParaVersion);
cmd.Parameters.Add(ParaYYear);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", GetAlertScript("保存成功!"));
GridViewPublishersDataBind();
}
catch (Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", GetAlertScript("保存失敗!" + ex.Message));
Response.Write("數據插入錯:" + ex.Message);
}
}
}
#endregion
else
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
修改状态#region 修改状态
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
sql = "UPDATE BookInfoSheet Set Name=@Name,SourceCode=@SourceCode,SelfCode=@SelfCode,ISBN=@ISBN,YYear=@YYear,Version=@Version,Price=@Price,PressCode=@PressCode,LocCode=@LocCode,Memo=@Memo WHERE Code=" + LblCode.Text;
using (cmd = new SqlCommand(sql, cnn))
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
SqlParameter ParaName, ParaSourceCode, ParaSelfCode, ParaISBN, ParaYYear, ParaVersion, ParaAuthor, ParaPrice, ParaPressCode, ParaLocCode, ParaMemo;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ParaName = new SqlParameter("Name", TxtName.Text);
ParaSourceCode = new SqlParameter("SourceCode", TxtSourceCode.Text);
ParaSelfCode = new SqlParameter("SelfCode", TxtBookSelfCode.Text);
ParaISBN = new SqlParameter("ISBN", TxtISBN.Text);
ParaYYear = new SqlParameter("YYear", TxtCbyYear.Text);
ParaVersion = new SqlParameter("Version", TxtBc.Text);
ParaAuthor = new SqlParameter("Author", TxtBookAuthor.Text);
ParaPrice = new SqlParameter("Price", TxtBookPrice.Text);
ParaPressCode = new SqlParameter("PressCode", DdlSelPress.SelectedValue);
ParaLocCode = new SqlParameter("LocCode", TxtLocateCode.Text);
ParaMemo = new SqlParameter("Memo", TxtMemo.Text);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
cmd.Parameters.Add(ParaAuthor);
cmd.Parameters.Add(ParaISBN);
cmd.Parameters.Add(ParaLocCode);
cmd.Parameters.Add(ParaMemo);
cmd.Parameters.Add(ParaName);
cmd.Parameters.Add(ParaPressCode);
cmd.Parameters.Add(ParaPrice);
cmd.Parameters.Add(ParaSelfCode);
cmd.Parameters.Add(ParaSourceCode);
cmd.Parameters.Add(ParaVersion);
cmd.Parameters.Add(ParaYYear);
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
cmd.ExecuteNonQuery();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", GetAlertScript("修改成功!"));
GridViewPublishersDataBind();
}
catch (Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", GetAlertScript("修改失敗!" + ex.Message));
Response.Write("數據修改錯:" + ex.Message);
}
}
cnn.Close();
}
#endregion
CloseSqlConnection(cnn);
ClearTextBox();
GridViewPublishersDataBind();
}
catch (Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ClearTextBox();
Response.Write("連接數據庫錯:" + ex.Message);
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
全选#region 全选
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
/// 全选事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void checkAll_ServerChange(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int zdkc, mskc;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
foreach (GridViewRow gvr in gridViewPublishers.Rows)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
zdkc = Convert.ToInt32(gvr.Cells[8].Text);
mskc = Convert.ToInt32(gvr.Cells[9].Text);
if(zdkc==0&&mskc==0)
((CheckBox)gvr.Cells[0].FindControl("check")).Checked = ((CheckBox)sender).Checked;
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
批量删除#region 批量删除
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnDelChecked_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
int zdkc, mskc;
bool isChecked;
HtmlInputCheckBox check;
GridViewRow row;
string sID="";
for (int i = 0; i < gridViewPublishers.Rows.Count; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
row = gridViewPublishers.Rows;
zdkc = Convert.ToInt32(row.Cells[8].Text);
mskc = Convert.ToInt32(row.Cells[9].Text);
check =(HtmlInputCheckBox)row.FindControl("check");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (zdkc == 0 && mskc == 0 && check.Checked)
sID += check.Value + ",";
}
if (sID.Length > 0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
DeleteRows("BookInfoSheet", sID);
GridViewPublishersDataBind();
Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", GetAlertScript("批量删除完成!"));
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Success", GetAlertScript("请选择要删除的记录!"));
}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
导出为Excel#region 导出为Excel
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
///
protected void BtnToExcel_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
GridViewExport("图书信息", "excel");
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/7ff8d92cded7e0ce15e7ca1acc870052.gif)
导出为Word#region 导出为Word
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
/**//// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnToWord_Click(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
GridViewExport("图书信息", "word");
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}