zoukankan
html css js c++ java
ASP.NET2.0下含有DropDownList的GridView编辑、删除的完整例子!
<
asp:GridView
ID
="GridView1"
runat
="server"
AutoGenerateColumns
="False"
PageSize
="10"
Width
="542px"
AllowPaging
="True"
AllowSorting
="True"
DataKeyNames
="DB1_1,DB1_2"
OnRowCancelingEdit
="GridView1_RowCancelingEdit"
OnRowDeleting
="GridView1_RowDeleting"
OnRowEditing
="GridView1_RowEditing"
OnRowUpdating
="GridView1_RowUpdating"
OnPageIndexChanging
="GridView1_PageIndexChanging"
OnRowDataBound
="GridView1_RowDataBound"
OnSelectedIndexChanged
="GridView1_SelectedIndexChanged"
OnSorting
="GridView1_Sorting"
>
<
Columns
>
<
asp:TemplateField
HeaderText
="序号"
>
<
ItemTemplate
>
<%
# this.GridView1.PageIndex
*
this.GridView1.PageSize
+
this.GridView1.Rows.Count
+
1
%>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
="学历代码"
SortExpression
="DB1_1"
>
<
EditItemTemplate
>
<%
--<
asp:TextBox ID
=
"
TextBox1
"
runat
=
"
server
"
Text
=
'
<%# Bind("DB1_1") %>'></asp:TextBox>--%>
<
asp:DropDownList ID
=
"
ddlXL
"
runat
=
"
server
"
DataValueField
=
'
<%# Bind("DB1_1") %>'></asp:DropDownList>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label1
"
runat
=
"
server
"
Text
=
'
<%# Bind("xueliText") %>'></asp:Label>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField HeaderText
=
"
学历名称
"
SortExpression
=
"
DB1_2
"
>
<
EditItemTemplate
>
<
asp:TextBox ID
=
"
TextBox2
"
runat
=
"
server
"
Text
=
'
<%# Bind("DB1_2") %>'></asp:TextBox>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:Label ID
=
"
Label2
"
runat
=
"
server
"
Text
=
'
<%# Bind("DB1_2") %>'></asp:Label>
</
ItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField HeaderText
=
"
操作
"
ShowHeader
=
"
False
"
>
<
EditItemTemplate
>
<
asp:LinkButton ID
=
"
LinkButton1
"
runat
=
"
server
"
CausesValidation
=
"
True
"
CommandName
=
"
Update
"
Text
=
"
更新
"
></
asp:LinkButton
>
<
asp:LinkButton ID
=
"
LinkButton2
"
runat
=
"
server
"
CausesValidation
=
"
False
"
CommandName
=
"
Cancel
"
Text
=
"
取消
"
></
asp:LinkButton
>
</
EditItemTemplate
>
<
ItemTemplate
>
<
asp:LinkButton ID
=
"
LinkButton1
"
runat
=
"
server
"
CausesValidation
=
"
False
"
CommandName
=
"
Edit
"
Text
=
"
编辑
"
OnClientClick
=
"
return confirm('确认要编辑吗?');
"
></
asp:LinkButton
>
<
asp:LinkButton ID
=
"
LinkButton3
"
runat
=
"
server
"
CausesValidation
=
"
False
"
CommandName
=
"
Delete
"
Text
=
"
删除
"
OnClientClick
=
"
return confirm('确认要删除吗?');
"
></
asp:LinkButton
>
<
asp:LinkButton ID
=
"
LinkButton2
"
runat
=
"
server
"
CausesValidation
=
"
False
"
CommandName
=
"
Select
"
Text
=
"
选择
"
></
asp:LinkButton
>
</
ItemTemplate
>
</
asp:TemplateField
>
</
Columns
>
<
AlternatingRowStyle BackColor
=
"
Aquamarine
"
/>
</
asp:GridView
>
/**/
///
<summary>
///
绑定数据到GridView
///
</summary>
private
void
GridViewBind()
{
检索数据库
string
strSql
=
"
SELECT * FROM DB1
"
;
得到数据集
this
.GridView1.DataSource
=
conn.GetDs(strSql).Tables[
0
].DefaultView;
this
.GridView1.DataBind();
}
/**/
///
<summary>
///
编辑当前行
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
GridView1_RowEditing(
object
sender, GridViewEditEventArgs e)
{
GridView1.EditIndex
=
e.NewEditIndex;
//
当前编辑行背景色高亮
this
.GridView1.EditRowStyle.BackColor
=
Color.FromName(
"
#F7CE90
"
);
GridViewBind();
}
/**/
///
<summary>
///
取消编辑状态
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
GridView1_RowCancelingEdit(
object
sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex
=
-
1
;
GridViewBind();
}
/**/
///
<summary>
///
删除记录过程
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
GridView1_RowDeleting(
object
sender, GridViewDeleteEventArgs e)
{
//
得到单位编号
string
rowToDelete
=
GridView1.DataKeys[e.RowIndex].Values[
0
].ToString();
//
转换为整数
//
int ID=Convert.ToInt32(rowToDelete);
//
从数据库中删除
string
str
=
"
DELETE FROM DB1 where DB1_1=
"
+
"
'
"
+
rowToDelete
+
"
'
"
+
""
;
try
{
conn.RunSql(str);
//
重新绑定数据
GridViewBind();
}
catch
(Exception ex)
{
Response.Write(
"
数据库错误,错误原因:
"
+
ex.Message);
Response.End();
}
}
/**/
///
<summary>
///
更新记录过程
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
GridView1_RowUpdating(
object
sender, GridViewUpdateEventArgs e)
{
string
ID
=
GridView1.DataKeys[e.RowIndex].Values[
0
].ToString();
string
DB1_1
=
((TextBox)GridView1.Rows[e.RowIndex].FindControl(
"
TextBox1
"
)).Text;
//
string DB1_2 = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
string
DB1_2
=
(((DropDownList))GridView1.Rows[e.RowIndex].FindControl(
"
ddlXL
"
)).SelectedItem.Text;
//
判断表单项是否有空并给出提示信息
if
(DB1_1
==
""
||
DB1_2
==
""
)
{
conn.Alert(
"
请输入完整信息!
"
, Page);
return
;
}
try
{
conn.BuilderEdit(
"
select * from DB1 where DB1_1 ='
"
+
ID
+
"
'
"
);
conn.dr[
"
DB1_1
"
]
=
DB1_1;
conn.dr[
"
DB1_2
"
]
=
DB1_2;
conn.BuilderEditClose();
}
catch
(OracleException err)
{
if
(err.Code.ToString()
==
"
1
"
)
conn.Alert(
"
错误:已存在具有相同主键的记录
"
, Page);
else
conn.Alert(
"
错误:未能添加记录
"
, Page);
}
Response.Write(
"
<script language='javascript'>alert('数据已被保存!');</script>
"
);
//
返回浏览状态
GridView1.EditIndex
=
-
1
;
GridViewBind();
}
/**/
///
<summary>
///
分页事件
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
GridView1_PageIndexChanging(
object
sender, GridViewPageEventArgs e)
{
GridView1.PageIndex
=
e.NewPageIndex;
GridViewBind();
}
/**/
///
<summary>
///
加入鼠标效果及为DropDownList绑定值
///
</summary>
///
<param name="sender"></param>
///
<param name="e"></param>
protected
void
GridView1_RowDataBound(
object
sender, GridViewRowEventArgs e)
{
//
为DropDownList绑定值
if
(((DropDownList)e.Row.FindControl(
"
ddlXL
"
))
!=
null
)
{
DropDownList ddlXL
=
(DropDownList)e.Row.FindControl(
"
ddlXL
"
);
ddlXL.Items.Clear();
ddlXL.Items.Add(
new
ListItem(
"
博士
"
,
"
1
"
));
ddlXL.Items.Add(
new
ListItem(
"
硕士
"
,
"
2
"
));
ddlXL.Items.Add(
new
ListItem(
"
学士
"
,
"
3
"
));
}
//
加入鼠标滑过的高亮效果
if
(e.Row.RowType
==
DataControlRowType.DataRow)
//
判定当前的行是否属于datarow类型的行
{
//
当鼠标放上去的时候 先保存当前行的背景颜色 并给附一颜色
e.Row.Attributes.Add(
"
onmouseover
"
,
"
currentcolor=this.style.backgroundColor;this.style.backgroundColor='yellow',this.style.fontWeight='';
"
);
//
当鼠标离开的时候 将背景颜色还原的以前的颜色
e.Row.Attributes.Add(
"
onmouseout
"
,
"
this.style.backgroundColor=currentcolor,this.style.fontWeight='';
"
);
}
//
单击行改变行背景颜色
if
(e.Row.RowType
==
DataControlRowType.DataRow)
{
e.Row.Attributes.Add(
"
onclick
"
,
"
this.style.backgroundColor='#99cc00'; this.style.color='buttontext';this.style.cursor='default';
"
);
}
}
查看全文
相关阅读:
大数据在智慧城市中的应用
使用Java+NetBeans设计web服务和页面,用Tomcat部署网页
VS2013环境下配置OSG(3.4.0版本)
sort函数用于vector向量的排序
利用eigen库简单实现矩阵功能
elasticsearch搜索QueryStringQueryBuilder时的一些问题记录
Hbase中HMaster作用
国内加速git下载速度
JAVA中Integer.valueOf, parsetInt() String.valueOf的区别和结果
JAVA API操作hbase1.4.2
原文地址:https://www.cnblogs.com/ryb/p/561474.html
最新文章
[原创].菜农M0助学板PDMA读取ADC样本小练(寄存器操作方式)
[笔记].上拉电阻的作用之一 将 TTL电平提升至CMOS电平
[原创].串行ADC TLC549读取实验,Verilog版本
[笔记].关于使用JLINK的三线SWD模式调试NUC1xx的一点粗浅认识
[笔记].怎样解决MDK中的警告:Warning: L6305W: Image does not have an entry point. (Not specified or not set due to multiple choices.)
[转载].程序匠人 程序调试(除错)过程中的一些雕虫小技
[笔记].怎样开关LCD12864的背光以及调节其对比度?
[笔记].关于使用fscanf无法录入正确数据的原因分析
[笔记].LED阵列的照度与输入电压之关系
[转载].gdb调试器快速入门
热门文章
[转载].Craftor EZUSB FX2LP,CY7C68013A学习笔记[1]
[笔记].小练Verilog版本PWM驱动步进电机
[转载].一直不怎么明白PID的运算输出结果怎么换算成执行机构的控制量
[笔记].怎样解决gcc无法编译UTF8格式的C文件的问题
[转载].Craftor 关于一阶状态机跳转问题的研究与心得
(转) Asp.net中实现同一用户名不能同时登录
(转).net中的session与cookies区别及使用方法
验证码
(转)使用 HTML5 WebSocket 构建实时 Web 应用
使用dcmtk库读取.dcm文件并获取信息+使用OpenCV显示图像
Copyright © 2011-2022 走看看