---
title: "第四章：使用Rich控件"
author: "cpsing"
date: "2008-10-20T23:38:00Z"
tags: []
slug: "rich"
draft: false
lastmod: "2008-10-20T23:38:00Z"
id: 257010
url: "https://cpsing.iteye.com/blog/257010"
---

.ashx 文件用于写web handler的。其实就是带HTML和C#的混合文件。当然你完全可以用.aspx 的文件后缀。使用.ashx 可以让你专注于编程而不用管相关的WEB技术。&lt;o:p&gt;&lt;/o:p&gt;

可以用于不需要表现页面的处理程序。

![](/Images/OutliningIndicators/ContractedBlock.gif)![](/Images/OutliningIndicators/ExpandedBlockStart.gif)Code  
public class FileHandler : IHttpHandler {

    const string conString = @"Server=.\\SQLExpress;Integrated Security=True;  
        AttachDbFileName=|DataDirectory|FilesDB.mdf;User Instance=True";  
      
    public void ProcessRequest (HttpContext context) {  
        context.Response.ContentType = "application/msword";  
          
        SqlConnection con = new SqlConnection(conString);  
        SqlCommand cmd = new SqlCommand("SELECT FileBytes FROM Files WHERE Id=@Id", con);  
        cmd.Parameters.AddWithValue("@Id", context.Request\["Id"]);  
        using (con)  
        {  
            con.Open();  
            byte\[] file = (byte\[])cmd.ExecuteScalar();  
            context.Response.BinaryWrite(file);  
        }  
    }  
   
    public bool IsReusable {  
        get {  
            return false;  
        }  
    }

}

* * *

&lt;o:p&gt;

\*\** 上传大文件&lt;o:p&gt;&lt;/o:p&gt;

配置文件, httpRuntime maxRequestLength 和 httpRuntime requestLenghtDiskThreshold.

![](/Images/OutliningIndicators/ContractedBlock.gif)![](/Images/OutliningIndicators/ExpandedBlockStart.gif)Code  
void AddFile(string fileName, Stream upload)  
    {  
        SqlConnection con = new SqlConnection(conString);

        SqlCommand cmd = new SqlCommand("INSERT Files (FileName) Values (@FileName);" +  
          "SELECT @Identity = SCOPE\_IDENTITY()", con);

        cmd.Parameters.AddWithValue("@FileName", fileName);  
        SqlParameter idParm = cmd.Parameters.Add("@Identity", SqlDbType.Int);  
        idParm.Direction = ParameterDirection.Output;

        using (con)  
        {  
            con.Open();  
            cmd.ExecuteNonQuery();  
            int newFileId = (int)idParm.Value;  
            StoreFile(newFileId, upload, con);  
        }  
    }  
      
    void StoreFile(int fileId, Stream upload, SqlConnection connection)  
    {  
        int bufferLen = 8040;  
        BinaryReader br = new BinaryReader(upload);  
        byte\[] chunk = br.ReadBytes(bufferLen);

        SqlCommand cmd = new SqlCommand("UPDATE Files SET FileBytes=@Buffer WHERE Id=@FileId", connection);  
        cmd.Parameters.AddWithValue("@FileId", fileId);  
        cmd.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen).Value = chunk;  
        cmd.ExecuteNonQuery();  
                  
          
        SqlCommand cmdAppend = new SqlCommand("UPDATE Files SET FileBytes .WRITE(@Buffer, NULL, 0) WHERE Id=@FileId", connection);  
        cmdAppend.Parameters.AddWithValue("@FileId", fileId);  
        cmdAppend.Parameters.Add("@Buffer", SqlDbType.VarBinary, bufferLen);  
        chunk = br.ReadBytes(bufferLen);  
          
        while (chunk.Length &gt; 0)  
        {  
            cmdAppend.Parameters\["@Buffer"].Value = chunk;  
            cmdAppend.ExecuteNonQuery();  
            chunk = br.ReadBytes(bufferLen);  
        }

        br.Close();  
    }

* * *

&lt;/o:p&gt;

读取数据库大二进制byte\[]

* * *

![](/Images/OutliningIndicators/ContractedBlock.gif)![](/Images/OutliningIndicators/ExpandedBlockStart.gif)Code

context.Response.Buffer = false;  
using (con)  
        {  
            con.Open();  
            SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess);  
            if (reader.Read())  
            {  
                int bufferSize = 8040;  
                byte\[] chunk = new byte\[bufferSize];  
                long retCount;  
                long startIndex = 0;

                retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize);

                  
                while (retCount == bufferSize)  
                {  
                    context.Response.BinaryWrite(chunk);

                    startIndex += bufferSize;  
                    retCount = reader.GetBytes(0, startIndex, chunk, 0, bufferSize);  
                }

                byte\[] actualChunk = new Byte\[retCount - 1];  
                Buffer.BlockCopy(chunk, 0, actualChunk, 0, (int)retCount - 1

);  
                context.Response.BinaryWrite(actualChunk);  
                                 
            }  
        }

&lt;o:p&gt;&lt;/o:p&gt;

\*\*\*&lt;o:p&gt;&lt;/o:p&gt;

使用Memu控件和Muliview可以实现tab标签调用效果。&lt;o:p&gt;&lt;/o:p&gt;

重要的是使用css&lt;o:p&gt;&lt;/o:p&gt;

   .tab&lt;o:p&gt;&lt;/o:p&gt;

        {&lt;o:p&gt;&lt;/o:p&gt;

            border:solid 1px black;&lt;o:p&gt;&lt;/o:p&gt;

            background-color:#eeeeee;&lt;o:p&gt;&lt;/o:p&gt;

            padding:2px 10px;&lt;o:p&gt;&lt;/o:p&gt;

        }&lt;o:p&gt;&lt;/o:p&gt;

        .selectedTab&lt;o:p&gt;&lt;/o:p&gt;

        {&lt;o:p&gt;&lt;/o:p&gt;

            background-color:white;&lt;o:p&gt;&lt;/o:p&gt;

            border-bottom:solid 1px white;&lt;o:p&gt;&lt;/o:p&gt;

        }&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt;

* * *

&lt;/o:p&gt;

&lt;o:p&gt;&lt;/o:p&gt;

\*\*\*&lt;o:p&gt;&lt;/o:p&gt;

使用Memu控件和Muliview可以实现tab标签调用效果。&lt;o:p&gt;&lt;/o:p&gt;

重要的是使用css&lt;o:p&gt;&lt;/o:p&gt;

   .tab&lt;o:p&gt;&lt;/o:p&gt;

        {&lt;o:p&gt;&lt;/o:p&gt;

            border:solid 1px black;&lt;o:p&gt;&lt;/o:p&gt;

            background-color:#eeeeee;&lt;o:p&gt;&lt;/o:p&gt;

            padding:2px 10px;&lt;o:p&gt;&lt;/o:p&gt;

        }&lt;o:p&gt;&lt;/o:p&gt;

        .selectedTab&lt;o:p&gt;&lt;/o:p&gt;

        {&lt;o:p&gt;&lt;/o:p&gt;

            background-color:white;&lt;o:p&gt;&lt;/o:p&gt;

            border-bottom:solid 1px white;&lt;o:p&gt;&lt;/o:p&gt;

        }&lt;o:p&gt;&lt;/o:p&gt;

\*\*\*&lt;o:p&gt;&lt;/o:p&gt;

Mutiview可用于向导式的表单，在表单里的按钮识别以下命令：&lt;o:p&gt;&lt;/o:p&gt;

NextView ----commandName&lt;o:p&gt;&lt;/o:p&gt;

PreView    ----commandName&lt;o:p&gt;&lt;/o:p&gt;

SwitchViewByID  ------CommanArgument&lt;o:p&gt;&lt;/o:p&gt;

SwichViewByIndex ------CommanArgument&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt;&lt;/o:p&gt;

\*\*\*&lt;o:p&gt;&lt;/o:p&gt;

Wizard 控件用于向导式的表单跳转。&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt;&lt;/o:p&gt;

&lt;o:p&gt;&lt;/o:p&gt;