问题:按钮控件和viewstate [英] question : button control and viewstate

查看:67
本文介绍了问题:按钮控件和viewstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,
我对按钮控件和视图状态有疑问.

1.假设我在页面中有三个按钮,那么如何识别页面被单击的按钮控件并在服务器端调用特定的按钮事件.

注意:我不需要代码.我只想知道页面如何处理按钮控制事件.

2. Viewstate在内部管理控件的状态,并将值填充在隐藏字段中,并对其进行哈希处理.
那么页面如何保留viewstate隐藏字段中的特定控件值?哪种方法将用于填充控件值?哪个密钥将用于解密隐藏字段值?

谢谢
Imrankhan

Hi friend,
I have questions regarding button controls and viewstate.

1. Suppose, I have three buttons in the page, So how page will be identify which button control is clicked and call particular button event in server side.

Note : I don''t want the code. I just want to know how page is handle button control event.

2. Viewstate internally manages the state of the control and value is filled up in hidden field and it is Hashed.
So how page retains the particular controls value from viewstate hidden field? which method will be used to fill up the controls value? which key will be used to decrypt the hidden field value?

Thanks
Imrankhan

推荐答案

以下链接将有助于理解您的问题.

http://msdn.microsoft.com/en-us/library/ms972976.aspx [ ^ ]

http://www.w3schools.com/aspnet/aspnet_viewstate.asp [
Following links will be useful to understand your questions.

http://msdn.microsoft.com/en-us/library/ms972976.aspx[^]

http://www.w3schools.com/aspnet/aspnet_viewstate.asp[^]

Hope this will help!


视图状态存储在哪里?

视图状态只是文本.它是页面上控件值的汇总.它是一个字符串,其中包含以某种方式哈希和编码的页面控件的值.视图状态不包含有关服务器或客户端的信息.它仅包含有关页面本身及其控件的信息.它与用户浏览器中的页面一起存在.
通常,视图状态会直接存储在页面中,因此它会来回移动.还记得旧的隐藏输入字段吗?视图状态不过是一个隐藏的输入,其中包含控制值的哈希.如果您查看ASP.NET Web表单的源代码,则会看到类似以下内容的内容:

Where Is View State Stored?

View state is simply text. It is an aggregate of values of controls on a page. It''s a string that contains values of page controls hashed and encoded in some manner. The view state contains no information about the server or the client. It only comprises information about the page itself and its controls. It lives along with the page in the user''s browser.
As a rule, view state is stored right in the page and therefore it travels with it back and forth. Remember good old hidden input fields? View state is nothing more than a hidden input which holds a hash of control values. If you view the source of an ASP.NET web form you will see something like this:

<input type="hidden" name="__VIEWSTATE" 
            value="CEbzzzEnmmz+Bc8IDFlnpgCLJ/HB00...">



头脑清醒的人都不会流利地阅读这些字符串.您在此处看到的是base64编码的字符串.我强调编码,因为有些人认为视图状态是加密的. Base64不是加密算法. Base64使字符串适合HTTP传输,并且使其难以阅读.一点点解码此字符串并查看其中内容很容易.这可能是安全问题吗?当然可以.我们会在适当的时候解决您的问题.坚持.

视图状态如何构建?

每个服务器控件最终都派生自Control类.无论是WebControl,HtmlControl还是LiteralControl,它都有一个名为EnableViewState的属性.构建页面时,启用此属性的每个控件都会通过序列化其内容(在这种情况下:将其内容转换为字符串)来贡献视图状态.现在,有些控件易于序列化,而另一些控件可能会让我们感到悲伤.管理视图状态的是StateBag类.此类就像字典,您可以在其中存储键/值对.这是在视图状态下存储有用数据的方式:



Nobody in sane mind can read these strings fluently. What you see here is a base64 encoded string. I emphasize encoded because some folks assume view state is encrypted. Base64 is not an encryption algorithm. Base64 makes a string suitable for HTTP transfer plus it makes it a little hard to read. Just a little. It''s easy to decode this string and see what''s inside. Can this be a security issue? It sure can. We''ll address your concern in due time. Stick around.

How Is View State Built?

Every server control ultimately derives from the Control class. Be it a WebControl, HtmlControl or even LiteralControl it has a property called EnableViewState. When a page is built every control that has this property enabled contributes to the view state by serializing its contents (in this case: converting its contents into a string). Now, some controls are easy to serialize, while others might give us grief.What manages the view state is the StateBag class. This class is like a dictionary—you may store key/value pairs in it. This is how you store a piece of useful data in the view state:

ViewState ["SortOrder"] = "email"



当页面回发时,其视图状态在服务器上被分解(解码),并且参与视图状态的每个控件都将恢复其值.您需要注意一个有趣的陷阱.某些控件会自动恢复其值(由ASP.NET提供),您无需维护其值!我将两个几乎完全相同的页面放在一起-一个是ASP.NET Web表单,另一个是纯HTML.ASP.NET页面的视图状态已完全关闭.单击提交后,查看其如何维护文本和选择内容.向下滚动到表单集合"以查看发布的内容. ASP.NET自动恢复这些值!

第二页是旧的传统" HTML.单击提交以获取无法恢复控制值的证明.这个故事的寓意是什么?您并不总是需要启用视图状态来维护页面状态. 不过我什么时候需要它?那是什么呢?"很高兴你问.参与视图状态的主要候选人是那些不使用HTTP表单回发的控件以及动态添加或填充的控件.让我举个例子.假设您有一个空的下拉列表,其中填充了数据库中的用户名.当页面首次运行时(!Page.IsPostback.响起铃声?),您需要对其进行数据绑定并用用户名填充.页面加载后会怎样?没有视图状态,下拉列表将为空.启用视图状态后,下拉列表内容将在回发时恢复.或者...每次页面回发时,您都必须从数据库中填充列表!如果您权衡数据库访问与视图状态之间的关系,比例尺将倾向于视图状态(除非用户列表如此之大,以至于您宁愿每次都对数据库执行ping操作,而不是四处拖动大型视图状态字符串).



When the page posts back its view state is taken apart (decoded) on the server and each control participating in the view state gets its value restored.There''s an interesting gotcha you need to be aware of. Some controls get their values restored automatically (courtesy of ASP.NET) and you don''t need to maintain their values! I put together two almost identical pages—one is an ASP.NET web form, and the other one is plain HTML.The ASP.NET page has its view state turned off completely. See how it maintains text and selections once you click Submit. Scroll down to "Form Collection" to see what was posted. ASP.NET restored these values automatically!

The second page is old, "traditional", HTML. Click Submit to receive proof that control values won''t be restored.What''s the moral of this story? You don''t always need view state enabled to maintain page state. "When do I need it though? What''s it for then?" Glad you asked. The prime candidates for participation in view state are those controls that don''t post back with the HTTP form and controls added or populated dynamically.Let me give you an example. Suppose you have an empty dropdown list which you populate with user names from the database. When the page runs for the first time (!Page.IsPostback. Rings a bell?) you need to databind it and fill it with user names. What happens once the page loads? Without view state the dropdown list will be empty. As you enable view state the dropdown list content will be restored on postback. Or... you would have to populate the list from the database every time the page posts back! If you weigh database access vs. view state the scale tips in favor of view state (unless the list of users is so huge that you''d rather ping the database each time instead of dragging around a giant view state string).


这篇关于问题:按钮控件和viewstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆