簡體   English   中英

在VB.NET中編寫一個表單,該表單在返回時將保持相同狀態

[英]Write a form in VB.NET that will be the same state when you return to it

我正在嘗試形成一種形式,當它返回時,它將具有與控件相同的狀態

我有3個.net頁面,testa,testb,testc

testa,設置會話並有一個進入testb的按鈕。 testb有一個復選框和一個進入testc的按鈕。 testc有一個按鈕可以返回到testb頁面。

我想要這樣,當我們離開testc,然后再返回時,復選標記將處於相同狀態,但事實並非如此。 當我從頁面testc返回頁面testb時,它保持為取消選中狀態

這就是我所做的,在頁面加載事件中,我將復選框設置為等於“按鈕” Session變量。 在按鈕事件中,我將按鈕檢查狀態保存在Session變量中,並調用頁面testc

A頁

Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
    Server.Transfer("testb.aspx")
End Sub

b頁

Partial Class testb
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        Dim t As Boolean
        t = Session.Item("button")
        CheckBox1.Checked = t
    End Sub

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim t As Boolean

        t = CheckBox1.Checked
        Session.Item("button") = t

        Server.Transfer("testc.aspx")
    End Sub
End Class

c頁

Partial Class testc
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Server.Transfer("testb.aspx")
    End Sub
End Class

會話中的數據存儲為Object 您需要做的是從Session中獲取值,然后將其轉換為Boolean null (如果它不為null (VB中Nothing )。 我們以前使用過這種技術:

Dim rawValue As Object = Session.Item("button")
Dim realValue as Boolean

If rawValue IsNot Nothing Then
    realValue = Boolean.Parse(rawValue.ToString())
End If

聽起來您需要檢查Page_Load方法中的IsPostBack屬性,以決定是否應加載值。 如果是回發,請不要從會話中加載。

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        Dim t As Boolean = Session.Item("button")
        CheckBox1.Checked = t
    End If
End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM