All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
asp.net2.0中GridView控件应用实例

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/9 1:36:21 Browse times: 331 Comment times: 0

asp.net2.0中GridView控件应用实例


Head photo

Go homepage
Upload pictures
Write articles

asp.net2.0中GridView控件应用实例

GridView控件应用实例
在开发某些管理网站或系统时,常常需要通过选中某行前面的CheckBox复选框对该行信息进行操作。本实例通过在GridView控件中添加一个CheckBox模板列来删除指定行信息,运行结果如图1所示。

图1 通过CheckBox删除选中记录
程序开发步骤如下所示。
(1)新建一个网站,命名为22_01,其主页默认为Default.aspx。
(2)在Default.aspx页面中添加一个Table表格,用来布局页面,然后在该Table表格中添加两个TextBox控件和一个Button控件,分别用来输入管理员姓名、管理员密码和执行登录功能。
(3)在该网站中添加一个Web页面,命名为Manger Page.aspx。在该页中,首先添加一个Table表格,用于布局页面,然后再在该Table表格中添加一个GridView控件、一个CheckBox控件和3个Button控件,分别用来显示用户基本信息、将GridView控件中数据全部选中、执行取消、删除和添加用户信息操作。
(4)主要程序代码如下所示。
当“全选”CheckBox框中选项发生改变时,循环访问GridView控件中的每一项,并使用FindControl方法取得TemplateField模板列中添加的CheckBox控件,然后建立该控件的引用对象,并使用该对象进行操作,实现全选功能。CheckBox控件的CheckedChanged事件代码如下:
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count-1;i++ )
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
If (CheckBox2.Checked == True)
{
cbox.Checked = True;
}
else
{
cbox.Checked = False;
}
}
}
单击【全删】按钮时,程序先判断GridView控件中哪些行被选中,并对选中行执行删除操作,然后对GridView控件进行重新绑定,以显示数据表中最新信息。【全删】按钮Click事件代码如下:
protected void btnAllDelete_Click(object sender, EventArgs e)
{
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
if (cbox.Checked == True)
{
string sqlstr = "delete from tb_Member where MemberID='"+GridView1.DataKeys[i].Value+"'";
SqlConnection myConn = GetConnection();
SqlCommand myCmd = new SqlCommand(sqlstr, myConn);
myConn.Open();
myCmd.ExecuteNonQuery();
myConn.Close();
}
}
bind();
}
完整程序代码如下:
★ ★★★★Default.aspx页面设计文件完整程序代码★★★★★
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>



无标题页




width: 256px; height: 74px" bordercolor="#003333">


管理员姓名:





管理员密码:





&nbsp;
&nbsp;&nbsp;&nbsp;





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.