书签 分享 收藏 举报 版权申诉 / 36

类型Gridview控件的使用.docx

  • 文档编号:9616168
  • 上传时间:2023-05-20
  • 格式:DOCX
  • 页数:36
  • 大小:732.62KB

Button1_Click方法:

protectedvoidButton1_Click(objectsender,EventArgse)

{

Label1.Text="";

//按行遍历cityList

foreach(GridViewRowgvrincityList.Rows)

{

//在每行中查找id为element的控件,强制转换成ChecBox类型

CheckBoxch=(CheckBox)gvr.FindControl("element");

//检查ch,如果处于选择状态

if(ch.Checked)

{

//累加Label的文本。

gvr.Cells[1].Text表示得到第二个元素的文本内容

Label1.Text+="选择的城市id为:

"+gvr.Cells[1].Text+"
";

}

}

}

还要在Page_Load方法中添加一个if判断:

当页面第一次加载时才执行对Gridview的数据绑定,如果没有这个判断的话,每次都重新绑定数据,原来的选择就失效了。

Page_Load的代码:

protectedvoidPage_Load(objectsender,EventArgse)

{

//第一次加载页面时

if(!

IsPostBack)

{

SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=demo;UserID=sa;Password=sa1");

DataSetcitySet=null;

try

{

SqlDataAdapteradapter=newSqlDataAdapter("select*fromcity",conn);

citySet=newDataSet();

adapter.Fill(citySet,"city");

}

catch(SqlExceptionex)

{

Console.WriteLine(ex.Message);

}

finally

{

conn.Close();

}

cityList.DataSource=citySet;

DataBind();

}

}

执行代码的效果:

在Gridview中显示图片

给t1表添加三条记录,img字段里存放的是图片的路径:

INSERTINTOt1(img)VALUES('./images/1.jpg')

INSERTINTOt1(img)VALUES('./images/2.jpg')

INSERTINTOt1(img)VALUES('./images/3.jpg')

在项目的images目录下应该已经有对应的图片存在了。

拖一个Gridview到页面,给这个Gridview添加两列:

第一列显示id字段的值,第二列显示图片:

图片列是通过TemplateField实现的。

页面代码:

GridViewID="cityList"runat="server"AutoGenerateColumns="False">

BoundFieldDataField="id"HeaderText="ID"/>

TemplateField>

图片

ImageID="img"runat="server"ImageUrl='<%#Eval("img")%>'/>

TemplateField>

GridView>

<%#Eval("img")%>表示输出当前行的img字段的值。

这个值赋给ImageUrl的属性,因为赋的值是图片的路径,图片控件就能显示对应的图片了。

后置C#代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data.SqlClient;

 

publicpartialclassDefault02:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=demo;UserID=sa;Password=sa");

DataSetcitySet=null;

try

{

SqlDataAdapteradapter=newSqlDataAdapter("select*fromt1",conn);

citySet=newDataSet();

adapter.Fill(citySet,"city");

}

catch(SqlExceptionex)

{

Console.WriteLine(ex.Message);

}

finally

{

conn.Close();

}

cityList.DataSource=citySet;

DataBind();

}

}

}

运行后的效果:

在Gridview中添加删除按钮

在Gridview中添加【删除CommandField】:

添加完后打开源代码试图,看到在节点下多了

CommandFieldShowDeleteButton="True"/>

这个控件会在Gridview的每一行形成一个[删除]超链接。

单击这个超链接会触发Gridview的删除事件。

下面给Gridview添加RowDeleting事件:

OnRowDeleting="cityList_RowDeleting"。

RowDeleting是由单击[删除]超链接时触发。

我们的思路是这样的:

单击[删除]超链接,Gridview会触发RowDeleting事件,然后在RowDeleting的后置代码中写删除的业务逻辑,将要删除的行删掉。

这就有个问题,我们删除一行肯定是根据这一行的主键进行删除,那如何将当前行的主键传递到cityList_RowDeleting方法中呢?

我们给Gridview添加DataKeyNames属性:

DataKeyNames="id"。

使用DataKeyNames属性指定表示数据源主键的字段,这里我们指定的字段是id。

当设置了DataKeyNames属性时,GridView控件用指定字段(id)的值填充它的DataKeys集合,这提供了一种访问每个行的主键的便捷方法。

aspx页面代码:

GridViewID="cityList"DataKeyNames="id"runat="server"AutoGenerateColumns="False"OnRowDeleting="cityList_RowDeleting">

BoundFieldDataField="id"HeaderText="id"/>

BoundFieldDataField="name"HeaderText="名称"/>

CommandFieldShowDeleteButton="True"/>

GridView>

用如下C#代码,在cityList_RowDeleting方法中得到DataKeyNames的id主键值。

stringid=cityList.DataKeys[e.RowIndex].Value.ToString();

C#后置代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

usingSystem.Data.SqlClient;

 

publicpartialclassDefault04:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

if(!

IsPostBack)

{

GetAll();

}

}

 

protectedvoidcityList_RowDeleting(objectsender,GridViewDeleteEventArgse)

{

stringid=cityList.DataKeys[e.RowIndex].Value.ToString();

SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=demo;UserID=sa;Password=sa");

DataSetcitySet=null;

try

{

conn.Open();

SqlCommandcmd=newSqlCommand("deletefromcitywhereid='"+id+"'");

cmd.Connection=conn;

cmd.ExecuteNonQuery();

}

catch(SqlExceptionex)

{

Console.WriteLine(ex.Message);

}

finally

{

conn.Close();

}

GetAll();

}

 

privatevoidGetAll()

{

SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=demo;UserID=sa;Password=sa");

DataSetcitySet=null;

try

{

SqlDataAdapteradapter=newSqlDataAdapter("select*fromcity",conn);

citySet=newDataSet();

adapter.Fill(citySet,"city");

}

catch(SqlExceptionex)

{

Console.WriteLine(ex.Message);

}

finally

{

conn.Close();

}

cityList.DataSource=citySet;

DataBind();

}

 

}

运行aspx页面后的效果:

这样就实现了点击[删除]链接后,删掉一条记录。

删除通常是要谨慎的,最好先让操作者再确认下,然后再进行删除,以避免误操作。

如何在Gridview删除前加上个确认对话框呢?

选择【删除CommandField】,点击右下角的【将此字段转换为TemplateField】链接,然后点【确定】按钮:

切换到源代码视图,发现原来的

CommandFieldShowDeleteButton="True"/>

现在变成了

TemplateFieldShowHeader="False">

LinkButtonID="LinkButton1"runat="server"CausesValidation="False"CommandName="Delete"Text="删除">

LinkButton>

TemplateField>

然后在

LinkButton>中添加提示脚本:

OnClientClick="returnconfirm('确认要删除吗?

');"

这时的Gridview控件代码:

GridViewID="cityList"DataKeyNames="id"runat="server"AutoGenerateColumns="False"OnRowDeleting="cityList_RowDeleting">

BoundFieldDataField="id"HeaderText="id"/>

BoundFieldDataField="name"HeaderText="名称"/>

TemplateFieldShowHeader="False">

LinkButtonID="LinkButton1"runat="server"CausesValidation="False"CommandName="Delete"Text="删除"OnClientClick="returnconfirm('确认要删除吗?

');">

LinkButton>

TemplateField>

GridView>

这样在单击【删除】链接

配套讲稿:

如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。

特殊限制:

部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。

关 键  词:
Gridview 控件 使用
提示  冰点文库所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:Gridview控件的使用.docx
链接地址:https://www.bingdoc.com/p-9616168.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2


收起
展开