如何在坚持变回发 [英] How to Persist Variable on Postback

查看:65
本文介绍了如何在坚持变回发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个单页(背后的.vb code)和创建公共intFileID作为整数

在页面加载我检查查询字符串,如果为它分配可用的或者设置intFileID = 0。

 公共intFileID为整数= 0保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
    如果没有Page.IsPostBack然后
        如果不是的Request.QueryString(FILEID)是没有那么
            intFileID = CINT(的Request.QueryString(FILEID))
        万一        如果intFileID> 0,则
            的GetFile(intFileID)
        万一
    万一
结束小组私人小组的GetFile()
    使用intFileID检索数据库中的特定记录,并设置是各种textbox.text
结束小组

有对于提交按钮中插入或更新基于所述intFileID变量的值的记录的点击事件。我需要能够坚持在回发的值,它的所有工作。

页面只需插入或在SQL数据库更新的记录。我使用的不是一个GridView,FormView控件,DetailsView控件,或者其本身仍然存在键值任何其他RAD类型的对象,我不希望使用任何他们。

我怎么能坚持intFileID设置的值而不在其中也可能会被改变的HTML的东西。

更改Page_Load中使用的ViewState来持久intFileID值

 保护小组的Page_Load(BYVAL发件人为对象,BYVAL E上System.EventArgs)把手Me.Load
    如果没有Page.IsPostBack然后
    如果不是的Request.QueryString(FILEID)是没有那么
    intFileID = CINT(的Request.QueryString(FILEID))
    \t万一    如果intFileID> 0,则
    的GetFile(intFileID)
    \t万一    ViewState中(intFileID)= intFileID
    其他
    intFileID =的ViewState(intFileID)
    万一
结束小组


解决方案

正如其他人所指出的那样,你可以把它存储在Session或ViewState的。如果它是具体的页面,我喜欢将其存储在ViewState中,而不是在会议上,但我不知道,如果一个方法一般$ pferred比其他的p $。

在VB中,你会一个项目存储在ViewState这样的:

 的ViewState(密钥)=值

和检索,如:​​

  =值的ViewState(键)

I created a single page (with code behind .vb) and created Public intFileID As Integer

in the Page load I check for the querystring and assign it if available or set intFileID = 0.

Public intFileID As Integer = 0

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
        If Not Request.QueryString("fileid") Is Nothing Then
            intFileID = CInt(Request.QueryString("fileid"))
        End If

        If intFileID > 0 Then
            GetFile(intFileID)
        End If
    End If
End Sub

Private Sub GetFile()
    'uses intFileID to retrieve the specific record from database and set's the various textbox.text
End Sub

There is a click event for the Submit button that inserts or updates a record based on the value of the intFileID variable. I need to be able to persist that value on postback for it all to work.

The page simply inserts or updates a record in a SQL database. I'm not using a gridview,formview,detailsview, or any other rad type object which persists the key value by itself and I don't want to use any of them.

How can I persist the value set in intFileID without creating something in the HTML which could possibly be changed.

[EDIT] Changed Page_Load to use ViewState to persist the intFileID value

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If Not Page.IsPostBack Then
    	If Not Request.QueryString("fileid") Is Nothing Then
    		intFileID = CInt(Request.QueryString("fileid"))
    	End If

    	If intFileID > 0 Then
    		GetFile(intFileID)
    	End If

    	ViewState("intFileID") = intFileID
    Else
    	intFileID = ViewState("intFileID")
    End If
End Sub

解决方案

As others have pointed out, you can store it in the Session or the ViewState. If it's page specific, I like to store it in the ViewState as opposed to the Session, but I don't know if one method is generally preferred over the other.

In VB, you would store an item in the ViewState like:

ViewState(key) = value

And retrieve it like:

value = ViewState(key)

这篇关于如何在坚持变回发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆