简体   繁体   English

为什么标签的值会改变?

[英]Why label's value is changed?

I know even if the ViewState is disabled for the TextBox, we are not losing the data because it implements the IPostBackDataHandler interface.我知道即使 TextBox 的 ViewState 被禁用,我们也不会丢失数据,因为它实现了IPostBackDataHandler接口。

<asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"/>

But my question is why this happens for label too?但我的问题是为什么标签也会发生这种情况? Why label is not losing it's data even if the ViewState is disabled since the label doesn't implements the IPostBackDataHandler interface?为什么即使 ViewState 被禁用,标签也不会丢失它的数据,因为标签没有实现IPostBackDataHandler接口?

<asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled"/>

TextBox definition:文本框定义:

public class TextBox : WebControl, IPostBackDataHandler,

Label definition:标签定义:

public class Label : WebControl, ITextControl

My code:我的代码:

<form id="form1" runat="server">
<div>
    <asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled" Text="Before click"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick"/>
</div>
</form>

And code behind:和后面的代码:

protected void Button1_OnClick(object sender, EventArgs e)
{
    Label1.Text = "Changed.";
}

I expected to see the "Before click" in my label after I clicked the button but I see the "Changed" text in my label after I clicked the button.单击按钮后,我希望在标签中看到“单击前”,但在单击按钮后,我在标签中看到了“已更改”文本。

I think you have wrong understanding what ViewState is.我认为您对 ViewState 的理解有误。

Data in ViewState is being stored BETWEEN requests, but not DURING page lifecycle. ViewState 中的数据存储在请求之间,而不是在页面生命周期中。 BTW - ViewState data is generated after PreRenderComplete event in SaveStateComplete event.顺便说一句 - ViewState 数据是在 SaveStateComplete 事件中的 PreRenderComplete 事件之后生成的。

https://msdn.microsoft.com/en-us/library/ms178472.aspx https://msdn.microsoft.com/en-us/library/ms178472.aspx

If you have ViewState switched off - just think that it will not be generated in output.如果您关闭了 ViewState - 只是认为它不会在输出中生成。

During page lifecycle all data assigned to controls(and also to page fields and properties, as the page is just a class) will be rendered as you defined in aspx.在页面生命周期中,分配给控件(以及页面字段和属性,因为页面只是一个类)的所有数据都将按照您在 aspx 中定义的方式呈现。 But will be lost after, unless is saved in ViewState.但是之后会丢失,除非是保存在ViewState中。

Ok I deleted my previous answer, I will try to re-state it with an example.好的,我删除了我之前的答案,我将尝试用一个例子重新说明它。

First, as others stated, the idea of ViewState is to hold the state between postbacks, rather than during a single page load cycle, so what you're seeing is the expected behavior.首先,正如其他人所说,ViewState 的想法是在回发之间保持状态,而不是在单个页面加载周期期间,因此您看到的是预期的行为。

To see the difference with an example, try adding your label with 2 buttons:要通过示例查看不同之处,请尝试使用 2 个按钮添加标签:

 <asp:Label ID="Label1" runat="server" EnableViewState="False" Text="Before click"></asp:Label>  
 <asp:Button ID="btn1" Text="Click" OnClick="btn1_Click" runat="server" />
 <asp:Button ID="btnReset" Text="Reset" OnClick="btnReset_Click" runat="server" />

Where btn1 sets the value to "Changed", and btnReset has an empty handler.其中btn1将值设置为“已更改”,而btnReset有一个空处理程序。

Now with EnableViewState set to False , if you click on btn1 the page reloads, btn1_Click is executed, and the page is rendered with the label value = "Changed", if you click btnReset the page will reload again, but since view state is disabled, the label will revert to its original text "Before click"现在EnableViewState设置为False ,如果您单击btn1页面重新加载,则执行btn1_Click ,并且页面呈现标签值 = "Changed",如果您单击btnReset页面将再次重新加载,但由于视图状态被禁用,标签将恢复为原始文本“点击前”

If you set EnableViewState to True on the lable and click btn1 then btnReset , the label value will stay as "Changed", since the state is kept during postbacks如果您设置EnableViewStateTrue的拉布勒并单击btn1然后btnReset ,标签值将保持为“改变”,因为状态回传过程中保持

Hope that clarifies things a bit希望能澄清一些事情

This is going to be long and detailed.这将是漫长而详细的。

Let's start with this markup.让我们从这个标记开始。 Almost identical to yours, with one additional button and a label.几乎与您的相同,只是多了一个按钮和一个标签。 I'll explain why we need them later.我稍后会解释为什么我们需要它们。

<form id="form1" runat="server">
<div>
    <asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled" Text="Before click"></asp:Label>
    <asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_OnClick"/>

    <asp:Label ID="Label2" runat="server" Text="Blah" />
    <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_OnClick"/>
</div>
</form>

And we'll use this code behind.我们将在后面使用这段代码。 Again, I'll explain the purpose of the second handler later on:同样,稍后我将解释第二个处理程序的目的:

protected void Button1_OnClick(object sender, EventArgs e)
{
    Label1.Text = "Changed";
}

protected void Button2_OnClick(object sender, EventArgs e)
{
    Label2.Text = Label1.Text;
}

Let's see what happens when we initially load the page.让我们看看当我们最初加载页面时会发生什么。 First ASP.NET reads all the markup, and then goes through the page life cycle with all the events.首先 ASP.NET 读取所有标记,然后处理所有事件的页面生命周期。 In markup parsing stage Label1 gets assigned text Before click , and it is never changed later during initial load.在标记解析阶段Label1 Before click获得分配的文本,并且在初始加载期间永远不会更改。 So later during rendering phase this is what is getting rendered into HTML and sent to browser.所以稍后在渲染阶段,这就是渲染成 HTML 并发送到浏览器的内容。 Thus Before click is displayed on the screen.因此Before click显示在屏幕上。 Nice and easy.又好又容易。

Now we click Button1 .现在我们单击Button1 Postback occurs, which is a term that describes a request sent to the same page by one of its controls after it was initially loaded.发生回发,这是一个术语,它描述了在初始加载后由其控件之一发送到同一页面的请求。 Again, everything starts with ASP.NET parsing the markup, and again Label1 gets assigned text Before click .同样,一切都从 ASP.NET 解析标记开始,并且Label1再次获得分配文本Before click But then page life cycle events happen, including control event handlers.但是随后会发生页面生命周期事件,包括控制事件处理程序。 And in the handler for Button1 the text of Label1 is changed to Changed .Button1的处理程序中, Label1的文本更改为Changed Now here is the important thing to note: if ViewState for the Label1 was enabled, this new value would be stored in there, and so called tracking would be enabled for property Label1.Text .现在需要注意的重要一点是:如果启用了Label1 ViewState,这个新值将存储在那里,并且将为属性Label1.Text启用所谓的跟踪。 But ViewState is disabled, and so the new value is not stored anywhere except Label1 .但是 ViewState 被禁用,因此新值不会存储在除Label1之外的任何地方。 Next comes the rendering page stage, and since Label1.Text still holds the value Changed this is what is rendered into HTML and sent to the browser to display.接下来是渲染页面阶段,由于Label1.Text仍然持有Changed值,这就是渲染成 HTML 并发送到浏览器以显示的内容。 Note however that this new value is not sent inside the ViewState field.但是请注意,此新值不会在ViewState字段内发送。 As you can see, whether ViewState is enabled or not plays no role in what is displayed after this postback.如您所见,是否启用 ViewState 对此次回发后显示的内容没有任何影响。

Now we'll click Button2 , which would trigger another postback.现在我们将单击Button2 ,这将触发另一个回发。 Again, ASP.NET parses the markup, again Label1 gets text Before click .再次,ASP.NET 解析标记,再次Label1获取文本Before click Then comes ViewState loading part.然后是 ViewState 加载部分。 If ViewState for Label1.Text was enabled, it would load changed value into this property.如果启用了Label1.Text ViewState,它会将更改后的值加载到此属性中。 But ViewState is disabled, and so the value stays the same.但是 ViewState 被禁用,因此该值保持不变。 Thus, when we reach Button2 click handler, the value of Label1.Text is still Before click , which is assigned to Label2.Text .因此,当我们到达Button2单击处理程序时, Label1.Text的值仍然是Before click ,它被分配给Label2.Text But Label2 has ViewState enabled, and so this new value for its text is stored in ViewState and sent to the client (ViewState is implemented as a hidden field on the client side).但是Label2启用了 ViewState,因此这个文本的新值存储在 ViewState 中并发送到客户端(ViewState 在客户端作为隐藏字段实现)。 When everything gets to the browser, we can see Label1 and Label2 both displaying Before click .当一切都到达浏览器时,我们可以看到Label1Label2都显示Before click

And to nail it down we'll do a third postback, now clicking Button1 again.为了确定它,我们将进行第三次回发,现在再次单击Button1 Just as during the first postback, Label1 ends up with Changed text.就像在第一次回发期间一样, Label1Changed text 结束。 But what about Label2 ?但是Label2呢? Well, this one has ViewState enabled, so during initial markup parsing ASP.NET assigns it the value Blah , and during ViewState loading it replaces this value with Before click .嗯,这个启用了 ViewState,因此在初始标记解析期间 ASP.NET 为其分配值Blah ,并在 ViewState 加载期间它用Before click替换该值。 Nothing else affects this value during the page life cycle (we did not click Button2 this time), and so we end up seeing Changed and Before click in the browser.在页面生命周期中没有其他任何影响此值(我们这次没有单击 Button2),因此我们最终在浏览器中看到ChangedBefore click

Hopefully it makes it clear what the ViewState is for and what disabling it does.希望它能清楚地说明 ViewState 的用途以及禁用它的作用。 If you want to dive even deeper into how ViewState works, I would greatly recommend this article: TRULY Understanding ViewState .如果您想更深入地了解 ViewState 的工作原理,我强烈推荐这篇文章: 真正理解 ViewState

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM