Make It Works !
FastCounter by bCentral
blog里所做读书笔记引用的原文内容版权都归原书作者所有,如果侵权,请告知,谢谢。
blog里所翻译文章,原文版权以及程序代码的版权都为原作者所有。
联系方式:
说明:加我QQ的朋友,注明一下博客园,C#,.NET都可以,加我MSN的朋友请先往我的hotmail发一封邮件注明一下同样的东西,因为近期很多发广告的人加我的MSN,希望大家理解。
2008年4月4日 #
2007年12月12日 #
2007年7月25日 #
2007年1月21日 #
2006年9月30日 #
ViewState is a very misunderstood animal. I would like to help put an end to the madness by attempting to explain exactly how the ViewState mechanism works, from beginning to end, and from many different use cases, such as declared controls vs. dynamic controls.
ViewState是一只非常容易让人误解的动物。我想结束为了尝试解释清楚ViewState的工作机制而搞得一团糟的状况,从头到尾,从各种不同的用例,比如说declared controls vs. dynamic controls(术语我都不翻了哈)
There are a lot of great articles out there that try to dispel the myths about ViewState. You might say this is like beating a dead horse (where ViewState is the horse, and the internet is the assailant). But this horse isn't dead, let me tell you. No, he's very much alive and he's stampeding through your living room. We need to beat him down once again. Don't worry, no horses were harmed during the authoring of this article.
已经有许多很好好的文章尝试驱散ViewState的神秘。你可能会认为这就像打一只死马(这里ViewState是那只马,internet是打的人)但让我来告诉你,这只马并不是死的。其实它十分活泼,在你的卧室惊跑(这作者还真能掰的…)我们需要再一次把它击倒。别担心,写这篇文章的时候没有马会受伤.
It's not that there's no good information out there about ViewState, it's just all of them seem to be lacking something, and that is contributing to the community's overall confusion about ViewState. For example, one of the key features that is important to understand about ViewState is how it tracks dirtiness. Yet, here is a very good, in-depth article on ViewState that doesn't even mention it! Then there's this W3Schools article on ViewState that seems to indicate that posted form values are maintained via ViewState, but that's not true. (Don't believe me? Disable ViewState on that textbox in their example and run it again). And it's the #1 Google Search Result for "ASP.NET ViewState". Here is ASP.NET Documentation on MSDN that describes how Controls maintain state across postbacks. The documentation isn't wrong per say, but it makes a statement that isn't entirely correct:
"If a control uses ViewState for property data instead of a private field, that property automatically will be persisted across round trips to the client."
其实并不是外界没有关于ViewState的好的资料,只是他们都缺少了一些东西,而造成整个社区对于ViewState的迷惑。比如说,其中一个关键特性就是,理解ViewState怎么tracks dirtiness是很重要的。然后,这里有一片很好的,深入的关于ViewState的文章似乎指出posted form values使用viewstate维护,但这是不正确的。(不相信我?Disable他们那个例子上的textbox的ViewState,再运行一次)。这是关于"ASP.NET ViewState"的#1 Google Search Result。这里是ASP.NET Documentation on MSDN,解释了Controls怎么across postbacks维护ViewState.这个文档每一句话都没错,当时他得出的一个statement并不完全正确:
“如果一个控件为属性值使用ViewState来代替私有字段,这个属性会往返于客户端时自动被持久化”
That seems to imply that anything you shove into the ViewState StateBag will be round-tripped in the client's browser. NOT TRUE! So it's really no wonder there is so much confusion on ViewState. There is no where I've found on the internet that has a 100% complete and accurate explanation of how it works! The best article I have ever found is this one by Scott Mitchell. That one should be required reading. However, it does not explain the relationship of controls and their child controls when it comes to initialization and ViewState Tracking, and it is this point alone that causes a bulk of the mishandlings of ViewState, at least in the experiences I've had.
这好像暗示说,任何你扔给ViewState StateBag的东西,都会往返于客户浏览器。这不是真的!这么看来,对ViewState有这么对困惑真的一点都不奇怪了。我在internet真找不出能100%完全正确解释Viewstate是怎么工作的文章。我曾今找到最好的一片是this one by Scott Mitchell。这篇值得阅读。然而,这文章并没有解释控件和他们子控件在初始化和ViewState Tracking时的关系,而这正是造成大量误用ViewState的关键所在,至少在我的经验中是这样的。
So the point of this article will be to first give a complete understanding of how ViewState basically functions, from beginning to end, hopefully filling in the holes that many other articles have. After a complete explanation of the entire ViewState process, I will go into some examples of how developers typically misuse ViewState, usually without even realizing it, and how to fix it. I should also preface this with the fact that I wrote this article with ASP.NET 1.x in mind. However, there are very few differences in the ViewState mechanism in ASP.NET 2.0. For one, ControlState is a new type of ViewState in ASP.NET 2.0, but it treated exactly like ViewState, so we can safely ignore it for the purposes of this article.
所以这篇文章的首先会给出一个关于ViewState基本功能的一个完整理解,从头到尾,希望能够堵上很多其他文章的漏洞。在对整个ViewState过程的完全解释以后,我会展示一些开发人员怎么误用ViewState的例子,通常他们根本不会意识到它,和也不知道如何修复它。我首先声明我这篇文章写得一些情况是对于ASP.NET 1.X而言的。然而,ASP.NET 2.0对于ViewState机制只有极少的更改。其中一点,ControlState在ASP.NET 2.0 中是一种ViewState的新类型,但是它跟ViewState几乎是一样的处理方式,所以我们可以很安全的忽略它。
First let me explain why I think understanding ViewState to it's core is so important:
首先让我解释为什么理解ViewState的核心是很重要的:
MISUNDERSTANDING OF VIEWSTATE WILL LEAD TO...
错误理解ViewState会造成…
If you develop an ASP.NET Application and you don't take ViewState seriously, this could happen to you:
如果你开发ASP.NET程序而不严肃处理ViewState,这就会发生在你身上:
The ViewState form data
ViewState will add your web app's distinctiveness to it's own. Performance is futile
I could go on but that is the gist of it. Now lets move on by starting back from the beginning:
我可以继续,不过这些是它的要点。现在让我们从头开始:
WHAT DOES VIEWSTATE DO?This is a list of ViewState's main jobs. Each of these jobs serves a very distinct purpose. Next we'll learn exactly how it fulfills those jobs.
ViewState做了什么?
这里是ViewState所作的主要工作的一个列表。每一个工作都达到了一个不同的目的。接下来我们会了解到它怎么完成这些工作的。
While ViewState does have one overall purpose in the ASP.NET Framework, it's four main roles in the page lifecycle are quite distinct from each other. Logically, we can separate them and try to understand them individually. It is often the mishmash of information on ViewState that confuses people. Hopefully this breaks it down into more bite size nuggets. Mmmm... ViewState Nuggets.
由于ViewState全面参与了ASP.NET Framework,在页面生命周期中它的四个角色各不相同。逻辑上,我们可以分割他们,尝试单独一个一个理解他们。ViewState中混乱的信息经常使人们迷惑。希望它能被打碎成小到可以被咬的nuggets…ViewState Nuggets(这作者又来这招了…)
To be continued… Part2 http://www.cnblogs.com/wdxinren/archive/2006/09/30/519325.html
2006年9月28日 #
2006年9月7日 #
需求:我们的ASP.NET站点需要提供多语言支持,考虑到我们使用的是ASP.NET2.0,我们可以利用资源文件来实现。
NOTE:这里我们使用编程的方法实现,使用其他方法,请参见:
http://msdn2.microsoft.com/en-us/library/c6zyy3s9.aspx
接下来我们演示一个简单的例子
Steps:
Step1:在Visual Studio2005中创建一个ASP.NET站点
Step2:在网站项目中添加App_GlobalResources
Note:这里我们使用全局资源文件作为演示,本地资源文件操作步骤类似,关于全局资源文件与本地资源文件的异同,请参见
http://msdn2.microsoft.com/en-us/library/ms227427.aspx
中Choosing Between Global and Local Resource Files这一段
Step3:在App_GlobalResources中添加项,选择Resource File模板,取名为Site.resx
Note:这是我们的默认语言资源文件,必须要有
在这个资源文件的键/值配对中写入:
Name : btnSubmitText
Value: Submit
Step4:增加我们需要的各种语言的资源文件,这里我们增加两个文件
Site.zh-CN.resx
Value: 确定
Site.en-US.resx
Note:这里给资源文件取名的时候,需要按照如下格式:
[Class]. [Culture Name].resx
比如说Site.zh-CN.resx
关于Culture Name请参见MSDN中CultureInfo类中的Remarks部分
Step5:在我们的Default页面中添加一个Button控件,ID为btnSubmit
Step6:在我们的Default页面中添加两个Button控件,ID为btnChinese,btnEnglish,Text为中文,English
在btnChinese的Click事件方法中,添加代码:
在btnEnglish的Click事件方法中,添加代码:
Note:如果我们不在代码里指定当前线程的CurrentUICulture,那么程序就会根据浏览器中的设置进行判断,你可以在IE中的语言设置中进行默认设置。
OK, 我们可以运行了。
UAT就让大家来做吧,呵呵。不出所料,一切都如我们所预料的运行。
By the way, 最近发的几篇文章难度都不是很高,其实重要的是告诉大家一种可以选择的方法,或是大家都不是很注意的一些知识点, 引导大家如果有需要继续去发掘,MSDN里有的内容,我就简要介绍或者直接给出链接,那样就不会显得文章庞大臃肿,让大家集中注意力在某一些点上。Enjoy it J
PS:欢迎大家加我的MSN或者QQ讨论.NET 2.0(主要是C#2.0跟ASP.NET2.0吧)跟.NET3.0,发现我加入的几个QQ群都还挺少有人讨论这两个版本的内容的。还有一些朋友问我的很多问题,其实通过查找MSDN都可以找到答案跟示例,所以我的建议是,首先通过自己查找资料寻找解决方案。还有一些朋友直接向我要源码,或者说让我做一个MSDN示例里已经有代码的演示Project,那我的建议是,你还是自己先去尝试着做吧,我做好给你,你再看肯定没有你自己做一边效果来的好。
2006年9月4日 #
2006年8月31日 #
初学MemberShip,遇到一个Membership设置的问题
因为一般教程使用的示例都是SQL Server 2005 Express版本的本地 ASPNETDB.MDF文件,如果我要使用远程的Sql Server 2005服务器的话,需要进行一些设置。
Setp1:当你使用Membership, 我们可以发现ASP.NET 2.0 已经在 SQL Server 2005 Express建立了很多表(aspnet_Membership等),视图,存储过程等。当时我们自己的远程服务器上并没有这些表。难道我们需要自己建?当然不用,微软给我们准备了一个工具:
%systemroot%\Microsoft.NET\Framework\v2.x\
目录下的aspnet_regsql工具
根据它的向导,可以很方便地指定你的服务器和数据库。完成之后,我们可以发现,我们的远程服务器上制定数据库上已经创建好了我们需要的表,视图,存储过程等。
Step2:这时候我们需要在web.config更改Membership的Provider
我们可以参考machine.config文件中的内容:
(machine.config在%systemroot%\Microsoft.NET\Framework\v2.x\CONFIG目录下)
你可以直接在machine.config里更改,不过这里一更改,会影响到以后所有ASP.NET程序的设置,所以我们还是在自己的web.config中更改比较好
在web.config中,首先创建我们自己的连接字符串:
然后修改Membership的provider.,修改的时候需要注意一个问题,因为machine.config中已经存在一个provider了,所以你直接增加一个provider的话
在程序运行中还是会去寻找在machineconfig中定义的AspNetSqlMembershipProvider,因为那个是default的。所以一种选择是,我们把这个新的Provider设置为Default的。
另一种选择是,我们首先clear原来的provider,然后再增加自己的provider(connectiongStringName设置为我们自己的ConnectionString):
这里需要注意的另一个问题就是,provider的名字一定需要是AspNetSqlMembershipProvider,其他名字就会出现这个错误:Default Membership Provider could not be found.因为原来已经指定了AspNetSqlMembershipProvider为Default Provider.PS:此文只针对SQL Server数据库
Powered by: 博客园 Copyright © 小新0574