ASP.NET 2.0 讀書筆記 聖殿 之 玩轉GridView(1)

來源:互聯網
上載者:User
1.新的文法,在aspx中設定
<%$ ConnectionStrings : NorthwindConnectionString %>

2.如果使用GridView通過DataSourceID綁定到SqlDataSource等資料來源控制項,則不用再使用GridView.DataBind()——會浪費效能。
   不能同時設定DataSource和DataSourceID。

3.支援七種FieldType:
   1)BoundField
         HtmlCode屬性設為true,對該欄位進行HTML編碼,從而防止惡意程式碼。
         readonly屬性為true時,才可以格式化;否則,編輯模式下,將ApplyFormatEditMode設為true,也能達到同樣效果。
         那個DataFormatString屬性沒有變化,可以這樣寫: Hi,{0}
         當格式化數字類型時,即 {0:000#},這時要把HtmlEncode設為false,這裡,有幾個#,就代表幾個數字
         NullDisplayText可以防止Null值,如果遇到,將其轉為自訂的文字;ConvertEmptyStringNull屬性設為true,將Null 字元串轉為null值。
         //我的感覺是,NullDisplayText真是個好東西,尤其是解決Datetime類型空資料的時候。
         
   2)ButtonField
         三種類型:Button/Link/Image,通過ButtonType擷取。
         Button按下時,激發RowCommand事件(DetailView控制項激發ItemCommand事件)
         如果某欄位的Command屬性="Order",則相應RowCommand事件如下:    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        int index = Convert.ToInt32(e.CommandArgument); //擷取行索引
        GridViewRow selectRow = GridView1.Rows[index];

        TableCell productName = selectRow.Cells[1];

        switch(e.CommandName)
        {
            case "Order":
                //do something
                break;
        }
    }

   3)CommandField
         預定義支援Select,Edit,Update,Delete(DetailView還支援Insert)
         刪除前的確認框:將Button的OnClientClick屬性設定為"return confirm('確定進行資料編輯?')"
         
         刪除前的取消:    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //注意這個e
        e.Cancel = true;

        //彈出Alert
        Literal txtMsg = new Literal();
        txtMsg.Text = "<script>alert('資料行刪除取消')</script>";
        Page.Controls.Add(txtMsg);
    }

   4)程式其實可以這麼寫:
以下是一個ImageHandler.ashx檔案,負責處理底層Handler:<%@ WebHandler Language="C#" Class="ImageHandler" %>

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public class ImageHandler : IHttpHandler 
{
    //取得資料庫連接設定
    static ConnectionStringSettings connString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"];

    public void ProcessRequest(HttpContext context)
    {
        MemoryStream ms=null;
        try
        {
            //取得員工代號
            string EmployeeID = context.Request.QueryString["EmployeeID"];
            //通過ReadImage類的GetImage()方法取得SQL Server中圖片資料
            //建立Sql命令
            string strSQL = "Select Photo from Employees where EmployeeID=@paramEmployeeID";
            //建立SqlDataSource
            SqlDataSource sqldsPhoto = new SqlDataSource(connString.ConnectionString, strSQL);
            sqldsPhoto.SelectParameters.Add("paramEmployeeID", TypeCode.Int32, EmployeeID);
            //通過SqlDataSource進行查詢
            DataView dv = (DataView)sqldsPhoto.Select(DataSourceSelectArguments.Empty);
            //返回DataView第一個Row的Photo欄位資料
            Byte[] PhotoImage = (Byte[])dv[0]["Photo"];
            ms = new MemoryStream(PhotoImage, 0, PhotoImage.Length);
        }
        catch
        {
        }
        
        if (ms != null)
        {
            //取得映像MemoryStream大小
            int bufferSize = (int)ms.Length;
            //建立 buffer
            byte[] buffer = new byte[bufferSize];
            //調用MemoryStream.Read,自MemoryStream 讀取至buffer,並返回count
            int countSize = ms.Read(buffer, 0, bufferSize);
            //返回映像buffer
            context.Response.OutputStream.Write(buffer, 0, countSize);
        }
    }
 
    public bool IsReusable 
    {
        get 
        {
            return false;
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.