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
}
}
查看全文
相关阅读:
[LeetCode] 71. Simplify Path 简化路径
[LeetCode] 173. Binary Search Tree Iterator 二叉搜索树迭代器
[LeetCode] 142. Linked List Cycle II 链表中的环 II
[LeetCode] 141. Linked List Cycle 链表中的环
读经典——《CLR via C#》(Jeffrey Richter著) 笔记_友元程序集
读经典——《CLR via C#》(Jeffrey Richter著) 笔记_通过ILDasm.exe查看编译器如何将类型及其成员编译成元数据
tfs强行签入和删除工作区
需要提升权限才能运行dism
读经典——《CLR via C#》(Jeffrey Richter著) 笔记_类型的各种成员
Jquery 获取URL中的参数
原文地址:https://www.cnblogs.com/voodgen/p/100253.html
最新文章
bzoj已挂,说好的更新明天吧,今天颓,才写3题
tyvj100题留念
【BZOJ】1085: [SCOI2005]骑士精神(A*启发式搜索)
【BZOJ】1295: [SCOI2009]最长距离(spfa+暴力)
【BZOJ】1084: [SCOI2005]最大子矩阵(DP)
【BZOJ】1491: [NOI2007]社交网络(floyd)
【BZOJ】1051: [HAOI2006]受欢迎的牛(tarjan)
【BZOJ】1202: [HNOI2005]狡猾的商人(并查集+前缀和)
【BZOJ】1191: [HNOI2006]超级英雄Hero(二分图匹配)
【BZOJ】1059: [ZJOI2007]矩阵游戏(二分图匹配)
热门文章
【BZOJ】3039: 玉蟾宫(DP/单调栈)
水题纪念
【BZOJ】1270: [BeijingWc2008]雷涛的小猫(DP+水题)
【BZOJ】1303: [CQOI2009]中位数图(特殊的技巧)
[LeetCode] 110. Balanced Binary Tree 平衡二叉树
[LeetCode] 675. Cut Off Trees for Golf Event 为高尔夫赛事砍树
[Amazon] Program for Fibonacci numbers 斐波那契数列
[LeetCode] 355. Design Twitter 设计推特
[LeetCode] 212. Word Search II 词语搜索 II
[LeetCode] 221. Maximal Square 最大正方形
Copyright © 2011-2022 走看看