32.Seven handy ViewState tips

32.7条便利的ViewState技巧

Every time I have to deal with a classic ASP.NET Web Forms application, one of the first things I look at is the resulting source, to check whether the DOM is a complete mess and whether the ViewState is an enormous, unnecessary blob of ugliness. Usually, they indicate what kind of mess will be found further down the stack. 每次我不得不处理一个传统的ASP.NET Web Forms应用时,首先查看生成的源码,检查DOM是否一团糟、ViewState是否是很大,多余的丑陋代码块。通常,它们预示着发现的烂摊子会更烂。

<input type=”hidden” name=”__VIEWSTATE” id=”__VIEWSTATE” value=”/wFzY3JpcHQ6IE9uY2xpY2s9J3dpbmRvdy5vcGVuKCJFcXVpcG1lbnQtRGV0YWlscy5hc3B4P1VzZWQtMjAxMC1UZXJl…(continues for 18,000 characters)…UlVTIiB3aWR0aD=” />
<input type=”hidden” name=”__VIEWSTATE” id=”__VIEWSTATE” value=”/wFzY3JpcHQ6IE9uY2xpY2s9J3dpbmRvdy5vcGVuKCJFcXVpcG1lbnQtRGV0YWlscy5hc3B4P1VzZWQtMjAxMC1UZXJl…(此处省略18,000个字)…UlVTIiB3aWR0aD=” />

Inadvertently using ViewState when it’s not necessary substantially increases the amount of data going back and forth, and can lead to a greater prevalence of invalid ViewState exceptions; the bigger the blob, the more likely it could be interrupted in transmission and not be posted back in entirety in a post. 当没有必要大幅度地增加往来数据量时无意中使用ViewState,可能导致更大的普遍无效的ViewState异常;代码块越大,越有可能在传输中中断并且可能在一次提交中不完全回发。

33.Unless you’re tracking a Text_Changed event, you don’t need ViewState enabled on TextBoxes and similar controls. Classic ASP.NET automatically repopulates TextBox.Text values upon postback, even without ViewState enabled. Turn it off on each TextBox with EnableViewState= “false” on each one. 除非跟踪一个Text_Changed事件之外,你不需在TextBoxes和类似的控件上要启用ViewState。传统的ASP.NET回发时会自动地填充TextBox.Text的值,甚至没有启用ViewState的情况下。在每个TextBox使用EnableViewState=“false”关掉ViewState。

You can do this for other controls like labels, but unless you’re setting their values after the page’s load event, you won’t reduce the size of the ViewState. 你可以在其它控件像labels禁用ViewState,但是除非在页面加载事件后设置它们的值,你将不能减少ViewState的大小。

34.The same goes for most implementations of Repeaters, ListViews, and so on. These are usually the biggest culprits and they can be ugly. The advantage of ViewState with these is avoiding having to populate values again in a postback. 这同样适用于Repeaters, ListViews, 等等大多数的控件实现。这些通常可能成为丑陋的罪魁祸首。ViewState对于这些的优势是避免不得不在回发时重新填充值。

If you’re convinced that it’s worth passing ViewState back and forth again and again to save your app the extra database hit…well…you’re probably wrong. Save the database hit (if you need to) with some caching and disable that dang ViewState on that Repeater! 如果你相信这是值得一次又一次地回传ViewState以便节省你的应用程序额外的数据库访问...好...你可能错了。使用一些缓存减少数据库访问(如果你需要)并且在Repeater上禁用那见鬼的ViewState才是正道!

35.If you’re re-binding data anyway, or just toggling one property on postback (asp:Panel anyone?), turn off that ViewState! Please! 如果你无论如何也要重新绑定数据,或者只是在回发时切换一个属性(asp:Panel 任何一个),请关掉ViewState!

36.If you do need ViewState, understand the page lifecycle and bind your data appropriately. A control loads its ViewState after Page_Init and before Page_Load, i.e. server controls don’t start tracking changes to their ViewState until the end of the initialization stage. 如果你确实需要使用ViewState,理解页面的生命周期并适当地绑定数据。控件在Page_Init之后Page_Load之前加载它的的ViewState,也就是说,服务端控件在结束初始化阶段之前都没有开始跟踪ViewState的改变。

Any changes to ViewState mean a bigger ViewState, because you have the before value and the after value. So, if you’re changing or setting a control’s value, set it before ViewState is being tracked, if possible. 任何ViewState的更改意味着更大的ViewState,因为你获得更改前后的值。所以,如果你正在改变或设置控件的值,如果可能的话,设置它之前的ViewState是正在跟踪的。

37.You may think it’s impossible to turn off ViewState on a DropDownList, even if you re-bind it on every postback. But with a tiny bit of elbow grease you can keep ViewState enabled and avoid passing all your option values back and forth. This is particularly worthwhile for DropDownLists with a big ListItem collection. 你也许认为在DropDownList上关掉ViewState是不可能的,即时你在每次回发时重新绑定它。但是随着一点点的费力工作你能够保持ViewState的启用并且避免反复地传输所有的选项值。

One way is to turn off ViewState and bind the select value manually to the actual posted value, like so: 一种方法是关闭ViewState并手动绑定实际传输的选项值,像这样:

string selectedId = Request[Countries.UniqueID];
if (selectedId != null)
Countries.SelectedValue = selectedId;

However, you may prefer something I came across more recently. Instead of binding your DropDown-List in the typical Page_Load or Page_Init, bind it in the control’s Init event: 然而,你可能更喜欢我最近的一些尝试。在控件的Init事件上绑定事件,而不是在传统的Page_Load或Page_Init事件绑定你的DropDown-List。

<asp:DropDownList ID=”Countries” ...
OnInit=”CountryListInit” />
protected void CountryListInit(object sender, EventArgs e)
{
Countries.DataSource = // get data from database
Countries.DataBind();
}

38.Make it your habit to turn off ViewState on every control by default, and only turn it on when you need it. If a page doesn’t need ViewState anywhere, turn it off at the page level. 你应该养成一个习惯:在默认情况下关闭所有控件的ViewState,只有当你需要的时候打开ViewState。如果一个页面不需要任何ViewState,在页面级关闭它。

You do all that work to reduce requests, combine and compress static references, and make sure your code is as clean as possible - don’t ruin it with a ViewState monster! If you’re anal, you can completely remove all traces of ViewState from pages that don’t need it by inheriting from a BasePage such as this: 你做所有工作来减少请求,合并和压缩静态引用,并确保你的代码尽可能的简洁 - 别让ViewState怪兽毁了它!If you’re anal(不知道怎么翻译),你可以继承一个基类页面,从页面完全移除所有的ViewState跟踪,像这样:

/// <summary>
/// BasePage providing cross-site functionality for pages that should not have ViewState enabled.
/// </summary>
public class BasePageNoViewState : Page // Or of course, inherit from your standard BasePage, which in turn inherits from Page
{
protected override void SavePageStateToPersistenceMedium(object viewState)
{
} protected override object LoadPageStateFromPersistenceMedium()
{
return null;
} protected override void OnPreInit(EventArgs e)
{
// Ensure that ViewState is turned off for every page inheriting this BasePage
base.EnableViewState = false;
base.OnPreInit(e);
}
}

【转载】7条便利的ViewState技巧的更多相关文章

  1. 教你50招提升ASP.NET性能(二十):7条便利的ViewState技巧

    (32)Seven handy ViewState tips 招数32: 7条便利的ViewState技巧 Every time I have to deal with a classic ASP.N ...

  2. 13条Android手机必备技巧 让玩机更有趣

    腾讯数码讯(编译:张秀梅)如果你不是一名极客或手机爱好者,那么或许对于手中的Android手机来说,肯定无法做到百分之百了解.对于这款世界上最受欢迎的操作系统来说,有许多不为大部分人所知晓的使用技巧或 ...

  3. (转载) Android Studio你不知道的调试技巧

    Android Studio你不知道的调试技巧 标签: android studio 2015-12-29 16:05 2514人阅读 评论(0) 收藏 举报  分类: android(74)    ...

  4. 数据库查询优化-20条必备sql优化技巧

    0.序言 本文我们来谈谈项目中常用的 20 条 MySQL 优化方法,效率至少提高 3倍! 具体如下: 1.使⽤ EXPLAIN 分析 SQL 语句是否合理 使⽤ EXPLAIN 判断 SQL 语句是 ...

  5. [转载]css代码优化的12个技巧

    1.ID 规则2.Class 规则3.标签规则4.通用规则对效率的普遍认识是从Steve Souders在2009年出版的<高性能网站建设进阶指南>开始,虽然该书中罗列的更加详细,但你也可 ...

  6. (转载) TextView使用一些小技巧

    TextView使用一些小技巧 标签: textviewandroid开发 2015-10-09 16:13 810人阅读 评论(0) 收藏 举报  分类: Android(20)  本文主要讲一些T ...

  7. 8条github使用小技巧

    1 简介 作为全球最大的开源及私有软件项目托管社区平台,github可以显著地帮助从事编程相关工作的人员提升自己的技术水平,也是费老师我日常最常浏览学习的技术类网站. github为了使得其使用更加便 ...

  8. 【转载】有哪些省时小技巧,是每个Linux用户都应该知道的

    http://www.cnblogs.com/amberly/p/4352682.html

  9. 【转载】一些VS2013的使用技巧

    1. Peek View 可以在不新建TAB的情况下快速查看.编辑一个函数的代码. 用法:在光标移至某个函数下,按下alt+F12. 然后在Peek窗口里可以继续按alt+F12.然后按ctrl+al ...

随机推荐

  1. 分数拆分(刘汝佳紫书P183)

    枚举,由已知条件推得y大于k,小于等于2K AC代码: #include"iostream"#include"cstring"using namespace s ...

  2. CUDA_one

    首先我看了讲解CUDA基础部分以后,大致对CUDA的基本了解如下: 第一:CUDA实行并行化的过程分为两部分,一个是线程块之间的并行(这是在每个线程网格中grid进行的),一个是对于每一个线程块内部各 ...

  3. 有三个数a,b,c要求按大小顺序将其输出<if,else语句的学习>

    #include <stdio.h> /* 有三个数a,b,c要求按大小顺序将其输出 ----------soulsjie 20170525------ */ void main(){ i ...

  4. Xcode4.5.1破解iOS免证书开发真机调试与ipa发布

    开发环境使用Mac OSX Mountain Lion 10.8 + Xcode 4.5.1,iOS设备需要越狱并从Cydia安装AppSync.Xcode4.5.1的安装破解详细步骤如下: 第一步, ...

  5. [K/3Cloud] 隐藏菜单后,如何在插件间接的调用隐藏菜单的操作

    使用场景: 动态表单里面挂了个单据的序时薄,序时薄有菜单,但是把序时薄的工具栏隐藏了.新增,修改全部动态表单自己写.删除和过滤我想间接调用下隐藏的序时薄的删除和过滤按钮的操作.在插件里如何实现? 答: ...

  6. Netbeans工具使用记录

     #自动换行设置 工具->选项->编辑器->格式设置

  7. Java Web 总结

    Java Servlet 总结 Servlet 简介 Servlet 是什么? 运行在Web服务器上的应用程序,作为浏览器和服务器之间的中间层. 与CGI功能类似,优点在于 性能更好 在Web服务器的 ...

  8. P1614 爱与愁的心痛

    洛谷——P1614 爱与愁的心痛 题目背景 (本道题目隐藏了两首歌名,找找看哪~~~) <爱与愁的故事第一弹·heartache>第一章 <我为歌狂>当中伍思凯神曲<舞月 ...

  9. easyUi 学习笔记 (二 ) 使用tabs 里datagridview 发送ajax请求 不访问后台的问题

    这个BUG 我花了一个半小时, 还是看不出哪里的问题,  于是就百度到这么一段话,我需要记住 <================================================= ...

  10. LOAP& its implimenlation

    LDAP 概念 LDAP的英文全称是Lightweight Directory Access Protocol,简称为LDAP,LDAP是轻量目录访问协议,LDAP是轻量目录访问协议.简单的说来,LD ...