ASP.NET 2.0 读书笔记 圣殿 之 玩转GridView(1)

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值。
         //我的感觉是,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;
        }

    }

}

原文地址:https://www.cnblogs.com/Jax/p/727274.html