转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
Taking a Bite Out of ASP.NET ViewState         ★★★★
Holds server resources? Yes No Times out? Yes – after 20 minutes (default) No Stores any .NET type? Yes No, limited support for: strings, integers, Booleans, arrays, ArrayList, hashtable, custom TypeConverters Increases "HTML payload"? No Yes

Getting the Best Performance with ViewState

Each object must be serialized going into ViewState and then deserialized upon post back, so the performance cost of using ViewState is definitely not free. However, there''''s usually no significant performance impact if you follow some simple guidelines to keep your ViewState costs under control.

  • Disable ViewState when you don''''t need it. The next section, Getting Less from ViewState, covers this in detail.
  • Use the optimized ViewState serializers. The types listed above have special serializers that are very fast and optimized to produce a small ViewState footprint. When you want to serialize a type not listed above, you can greatly improve its performance by creating a custom TypeConverter for it.
  • Use the fewest number of objects, and if possible, reduce the number of objects you put into ViewState. For example, rather than a two-dimensional string array of names/values (which has as many objects as the length of the array), use two string arrays (only two objects). However, there is usually no performance benefit to convert between two known types before storing them in ViewState—then you''''re basically paying the conversion price twice.

Getting Less from ViewState

ViewState is enabled by default, and it''''s up to each control—not the page developer—to decide what gets stored in ViewState. Sometimes, this information is not useful to your application. While it''''s not harmful either, it can dramatically increase the size of the page sent to the browser. It''''s a good idea to turn off ViewState if you are not using it, especially where the ViewState size is significant.

You can turn off ViewState on a per-control, per-page, or even per-application basis. You don''''t need ViewState if:

  • The page doesn''''t post back to itself.
  • You aren''''t handling the control''''s events.
  • The control has no dynamic or data bound property values (or they are set in code on every request).

The DataGrid control is a particularly heavy user of ViewState. By default, all of the data displayed in the grid is also stored in ViewState, and that''''s a wonderful thing when an expensive operation (like a complex search) is required to fetch the data. However, this behavior also makes DataGrid the prime suspect for unnecessary ViewState.

For example, here''''s a simple page that meets the criteria above. ViewState is not needed because the page doesn''''t post back to itself.

Figure 3. Simple page LessViewState.aspx with DataGrid1

<%@ Import Namespace="System.Data" %>
<html>
    <body>
        <form runat="server">
            <asp:DataGrid runat="server" />
        </form>
    </body>
</html>
<script runat="server">

    Private Sub Page_Load(sender As Object, e As EventArgs) 

        Dim ds as New DataSet()
        ds.ReadXml(Server.MapPath("TestData.xml"))

        DataGrid1.DataSource = ds
        DataGrid1.DataBind()

    End Sub

</script>

With ViewState enabled, this little grid contributes over 3000 bytes to the HTML payload for the page! You can see this using ASP.NET Tracing, or by viewing the source of the page sent to the browser, as shown in the code below.

<HTML>
    <HEAD>
        <title>Reducing Page "HTML P

上一页  [1] [2] [3] [4]  下一页


[办公软件]Word表格中Shift+Alt+方向键的妙用  [系统软件]A REVIEW OF SQLEXPLORER PLUG-IN
[VB.NET程序]Read a string at a given address  [VB.NET程序]Read a byte, integer or long from memory
[Delphi程序]// I have a comment ----Delphi 研发人员谈注释 …  [Delphi程序]// I have a comment  ----Delphi 研发人员谈注释
[Delphi程序]Creating a real singleton class in Delphi 5  [Delphi程序]Download a file from a FTP Server
[Delphi程序]2004.11.29.Starting a Project  [Delphi程序]How can I create a tray icon

Taking a Bite Out of ASP.NET ViewState

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3082 更新时间:2009/4/23 10:46:57
ent sort order for a static list of data in ViewState.<br> Click the link in the column header to sort the data by that field.<br> Click the link a second time to reverse the sort direction. <br><br><br> <asp:datagrid id="DataGrid1" runat="server" OnSortCommand="SortGrid" BorderStyle="None" BorderWidth="1px" BorderColor="#CCCCCC" BackColor="White" CellPadding="5" AllowSorting="True"> <HeaderStyle Font-Bold="True" ForeColor="White" BackColor="#006699"> </HeaderStyle> </asp:datagrid> </P> </form> </body> </HTML> <script runat="server"> // SortField property is tracked in ViewState string SortField { get { object o = ViewState["SortField"]; if (o == null) { return String.Empty; } return (string)o; } set { if (value == SortField) { // same as current sort file, toggle sort direction SortAscending = !SortAscending; } ViewState["SortField"] = value; } } // SortAscending property is tracked in ViewState bool SortAscending { get { object o = ViewState["SortAscending"]; if (o == null) { return true; } return (bool)o; } set { ViewState["SortAscending"] = value; } } void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { BindGrid(); } } void BindGrid() { // get data DataSet ds = new DataSet(); ds.ReadXml(Server.MapPath("TestData.xml")); DataView dv = new DataView(ds.Tables[0]); // Apply sort filter and direction dv.Sort = SortField; if (!SortAscending) { dv.Sort += " DESC"; } // Bind grid DataGrid1.DataSource = dv; DataGrid1.DataBind(); } void SortGrid(object sender, DataGridSortCommandEventArgs e) { DataGrid1.CurrentPageIndex = 0; SortField = e.SortExpression; BindGrid(); } </script>

Here''''s the code for testdata.xml, referenced in both code sections above:

<?xml version="1.0" standalone="yes"?>
<NewDataSet>
  <Table>
    <pub_id>0736</pub_id>
    <pub_name>New Moon Books</pub_name>
    <city>Boston</city>
    <state>MA</state>
    <country>USA</country>
  </Table>
  <Table>
    <pub_id>0877</pub_id>
    <pub_name>Binnet &amp; Hardley</pub_name>
    <city>Washington</city>
    <state>DC</state>
    <country>USA</country>
  </Table>
  <Table>
    <pub_id>1389</pub_id>
    <pub_name>Algodata Infosystems</pub_name>
    <city>Berkeley</city>
    <state>CA</state>
    <country>USA</country>
  </Table>
  <Table>
    <pub_id>1622</pub_id>
    <pub_name>Five Lakes Publishing</pub_name>
    <city>Chicago</city>
    <state>IL</state>
    <country>USA</country>
  </Table>
  <Table>
    <pub_id>1756</pub_id>
    <pub_name>Ramona Publishers</pub_name>
    <city>Dallas</city>
    <state>TX</state>
    <country>USA</country>
  </Table>
  <Table>
    <pub_id>9901</pub_id>
    <pub_name>GGG&G</pub_name>
    <city>München</city>
    <country>Germany</country>
  </Table>
  <Table>
    <pub_id>9952</pub_id>
    <pub_name>Scootney Books</pub_name>
    <city>New York</city>
    <state>NY</state>
    <country>USA</country>
  </Table>
  <Table>
    <pub_id>9999</pub_id>
    <pub_name>Lucerne Publishing</pub_name>
    <city>Paris</city>
    <country>France</country>
  </Table>
</NewDataSet>

Session State or ViewState?

There are certain cases where holding a state value in ViewState is not the best option. The most commonly used alternative is Session state, which is generally better suited for:

  • Large amounts of data. Since ViewState increases the size of both the page sent to the browser (the HTML payload) and the size of form posted back, it''''s a poor choice for storing large amounts of data.
  • Secure data that is not already displayed in the UI. While the ViewState data is encoded and may optionally be encrypted, your data is most secure if it is never sent to the client. So, Session state is a more secure option. (Storing the data in the database is even more secure due to the additional database credentials. You can add SSL for even better link security.) But if you''''ve displayed the private data in the UI, presumably you''''re already comfortable with the security of the link itself. In this case, it is no less secure to put the same value into ViewState as well.
  • Objects not readily serialized into ViewState, for example, DataSet. The ViewState serializer is optimized for a small set of common object types, listed below. Other types that are serializable may be persisted in ViewState, but are slower and generate a very large ViewState footprint.
  Session State ViewState Pages Controls
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台