zoukankan
html css js c++ java
DataGrid 完全攻略之五(使用DropDownList)
前台代码:html
<%
@ Page language
=
"
c#
"
Codebehind
=
"
UseDropDown.aspx.cs
"
AutoEventWireup
=
"
false
"
Inherits
=
"
MsDataGrid.UseDropDown
"
%>
<!
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: 31px; POSITION: absolute; TOP: 93px"
runat
="server"
Width
="842px"
Height
="172px"
BorderColor
="Tan"
BorderWidth
="1px"
BackColor
="LightGoldenrodYellow"
CellPadding
="2"
GridLines
="None"
ForeColor
="Black"
PageSize
="1"
AutoGenerateColumns
="False"
>
<
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
="生日"
DataFormatString
="{0:yyyy-M-d}"
></
asp:BoundColumn
>
<
asp:BoundColumn
DataField
="Email"
HeaderText
="邮件地址"
></
asp:BoundColumn
>
<
asp:TemplateColumn
HeaderText
="性别模板列"
>
<
ItemTemplate
>
<
asp:DropDownList
id
="ddlSexI"
runat
="server"
Enabled
="False"
>
<
asp:ListItem
Value
="1"
>
男
</
asp:ListItem
>
<
asp:ListItem
Value
="0"
>
女
</
asp:ListItem
>
</
asp:DropDownList
>
</
ItemTemplate
>
<
EditItemTemplate
>
<
asp:DropDownList
id
="ddlSexE"
runat
="server"
>
<
asp:ListItem
Value
="1"
>
男
</
asp:ListItem
>
<
asp:ListItem
Value
="0"
>
女
</
asp:ListItem
>
</
asp:DropDownList
>
</
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
>
</
Columns
>
<
PagerStyle
HorizontalAlign
="Center"
ForeColor
="DarkSlateBlue"
BackColor
="PaleGoldenrod"
></
PagerStyle
>
</
asp:DataGrid
></
FONT
>
</
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
MsDataGrid
{
/**/
///
<summary>
///
WebForm1 的摘要说明。
///
</summary>
public
class
UseDropDown : 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();
foreach
(DataGridItem dgi
in
dgShow.Items)
{
//
以下绑定非编辑状态下拉列表
DropDownList ddI
=
(DropDownList)dgi.FindControl(
"
ddlSexI
"
);
if
(ddI
!=
null
)
{
bool
bSex
=
(
bool
)ds.Tables[
"
studentinfo
"
].Rows[dgi.ItemIndex][
"
Sex
"
];
if
(bSex)
ddI.SelectedIndex
=
0
;
else
ddI.SelectedIndex
=
1
;
}
//
以下绑定编辑状态下拉列表
DropDownList ddE
=
(DropDownList)dgi.FindControl(
"
ddlSexE
"
);
if
(ddE
!=
null
)
{
bool
bSex
=
(
bool
)ds.Tables[
"
studentinfo
"
].Rows[dgi.ItemIndex][
"
Sex
"
];
if
(bSex)
ddE.SelectedIndex
=
0
;
else
ddE.SelectedIndex
=
1
;
}
}
}
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.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_CancelCommand(
object
source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgShow.EditItemIndex
=
-
1
;
BindData();
}
private
void
dgShow_PageIndexChanged(
object
source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
dgShow.CurrentPageIndex
=
e.NewPageIndex;
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
=
((DropDownList)(e.Item.FindControl(
"
ddlSexE
"
))).SelectedItem.Value;
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();
}
}
}
查看全文
相关阅读:
网站测试中如何做好安全性测试
Web安全性测试总结
文件上传验证绕过技术总结
Burp Suite使用介绍
Burpsuite教程与技巧之HTTP brute暴力破解
burpsuite绕过本地javascripte上传文件
文件上传漏洞演示脚本之js验证
上传验证绕过
Burp Suite详细使用教程
关于post和get传递参数的区别
原文地址:https://www.cnblogs.com/ghd258/p/253198.html
最新文章
PHP 获取客户端ip地址
小程序开发文档
写一个函数,获取一篇文章内容中的全部图片,并下载
php目录分隔符DIRECTORY_SEPARATOR
php中的<?= ?>替换<?php echo ?>
全面了解 Nginx 到底能做什么
博客园主题美化
火狐浏览器显示“已阻止载入混合活动内容“的解决方法
博客园—打赏功能
杨列昂:腾讯移动分析与服务架构
热门文章
腾讯云AI应用产品总监王磊:AI 在传统产业的最佳实践
腾讯云AI平台张文杰:构建一站式机器学习服务平台
董朝:打造云存储服务——移动端数据存储与分发
甘恒通:腾讯信鸽海量移动推送服务构建
胡泽锐:移动开发即服务——腾讯云移动开发平台技术分享
新时代运维重器 Tencent Hub 最佳实践——云+未来峰会开发者专场回顾
大咖说:如何借助腾讯云简单、高效移动开发
日调度万亿次,微服务框架TSF大规模应用——云+未来峰会开发者专场回顾
万物智联,腾讯云 IoT 边缘计算揭秘——云+未来峰会开发者专场回顾
WebScarab使用说明
Copyright © 2011-2022 走看看