我知道即使ViewState被禁用TextBox,我們也不會丟失數據,因爲它實現了IPostBackDataHandler
接口。爲什麼標籤的價值改變了?
<asp:TextBox ID="TextBox1" runat="server" EnableViewState="False"/>
但我的問題是爲什麼這也發生在標籤呢?爲什麼標籤不會丟失數據,即使ViewState被禁用,因爲標籤沒有實現IPostBackDataHandler
接口?
<asp:Label ID="Label1" runat="server" EnableViewState="False" ViewStateMode="Disabled"/>
文本框定義:
public class TextBox : WebControl, IPostBackDataHandler,
標籤的定義:
public class Label : WebControl, ITextControl
我的代碼:
<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>
而後面的代碼:
protected void Button1_OnClick(object sender, EventArgs e)
{
Label1.Text = "Changed.";
}
點擊按鈕後,我希望看到標籤中的「點擊之前」,但點擊按鈕後,我看到標籤中的「已更改」文本。
你叫什麼「數據」?文本在標籤和文本框上?如果是這樣,這個文本如何在標記中設置? – Andrei
@Andrei在按鈕上單擊我寫這TextBox1.Text =「已更改」; Label1.Text =「已更改。」;我希望標籤會丟失數據,因爲我禁用了viewstate並且它沒有實現IPostBackDataHandler。 –
好吧,數據的含義很清楚,但不是「失去」的意思。所以你點擊改變文字。現在你期望什麼?頁面加載和控件具有「更改」以外的值?或者下一次回傳發生並且控件具有除「已更改」之外的其他值? – Andrei