zoukankan
html css js c++ java
编辑 删除 分页
<%
@ Page language
=
"
c#
"
Codebehind
=
"
WebForm1.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
kaoshi.WebForm1
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
WebForm1
</
title
>
<
meta
name
="GENERATOR"
Content
="Microsoft Visual Studio .NET 7.1"
>
<
meta
name
="CODE_LANGUAGE"
Content
="C#"
>
<
meta
name
="vs_defaultClientScript"
content
="JavaScript"
>
<
meta
name
="vs_targetSchema"
content
="http://schemas.microsoft.com/intellisense/ie5"
>
</
HEAD
>
<
body
MS_POSITIONING
="GridLayout"
>
<
form
id
="Form1"
method
="post"
runat
="server"
>
<
TABLE
id
="Table1"
cellSpacing
="1"
cellPadding
="1"
width
="380"
align
="center"
border
="0"
>
<
TR
>
<
TD
></
TD
>
</
TR
>
<
TR
>
<
TD
class
="red12"
height
="30"
>
<
asp:label
id
="lblProcess"
runat
="server"
></
asp:label
>
<
asp:textbox
id
="txtClssid"
runat
="server"
Visible
="False"
Width
="16px"
Height
="12px"
></
asp:textbox
></
TD
>
</
TR
>
<
TR
>
<
TD
align
="center"
>
<
asp:datagrid
id
="gridRoot"
style
="FONT-SIZE: 9pt"
runat
="server"
HorizontalAlign
="Center"
OnDeleteCommand
="gridRoot_Delete"
OnUpdateCommand
="gridRoot_Update"
OnCancelCommand
="gridRoot_Cancel"
OnEditCommand
="gridRoot_Edit"
DataKeyField
="id"
BorderColor
="black"
BorderWidth
="1"
autogeneratecolumns
="False"
HeaderStyle-BackColor
="#67bcce"
OnPageIndexChanged
="grid_PageChanged"
PageSize
="20"
PagerStyle-HorizontalAlign
="Right"
PagerStyle-Mode
="NumericPages"
AllowPaging
="True"
CellSpacing
="0"
CellPadding
="4"
width
="500px"
>
<
HeaderStyle
BackColor
="#67BCCE"
></
HeaderStyle
>
<
Columns
>
<
asp:BoundColumn
DataField
="id"
ReadOnly
="True"
HeaderText
="序号"
>
<
HeaderStyle
HorizontalAlign
="Center"
Height
="30px"
Width
="50px"
></
HeaderStyle
>
<
ItemStyle
Font-Names
="verdana"
HorizontalAlign
="Center"
BackColor
="#EEEEEE"
></
ItemStyle
>
</
asp:BoundColumn
>
<
asp:TemplateColumn
SortExpression
="Typ"
HeaderText
="产品型号"
>
<
HeaderStyle
HorizontalAlign
="Center"
Width
="180px"
></
HeaderStyle
>
<
ItemStyle
HorizontalAlign
="Center"
BackColor
="#EEEEEE"
></
ItemStyle
>
<
ItemTemplate
>
<
asp:Label
id
=Label1
runat
="server"
Text
='<%#
DataBinder.Eval(Container.DataItem, "Typ") %
>
'>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
NOBR
>
<
asp:TextBox
id
=Typ
runat
="server"
Width
="94px"
Text
='<%#
DataBinder.Eval(Container.DataItem, "Typ") %
>
'>
</
asp:TextBox
>
<
asp:RequiredFieldValidator
id
="clssReqVal"
runat
="server"
Font-Size
="12"
Font-Name
="Verdana"
Display
="Dynamic"
ControlToValidate
="Typ"
>
*
</
asp:RequiredFieldValidator
></
NOBR
>
</
EditItemTemplate
>
</
asp:TemplateColumn
>
<
asp:TemplateColumn
SortExpression
="Factory"
HeaderText
="工厂名称"
>
<
HeaderStyle
HorizontalAlign
="Center"
Width
="180px"
></
HeaderStyle
>
<
ItemStyle
HorizontalAlign
="Center"
BackColor
="#EEEEEE"
></
ItemStyle
>
<
ItemTemplate
>
<
asp:Label
id
="Label2"
runat
="server"
Text
='<%#
DataBinder.Eval(Container.DataItem, "Factory") %
>
'>
</
asp:Label
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
NOBR
>
<
asp:TextBox
id
="Factory"
runat
="server"
Width
="94px"
Text
='<%#
DataBinder.Eval(Container.DataItem, "Factory") %
>
'>
</
asp:TextBox
>
<
asp:RequiredFieldValidator
id
="Requiredfieldvalidator1"
runat
="server"
Font-Size
="12"
Font-Name
="Verdana"
Display
="Dynamic"
ControlToValidate
="Factory"
>
*
</
asp:RequiredFieldValidator
></
NOBR
>
</
EditItemTemplate
>
</
asp:TemplateColumn
>
<
asp:EditCommandColumn
ButtonType
="LinkButton"
UpdateText
="更新"
HeaderText
="编辑"
CancelText
="取消"
EditText
="编辑"
>
<
HeaderStyle
HorizontalAlign
="Center"
Width
="100px"
></
HeaderStyle
>
<
ItemStyle
Wrap
="False"
HorizontalAlign
="Center"
BackColor
="#EEEEEE"
></
ItemStyle
>
</
asp:EditCommandColumn
>
<
asp:ButtonColumn
Text
="删除"
HeaderText
="删除"
CommandName
="Delete"
>
<
HeaderStyle
HorizontalAlign
="Center"
Width
="50px"
></
HeaderStyle
>
<
ItemStyle
HorizontalAlign
="Center"
BackColor
="#EEEEEE"
></
ItemStyle
>
</
asp:ButtonColumn
>
</
Columns
>
<
PagerStyle
HorizontalAlign
="Right"
Mode
="NumericPages"
></
PagerStyle
>
</
asp:datagrid
></
TD
>
</
TR
>
</
TABLE
>
</
form
>
</
body
>
</
HTML
>
cs
using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
using
System.Data.SqlClient;
namespace
kaoshi
{
/**/
///
<summary>
///
WebForm1 的摘要说明。
///
</summary>
public
class
WebForm1 : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.Label lblProcess;
protected
System.Web.UI.WebControls.TextBox txtClssid;
protected
System.Web.UI.WebControls.DataGrid gridRoot;
SqlConnection thisConnection
=
new
SqlConnection(
"
server=localhost;Database=stst;uid=sa;pwd=fan
"
);
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
if
(
!
this
.IsPostBack)
BindGrid();
}
public
void
BindGrid()
{
SqlDataAdapter myCommand
=
new
SqlDataAdapter(
"
select * from FactoryType order by Factory
"
,thisConnection);
DataSet ds
=
new
DataSet();
myCommand.Fill(ds,
"
xatop
"
);
gridRoot.DataSource
=
ds.Tables[
"
xatop
"
].DefaultView;
gridRoot.DataBind();
}
public
void
gridRoot_Edit(Object sender, DataGridCommandEventArgs e)
{
gridRoot.EditItemIndex
=
(
int
)e.Item.ItemIndex;
BindGrid();
}
public
void
grid_PageChanged(
object
sender, DataGridPageChangedEventArgs e)
{
gridRoot.CurrentPageIndex
=
e.NewPageIndex;
BindGrid();
}
public
void
gridRoot_Delete(Object sender, DataGridCommandEventArgs e)
{
String deleteCmd
=
"
DELETE from FactoryType where id = @Id
"
;
SqlCommand myCommand
=
new
SqlCommand(deleteCmd, thisConnection);
myCommand.Parameters.Add(
new
SqlParameter(
"
@Id
"
, SqlDbType.NVarChar,
11
));
myCommand.Parameters[
"
@Id
"
].Value
=
gridRoot.DataKeys[(
int
)e.Item.ItemIndex];
myCommand.Connection.Open();
try
{
myCommand.ExecuteNonQuery();
lblProcess.Text
=
"
<b>已删除记录</b>
"
;
}
catch
(SqlException)
{
lblProcess.Text
=
"
错误:未能删除记录
"
;
lblProcess.Style[
"
color
"
]
=
"
red
"
;
}
myCommand.Connection.Close();
BindGrid();
}
public
void
gridRoot_Cancel(Object sender, DataGridCommandEventArgs e)
{
gridRoot.EditItemIndex
=
-
1
;
BindGrid();
}
public
void
gridRoot_Update(Object sender, DataGridCommandEventArgs e)
{
string
colvalue
=
((TextBox)e.Item.FindControl(
"
Typ
"
)).Text;
String updateCmd
=
"
UPDATE FactoryType SET Typ = @Typ,Factory=@Factory where id = @Id
"
;
SqlCommand myCommand
=
new
SqlCommand(updateCmd, thisConnection);
myCommand.Parameters.Add(
new
SqlParameter(
"
@Id
"
, SqlDbType.NVarChar,
11
));
myCommand.Parameters.Add(
new
SqlParameter(
"
@Typ
"
, SqlDbType.NVarChar,
40
));
myCommand.Parameters.Add(
new
SqlParameter(
"
@Factory
"
, SqlDbType.NVarChar,
40
));
myCommand.Parameters[
"
@Id
"
].Value
=
gridRoot.DataKeys[(
int
)e.Item.ItemIndex];
myCommand.Parameters[
"
@Typ
"
].Value
=
((TextBox)e.Item.FindControl(
"
Typ
"
)).Text;
myCommand.Parameters[
"
@Factory
"
].Value
=
((TextBox)e.Item.FindControl(
"
Factory
"
)).Text;
myCommand.Connection.Open();
try
//
捕捉错误
{
myCommand.ExecuteNonQuery();
lblProcess.Text
=
"
<b>已更新记录</b>
"
;
gridRoot.EditItemIndex
=
-
1
;
}
catch
(SqlException b)
{
if
(b.Number
==
2627
)
lblProcess.Text
=
"
错误:已存在具有相同主键的记录
"
;
else
lblProcess.Text
=
"
错误:未能更新记录,请确保正确填写了字段
"
;
lblProcess.Style[
"
color
"
]
=
"
red
"
;
}
myCommand.Connection.Close();
//
更新数据完毕
BindGrid();
//
DdlLst(e);
}
Web 窗体设计器生成的代码
#region
Web 窗体设计器生成的代码
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.gridRoot.PageIndexChanged
+=
new
System.Web.UI.WebControls.DataGridPageChangedEventHandler(
this
.grid_PageChanged);
this
.gridRoot.CancelCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.gridRoot_Cancel);
this
.gridRoot.EditCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.gridRoot_Edit);
this
.gridRoot.UpdateCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.gridRoot_Update);
this
.gridRoot.DeleteCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.gridRoot_Delete);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
}
}
查看全文
相关阅读:
【HDU1698】 Just a Hook 【线段树入门】
【转载】线段树 区间合并 小结
Codeforces 1138B(列方程枚举)
Codeforces 1132G(关系转化树+dfn+线段树)
Codeforces 1132E(转化+dp)
Codeforces 1132D(二分模拟)
Codeforces 1131G(dp)
洛谷1941(dp)
洛谷2758(字符串dp)
Codeforces 1143B(思维、技巧)
原文地址:https://www.cnblogs.com/voodgen/p/100253.html
最新文章
HDU.2561 第二小整数(water)
HDU.2111 Saving HDU(贪心)
Linux学习笔记(12)用户和用户组管理
Linux学习笔记(11)软件包管理
Linux学习笔记(10)文本编辑器vim
Linux学习笔记(9)Linux常用命令之关机重启命令
Linux学习笔记(8)Linux常用命令之网络命令
Linux学习笔记(7)Linux常用命令之压缩解压命令
Linux学习笔记(6)Linux常用命令之帮助命令与用户管理命令
Linux学习笔记(5)Linux常用命令之文件搜索命令
热门文章
Linux学习笔记(4)Linux常用命令之权限管理命令
Linux学习笔记(3)Linux常用命令之文件处理命令
【poj1995】快速幂
poj1958-汉诺四塔问题(三种方法)
归并排序 及拓展—逆序对
[CodeForces522B] Photo to Remember
【区间DP】释放囚犯
【BZOJ1306】match循环赛
【转载】线段树题目2
【题解】【CodeForces712C】Memory and De-Evolution
Copyright © 2011-2022 走看看