blogjava's web log

          blogjava's web log
          ...

          【筆記】關于ViewState截獲移動到底部的問題

          關于把asp.net中ViewState 移動到頁面底部的好處,這個都不需要說了。
          1.更容易被搜索引擎收錄。
          2.頁面速度好像也有所提高.

          但當頁面又GridView 排序和分頁將出現問題。????
          思考中。
          看看有沒有什么辦法可以解決!


          關于asp.net怎么把ViewState移動頁面底部,給段代碼大家參考。
          1.新建個HttpModule 代碼如下。
          2.
          //在web.config插入下面?modules

          ??
          <httpModules>
          ???????
          <add?name="ViewstateModule"?type="blogjava.Net.ViewstateModule"?/>
          ????
          </httpModules>
          ?
          </system.web>
          他的原理就是在請求是aspx頁面得時候,的到請求得數據(網頁得源代碼)再根據網頁源代碼用字符串查找方式查找"
          input?type=\"hidden\"?name=\"__VIEWSTATE\ 再把他移動到底部。看覆蓋得Write 方法就一目了然了。


          #region?Using

          using?System;
          using?System.IO;
          using?System.Web;
          using?System.Text;
          using?System.Text.RegularExpressions;
          using?System.Web.UI;

          #endregion
          namespace Blogjava.Net
          {
          ????
          ///?<summary>
          ????
          ///?Removes?whitespace?from?the?webpage.
          ????
          ///?</summary>
          ????public?class?ViewstateModule?:?IHttpModule
          ????{

          ????????
          #region?IHttpModule?Members

          ????????
          void?IHttpModule.Dispose()
          ????????{
          ????????????
          //?Nothing?to?dispose;?
          ????????}

          ????????
          void?IHttpModule.Init(HttpApplication?context)
          ????????{
          ????????????context.BeginRequest?
          +=?new?EventHandler(context_BeginRequest);
          ????????}

          ????????
          #endregion

          ????????
          void?context_BeginRequest(object?sender,?EventArgs?e)
          ????????{
          ????????????HttpApplication?app?
          =?sender?as?HttpApplication;
          ??????????

          ????????????????
          if?(app.Request.Url.OriginalString.Contains(".aspx"))
          ????????????????{
          ????????????????????app.Response.Filter?
          =?new?ViewstateFilter(app.Response.Filter);

          ????????????????}
          ????????}

          ????????
          #region?Stream?filter

          ????????
          private?class?ViewstateFilter?:?Stream
          ????????{
          ????????????
          public?ViewstateFilter(Stream?sink)
          ????????????{
          ????????????????_sink?
          =?sink;
          ????????????}

          ????????????
          private?Stream?_sink;

          ????????????
          #region?Properites

          ????????????
          public?override?bool?CanRead
          ????????????{
          ????????????????
          get?{?return?true;?}
          ????????????}

          ????????????
          public?override?bool?CanSeek
          ????????????{
          ????????????????
          get?{?return?true;?}
          ????????????}

          ????????????
          public?override?bool?CanWrite
          ????????????{
          ????????????????
          get?{?return?true;?}
          ????????????}

          ????????????
          public?override?void?Flush()
          ????????????{
          ????????????????_sink.Flush();
          ????????????}

          ????????????
          public?override?long?Length
          ????????????{
          ????????????????
          get?{?return?0;?}
          ????????????}

          ????????????
          private?long?_position;
          ????????????
          public?override?long?Position
          ????????????{
          ????????????????
          get?{?return?_position;?}
          ????????????????
          set?{?_position?=?value;?}
          ????????????}

          ????????????
          #endregion

          ????????????
          #region?Methods

          ????????????
          public?override?int?Read(byte[]?buffer,?int?offset,?int?count)
          ????????????{
          ????????????????
          return?_sink.Read(buffer,?offset,?count);
          ????????????}

          ????????????
          public?override?long?Seek(long?offset,?SeekOrigin?origin)
          ????????????{
          ????????????????
          return?_sink.Seek(offset,?origin);
          ????????????}

          ????????????
          public?override?void?SetLength(long?value)
          ????????????{
          ????????????????_sink.SetLength(value);
          ????????????}

          ????????????
          public?override?void?Close()
          ????????????{
          ????????????????_sink.Close();
          ????????????}

          ????????????
          public?override?void?Write(byte[]?buffer,?int?offset,?int?count)
          ????????????{
          ????????????????
          byte[]?data?=?new?byte[count];
          ????????????????Buffer.BlockCopy(buffer,?offset,?data,?
          0,?count);
          ????????????????
          string?html?=?System.Text.Encoding.Default.GetString(buffer);

          ????????????????
          int?startPoint?=?html.IndexOf("<input?type=\"hidden\"?name=\"__VIEWSTATE\"");
          ????????????????
          if?(startPoint?>=?0)
          ????????????????{
          ????????????????????
          int?endPoint?=?html.IndexOf("/>",?startPoint)?+?2;
          ????????????????????
          string?viewstateInput?=?html.Substring(startPoint,?endPoint?-?startPoint);
          ????????????????????html?
          =?html.Remove(startPoint,?endPoint?-?startPoint);
          ????????????????????
          int?formEndStart?=?html.IndexOf("</form>")?-?1;
          ????????????????????
          if?(formEndStart?>=?0)
          ????????????????????{
          ????????????????????????html?
          =?html.Insert(formEndStart,?viewstateInput);
          ????????????????????}

          ????????????????}
          ????????????????
          byte[]?outdata?=?System.Text.Encoding.Default.GetBytes(html);
          ????????????????_sink.Write(outdata,?
          0,?outdata.GetLength(0));

          ????????????}
          ????????????
          #endregion

          ????????}

          ????????
          #endregion

          ????}
          }

          ?參考:http://www.hanselman.com/blog/ZippingCompressingViewStateInASPNET.aspx

          //end

          posted on 2006-12-16 10:33 record java and net 閱讀(446) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          常用鏈接

          留言簿(44)

          新聞檔案

          2.動態語言

          3.工具箱

          9.文檔教程

          友情鏈接

          搜索

          最新評論

          主站蜘蛛池模板: 崇州市| 岑溪市| 宣武区| 老河口市| 项城市| 万载县| 华坪县| 呼伦贝尔市| 开原市| 漾濞| 朝阳县| 桃江县| 邵东县| 莱芜市| 杭州市| 伊春市| 柳林县| 绥德县| 孟州市| 阿鲁科尔沁旗| 佳木斯市| 社会| 元谋县| 嵊泗县| 石屏县| 土默特左旗| 天气| 安阳市| 深州市| 雅江县| 沭阳县| 犍为县| 多伦县| 鹿邑县| 会理县| 类乌齐县| 呼玛县| 白城市| 黑河市| 鄂托克前旗| 年辖:市辖区|