在Response.Redirect方法上释放会话变量值 [英] Loosing Session variable value on Response.Redirect method

查看:46
本文介绍了在Response.Redirect方法上释放会话变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



页面重定向有一个问题。这是示例代码

1)数据库连接 - >完成

2)插入数据 - >完成

3)在Session变量中添加值 - >完成

4)重定向到成功页面 - >问题出现在这里:(



Default1.aspx:

Hello Everyone,

There is one problem regarding Page Redirection. Here is Sample code
1) Database Connection -> Done
2) Insert Data -> Done
3) Add value inside Session variable -> Done
4) Redirect to success page -> Problem come up Here :(

Default1.aspx:

......
Dim success As String = "Success"
......
'btn_Click Event
    Try
        sqlCom.ExecuteNonQuery()
        sqlCon.Close()
        Session("success") = success
        MsgBox(Session("success") 'Msg Prints Properly

        modifyXmlData(filePath); ' Modifies the attribute value
        '1st Approach
        Response.Redirect("default2.aspx", False)
        '2st Approach
        'Server.Execute("default2.aspx", False)
        Catch ex As Exception
        MsgBox("Exception" + ex.Message)
        'lbl_Success.Text = ex.Message
    End Try
End Sub

Protected Sub modifyXmlData(ByVal filepath As String)
        Dim strPath = Server.MapPath(filepath)
        Dim xmlFile = XDocument.Load(strPath)

        xmlFile.<root>.<ListItem>.Where(Function(f) _
                        f.@value = ddl_SelectedValue).@visited = "Yes"
        xmlFile.Save(strPath)
End Sub

< br $> b $ b



defualt2.aspx

inside page_Load()
'MsgBox(System.Web.HttpContext.Current.Session("success")) ' o/p : Empty
Dim success As String = Session("success").toString
MsgBox(success) 'o/p : Empty
End Sub





第一种方法:使用响应

它正确地重定向到下一页但当我试图获得会话值时

它始终显示为空。 。



第二种方法:使用服务器

我担心这个选项,因为它保存会话值并且在重定向时它执行Default2.aspx的Page_Load事件,但在page_load控件被销毁并保留在我的Default1.aspx(第一页)



而不是Server.Execute我使用Server。转移,但它提出了例外 - 线程被中止



感谢你

请显示我纠正这个问题的方法



1st Approach : Using Response
It Redirects to next Page properly but when I am trying to get Session value
It Shows Empty always..

2nd Approach : Using Server
I am afraid with this option, because It holds the session value and at the time of redirection it executes the Page_Load event of Default2.aspx but after page_load Control get destroyed and stays on my Default1.aspx (first page)

instead of Server.Execute I used Server.Transfer but it raise the Exception -" thread is being aborted"

Thanking you
Please show me way to rectify this problem

推荐答案

试试

try
session["success"]="success";



因为如果你不使用引号而不是成功将被视为变量。使用此功能,您可以直接将字符串分配给会话。


beacause if you dont use quotes than success will be considered as a variable. Using this you can directly assign string to session.


这篇关于在Response.Redirect方法上释放会话变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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