首页 > 学院 > 开发设计 > 正文

Asp.net GridView控件使用纪要

2019-11-17 01:33:18
字体:
来源:转载
供稿:网友
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--页面后台方法


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表