·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> Asp.net GridView控件使用纪要

Asp.net GridView控件使用纪要

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23
asp.net GridView控件使用纪要

1:数据绑定

GridView 支持数据绑定的数据源格式比较多,例如可以使用ObjectDataSource绑定数据源,

Dataset,datatable,List<T>等

2:列绑定

1)BoundField--一般直接绑定数据源对应的字段,通过指定DataField来实现绑定。

2)CheckBoxField--当GridView控件需要展示CheckBox控件时使用,也是通过DataField绑定一个bool类型的字段即可。

3)HyperLinkFied--绑定的列实现超链接功能,DataNavigateUrlFields="ID" DataNavigateUrlFormatString="XXX.aspx?ID{0}"

4)ImageField,

5)ButtonField,

6)CommandField--命令行列模板,内置增删改查,选择等功能(没有具体使用)

7)TemplateField --比较灵活,一般通过编辑列模板可以实现我们需要的功能。

3:GridView -OnRowDataBound事件

通过该事件我们可以为绑定的列指定事件等一系列操作

PRotected void FSLGridView1_RowDataBounding(object sender, GridViewRowEventArgs e)

{ if (e.Row.RowType == DataControlRowType.DataRow)

{

System.Data.DataRowView dv = (System.Data.DataRowView)e.Row.DataItem;

string ID = dv["ID"].ToString();

    Button btnCheck = (Button)e.Row.FindControl("BtnCheck");

    Button btnLook = (Button)e.Row.FindControl("BtnLook");

btnCheck.Attributes.Add("onclick", "changevalue('" + btnCheck.ClientID + "','" + ID + "');return false");

}

}

4:GridView -OnRowCommand事件

当我们为模板列里面的控件指定CommandName="linkDel" CommandArgument='<%#Eval("ID") %>'等参数时,

我们操作这些控件时就会触发该事件。protected void FSLGridView2_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "linkDel") { if (e.CommandArgument == null) { return; } string ID = e.CommandArgument.ToString(); EvaluateDataBLL bll = new EvaluateDataBLL(); if (bll.DeleteRowDataByID(ID)) { Framework.Assistant.Web.ClientScriptHelper.WriteAlert("success", "删除成功!"); Framework.Assistant.Web.ClientScriptHelper.RegisterScript("Close", "CloseWindow(true);"); } else { Framework.Assistant.Web.ClientScriptHelper.WriteAlert("Failure", "删除失败,请校正后重新操作!"); } } }

5:GirdView控件的遍历

foreach (GridViewRow item in this.FSLGridView1.Rows) {//TODO:The Things you want to DO }

6:列的格式化展示

<asp:TemplateField HeaderText="内容 <ItemStyle Width="15%" HorizontalAlign="Center" /> <ItemTemplate> <%# DecryptinfoContent(Eval("infoContent").ToString())%> </ItemTemplate></asp:TemplateField>

DecryptinfoContent--页面后台方法