我有一个多视图,里面有 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。由于我进行回发,因此我无法在 view2 中使用 txtbox1(在 view1 中)中的值。txtbox1 中的值在回发期间变为空。我尝试了以下代码
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
当页面第一次加载时,我在 view1 的 txtbox1 中输入一些内容并单击按钮,view2 被加载,我有一个代码获取 txtbox1 的值并将值写入 view1 中的标签 1。当我做 response.redirect 时,textbox1 变为空,视图也变为空。
为什么视图状态没有价值?
谢谢!