本文介绍了什么是一个超链接的EnableViewState做或什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么的EnableViewState一个超级链接或做呢?

What does EnableViewState on a HyperLink do or mean?

<asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="false">Register</asp:HyperLink>

这是什么意思?什么会做,如果我将它设置为true。谢谢!我看着它,但定义不是用简单的术语。

What does it mean? and what will it do if I set it to true. Thanks! I looked it up, but the definition was not in simple terms.

推荐答案

ViewState是用于保留在回传控制属性的状态。禁用这意味着任何属性设置编程方式(在code-后面)将不会在页面回发保持。然而,如果你声明声明的所有值(在.aspx页面),则禁用它不会使任何区别。

ViewState is used to persist the state of control properties across postbacks. Disabling it means that any properties you set programmatically (in code-behind) won't be persisted across page postbacks. However, if you declare all the values declaratively (in your .aspx page) then disabling it won't make any difference.

一个简单的例子:

假设你有这个ASPX标记了的ViewState启用:

Say you have this aspx mark-up with ViewState enabled:

<form id="form1" runat="server">
<div>
    <asp:HyperLink ID="RegisterHyperLink" runat="server" EnableViewState="true">Register</asp:HyperLink>
    <br /><br />
    <asp:Button ID="ButtonPostBack" runat="server" Text="Post Back" />
</div>
</form>

和你这样做在code-背后:

And you do this in code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        RegisterHyperLink.ForeColor = System.Drawing.Color.Red;
    }
}

即使你只设置超链接的前景色的颜色为红色的第一次加载的超级链接仍将点击执行回发的按钮后,红色。这是因为回发后超链接属性和ViewState中存储值重新创建它们。

Even though you only set the ForeColor of the HyperLink to the colour red on the first load the HyperLink will still remain red after clicking the Button that performs the postback. That is because ViewState stores the value of the HyperLinks properties and re-creates them after postback.

如果您尝试完全相同的事情,但与ViewState的超链接被禁用,当你点击提交按钮的超级链接将恢复到其原来的颜色。这是因为视图状态不是保存将其设置的事实是红色。

If you try the exact same thing but with ViewState disabled on the HyperLink, when you click the submit button the HyperLink will revert back to its original colour. That is because viewstate isn't "storing" the fact that you set it to be red.

在实际应用中可以正常禁用ViewState的,如果:

In practical terms you can normally disable ViewState if:

A)您的网页不执行任何回发
B)你声明方式设置的所有属性。

A) Your page doesn't perform any postbacksB) You set all the properties declaratively

如果你真的想了解的ViewState我建议你读的。

If you really want to understand ViewState I'd recommend reading TRULY Understanding ViewState.

这篇关于什么是一个超链接的EnableViewState做或什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 22:35