我有一個多視圖,並有2個視圖裏面。我要粘貼一個示例代碼。在多視圖中使用視圖狀態跨回傳傳遞值
<asp:MultiView ID="MultiView1" ActiveViewIndex="0" runat="server">
<asp:View ID="View1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="view1" />
<asp:Label ID="Label2" runat="server" ></asp:Label>
</asp:View>
<asp:View ID="View2" runat="server">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="view2" />
</asp:View>
</asp:MultiView>
我希望txtbox1中的值在回發中。雖然多視圖保持狀態,我做response.redirect將查詢字符串傳遞給view2。由於我做回發,我不能使用txtbox1(在view1),在view2中的值。回發期間,txtbox1中的值變爲null。我嘗試下面的代碼
Public Partial Class viewstatetest
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (Not Page.IsPostBack()) Then
MultiView1.ActiveViewIndex = 0
Else
TypedPassword = TextBox1.Text
TextBox1.Attributes.Add("value", TypedPassword)
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
MultiView1.ActiveViewIndex = 1
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button2.Click
MultiView1.ActiveViewIndex = 0
Label1.Text = TextBox1.Text
Response.Redirect("viewstatetest.aspx")
End Sub
Public Property TypedPassword() As String
Get
If (ViewState("TypedPassword") IsNot Nothing) Then
Return CStr(ViewState("TypedPassword"))
End If
Return ""
End Get
Set(ByVal value As String)
ViewState("TypedPassword") = value
End Set
End Property
End Class
頁面加載後的第一次,我鍵入txtbox1在廠景的東西,點擊按鈕,視圖2加載和我有得到txtbox1和值碼將值inlabel1寫入view1中。當我做response.redirect時,textbox1變爲null,並且view也變爲null。
爲什麼viewstate中沒有價值?
謝謝!
實際上,我認爲viewstate被「拋棄」了,因爲MVC沒有服務器端控件,這是首先使用viewstate的地方。 –
100%同意。雞和雞蛋。 – randomfactor
Buck-AWK!很好地分析發生了什麼,否則,順便說一句。 +1 –