Asp.net GridView控件使用紀要

1:數據綁定this

GridView 支持數據綁定的數據源格式比較多,例如能夠使用ObjectDataSource綁定數據源,lua

Dataset,datatable,List<T>等命令行

 

2:列綁定orm

1)BoundField--通常直接綁定數據源對應的字段,經過指定DataField來實現綁定。事件

2)CheckBoxField--當GridView控件須要展現CheckBox控件時使用,也是經過DataField綁定一個bool類型的字段便可。ip

3)HyperLinkFied--綁定的列實現超連接功能,DataNavigateUrlFields="ID" DataNavigateUrlFormatString="XXX.aspx?ID{0}"string

4)ImageField,it

5)ButtonField,table

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--頁面後臺方法

相關文章
相關標籤/搜索