Asp.net GridView控制項使用紀要

來源:互聯網
上載者:User

標籤:style   c   tar   ext   a   width   

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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.