zoukankan
html css js c++ java
DataGrid 完全攻略之七(实现选择、编辑和修改)
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
MsDataGrid
{
/**/
///
<summary>
///
WebForm1 的摘要说明。
///
</summary>
public
class
UserDelete : System.Web.UI.Page
{
protected
System.Web.UI.WebControls.DataGrid dgShow;
private
void
Page_Load(
object
sender, System.EventArgs e)
{
//
在此处放置用户代码以初始化页面
if
(
!
IsPostBack)
BindData();
}
private
void
BindData()
{
string
strCon
=
System.Configuration.ConfigurationSettings.AppSettings[
"
DSN
"
];
SqlConnection con
=
new
SqlConnection(strCon);
SqlDataAdapter da
=
new
SqlDataAdapter(
"
Select * from tbStudentinfo
"
,con);
DataSet ds
=
new
DataSet();
da.Fill(ds,
"
studentinfo
"
);
dgShow.DataSource
=
ds.Tables[
"
studentinfo
"
].DefaultView;
dgShow.DataBind();
}
Web Form Designer generated code
#region
Web Form Designer generated code
override
protected
void
OnInit(EventArgs e)
{
//
//
CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base
.OnInit(e);
}
/**/
///
<summary>
///
设计器支持所需的方法 - 不要使用代码编辑器修改
///
此方法的内容。
///
</summary>
private
void
InitializeComponent()
{
this
.dgShow.ItemCreated
+=
new
System.Web.UI.WebControls.DataGridItemEventHandler(
this
.dgShow_ItemCreated);
this
.dgShow.ItemCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.dgShow_ItemCommand);
this
.dgShow.PageIndexChanged
+=
new
System.Web.UI.WebControls.DataGridPageChangedEventHandler(
this
.dgShow_PageIndexChanged);
this
.dgShow.CancelCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.dgShow_CancelCommand);
this
.dgShow.EditCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.dgShow_EditCommand);
this
.dgShow.UpdateCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.dgShow_UpdateCommand);
this
.dgShow.DeleteCommand
+=
new
System.Web.UI.WebControls.DataGridCommandEventHandler(
this
.dgShow_DeleteCommand);
this
.Load
+=
new
System.EventHandler(
this
.Page_Load);
}
#endregion
private
void
dgShow_EditCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex
=
e.Item.ItemIndex;
BindData();
}
private
void
dgShow_PageIndexChanged(
object
source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShow.CurrentPageIndex
=
e.NewPageIndex;
BindData();
}
private
void
dgShow_CancelCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex
=
-
1
;
BindData();
}
private
void
dgShow_DeleteCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if
(dgShow.Items.Count
==
1
)
{
if
(dgShow.CurrentPageIndex
!=
0
)
dgShow.CurrentPageIndex
=
dgShow.CurrentPageIndex
-
1
;
}
string
strSql
=
"
delete from tbStudentinfo where studentid=
"
+
e.Item.Cells[
0
].Text
+
""
;
ExecuteSql(strSql);
BindData();
}
/**/
////////////////////////////////////////////////////////////
//
说明:执行制定SQL语句
////////////////////////////////////
/
/**/
/////////////////////////////////////////////////////////
//
private
void
ExecuteSql(
string
strSql)
{
try
{
string
strconn
=
System.Configuration.ConfigurationSettings.AppSettings[
"
DSN
"
];
//
从Web.config中读取
SqlConnection conn
=
new
SqlConnection(strconn);
SqlCommand com
=
new
SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch
(Exception e)
{
Response.Write(
"
<script language = 'javascript'>alert('
"
+
e.Message
+
"
');</script>
"
) ;
}
}
private
void
dgShow_UpdateCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string
strStudentID
=
e.Item.Cells[
0
].Text;
//
处于非编辑状态
string
strName
=
((TextBox)(e.Item.Cells[
1
].Controls[
0
])).Text;
//
处于编辑状态
string
strPass
=
((TextBox)(e.Item.Cells[
2
].Controls[
0
])).Text;
string
strSex
=
((CheckBox)(e.Item.Cells[
3
].FindControl(
"
cbSex
"
))).Checked
?
"
1
"
:
"
0
"
;
string
strBirthday
=
((TextBox)(e.Item.Cells[
4
].Controls[
0
])).Text;
string
strEmail
=
((TextBox)(e.Item.Cells[
5
].Controls[
0
])).Text;
string
strSql
=
"
update tbStudentinfo set StudentName='
"
+
strName
+
"
',StudentPass='
"
+
strPass
+
"
'
"
;
strSql
+=
"
,Sex=
"
+
strSex
+
"
,Birthday='
"
+
strBirthday
+
"
',Email='
"
+
strEmail
+
"
' where studentid=
"
+
strStudentID
+
""
;
ExecuteSql(strSql);
dgShow.EditItemIndex
=
-
1
;
BindData();
}
private
void
dgShow_ItemCreated(
object
sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch
(e.Item.ItemType)
{
case
ListItemType.Item:
case
ListItemType.EditItem:
case
ListItemType.AlternatingItem:
Button myDeleteButton
=
(Button)e.Item.FindControl(
"
btnDelete
"
);
myDeleteButton.Text
=
"
删除此行
"
;
myDeleteButton.Attributes.Add(
"
onclick
"
,
"
return confirm('您真的要删除第
"
+
e.Item.ItemIndex.ToString()
+
"
行吗?');
"
);
break
;
}
}
private
void
dgShow_ItemCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if
(e.CommandName
==
"
UserDelete
"
)
dgShow_DeleteCommand(source,e);
}
}
}
前台代码:html
<%
@ Page language
=
"
c#
"
Codebehind
=
"
UserDelete.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
MsDataGrid.UserDelete
"
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
>
<
HTML
>
<
HEAD
>
<
title
>
DataGrid使用举例
</
title
>
<
meta
name
="GENERATOR"
Content
="Microsoft Visual Studio 7.0"
>
<
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"
>
<
FONT
face
="宋体"
>
<
asp:DataGrid
id
="dgShow"
style
="Z-INDEX: 101; LEFT: 69px; POSITION: absolute; TOP: 90px"
runat
="server"
Width
="842px"
Height
="172px"
BorderColor
="Tan"
BorderWidth
="1px"
BackColor
="LightGoldenrodYellow"
CellPadding
="2"
GridLines
="None"
ForeColor
="Black"
AutoGenerateColumns
="False"
AllowPaging
="True"
>
<
SelectedItemStyle
ForeColor
="GhostWhite"
BackColor
="DarkSlateBlue"
></
SelectedItemStyle
>
<
AlternatingItemStyle
BackColor
="PaleGoldenrod"
></
AlternatingItemStyle
>
<
HeaderStyle
Font-Bold
="True"
BackColor
="Tan"
></
HeaderStyle
>
<
FooterStyle
BackColor
="Tan"
></
FooterStyle
>
<
Columns
>
<
asp:BoundColumn
DataField
="StudentID"
ReadOnly
="True"
HeaderText
="学生ID"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="StudentName"
HeaderText
="学生姓名"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="StudentPass"
HeaderText
="密码"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="Sex"
HeaderText
="性别"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="Birthday"
HeaderText
="生日"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="Email"
HeaderText
="邮件地址"
></
asp:BoundColumn
>
<
asp:TemplateColumn
HeaderText
="性别模板列"
>
<
ItemTemplate
>
<
asp:RadioButton
id
=RadioButton2
runat
="server"
Text
="男"
Checked
='<%#
DataBinder.Eval(Container, "DataItem.Sex") %
>
' Enabled="False">
</
asp:RadioButton
>
<
asp:RadioButton
id
=RadioButton1
runat
="server"
Text
="女"
Checked
='<%#
!(bool)DataBinder.Eval(Container, "DataItem.Sex") %
>
' Enabled="False">
</
asp:RadioButton
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:RadioButton
id
=cbSex
runat
="server"
Text
="男"
Checked
='<%#
DataBinder.Eval(Container, "DataItem.Sex") %
>
' GroupName="Sex">
</
asp:RadioButton
>
<
asp:RadioButton
id
=RadioButton4
runat
="server"
Text
="女"
Checked
='<%#
!(bool)DataBinder.Eval(Container, "DataItem.Sex") %
>
' GroupName="Sex">
</
asp:RadioButton
>
</
EditItemTemplate
>
</
asp:TemplateColumn
>
<
asp:ButtonColumn
Text
="选择"
HeaderText
="选择"
CommandName
="Select"
></
asp:ButtonColumn
>
<
asp:EditCommandColumn
ButtonType
="LinkButton"
UpdateText
="更新"
HeaderText
="操作"
CancelText
="取消"
EditText
="编辑"
></
asp:EditCommandColumn
>
<
asp:ButtonColumn
Text
="删除"
HeaderText
="删除"
CommandName
="Delete"
></
asp:ButtonColumn
>
<
asp:TemplateColumn
HeaderText
="自定义删除"
>
<
ItemTemplate
>
<
asp:Button
id
="btnDelete"
runat
="server"
CommandName
="UserDelete"
Text
="删除"
></
asp:Button
>
</
ItemTemplate
>
</
asp:TemplateColumn
>
<
asp:HyperLinkColumn
Text
="点击查看"
DataNavigateUrlField
="StudentID"
DataNavigateUrlFormatString
="Show.aspx?ID={0}"
DataTextField
="StudentName"
HeaderText
="详细信息"
></
asp:HyperLinkColumn
>
</
Columns
>
<
PagerStyle
HorizontalAlign
="Center"
ForeColor
="DarkSlateBlue"
BackColor
="PaleGoldenrod"
></
PagerStyle
>
</
asp:DataGrid
></
FONT
>
</
form
>
</
body
>
</
HTML
>
查看全文
相关阅读:
读《见识》 | 当别人扇了你一巴掌
Java集合类
Java数据结构简述
Java加密算法
Java JDK与JRE
Java String、StringBuilder、StringBuffer[笔记]
Java同步(Synchronization)
Java断言(Assertion)
Java strictfp
Java Native Interface(JNI)
原文地址:https://www.cnblogs.com/ghd258/p/253218.html
最新文章
[Go]TCP服务中增加消息队列与工作池
[Go]TCP服务中读写进行协程分离
[PHP] RBAC权限与审批流的简单数据库构想
[Go] 轻量服务器框架tcp的粘包问题 封包与拆包
[PHP] socket客户端时的超时问题
[Go] 轻量服务器框架全局配置的实现以及解析json
[Go] 实现面向对象中的继承和覆盖方法
[Go] 轻量服务器框架基础TCP连接的抽象和封装
[Go] 利用函数类型实现封装中的回调
[Go] 轻量服务器框架基础TCP服务模块
热门文章
[PHP] 编译安装swoole
2018广东公考笔试成绩
分布式
负载均衡
关系数据库集群
正向代理与反向代理
Oauth2.0[笔记]
Tomcat结构
Java虚拟机(Java Virtual Machine)
读《小众时代》
Copyright © 2011-2022 走看看