【问题标题】:Fetching session value from webmethod gives error从 webmethod 获取会话值会出错
【发布时间】:2014-03-26 11:31:48
【问题描述】:

我已经在 webmethod 中设置了当前页面的会话值,我正在尝试获取这些会话值并将它们设置为 javascript 中的变量。
在我的 webmethod 中,这是设置会话值的代码:

HttpContext.Current.Session.Add("benchmark", ds.Tables(0).Rows(0)("bm"))

它有效并且 ds.Tables(0).Rows(0)("bm") 给出 2。以下是我的脚本,它无法获取会话值并将值设为 0。

var bm='';

我的完整 javscript 是:

function ddlpf_SelectedIndexChanged(selectedItemValue)
{
   btn_enable(false);
   var i;
   var userid='<%=Session("userid").toString %>';
   if(ddlpf.selectedIndex > 0)
   {                  
   PageMethods.setpfdetails2(ddlpf.options[ddlpf.selectedIndex].value, userid,onSuc,onErr);
   function onSuc(res){'<%=Session("benchmark")="' + res + '" %>';}
   function onErr(){}
   var bm='<%=Session("benchmark").toString %>';
   var ses='<%=Session("hcur").toString %>';          
   }
alert(bm);
}

我的完整网络方法是:

<WebMethod(EnableSession:=True)> _
<ScriptMethod()> _
   Public Shared Function setpfdetails2(ByVal pfid As String, ByVal uid As String) As String
    Dim da As New dataaccess, para(2) As Object, ds As New DataSet
    Dim strtemp() As String = {"hcur", "mtm", "amtdis", "ratedis", "rd", "dtformat", "day1type", "benchmark", "covmtm", "dtsep", "impset"}
    Try
        For Each strt As String In strtemp
            If HttpContext.Current.Session.Item(strt) IsNot Nothing Then HttpContext.Current.Session.Remove(strt)
        Next
    Catch ex As Exception
    End Try
    custrefdetails1(pfid, uid)
    Try
        para(0) = "SPADD" : para(1) = uid : para(2) = pfid
        ds = da.retds1("select_portfolio", conn, para)
        If ds.Tables(0).Rows.Count > 0 Then
            HttpContext.Current.Session.Add("hcur", ds.Tables(0).Rows(0)("hcur"))
            HttpContext.Current.Session.Add("mtm", ds.Tables(0).Rows(0)("mtm"))
            HttpContext.Current.Session.Add("amtdis", ds.Tables(0).Rows(0)("amtdis"))
            HttpContext.Current.Session.Add("ratedis", ds.Tables(0).Rows(0)("ratedis"))
            HttpContext.Current.Session.Add("rd", ds.Tables(0).Rows(0)("rd"))
            HttpContext.Current.Session.Add("dtformat", ds.Tables(0).Rows(0)("dtformat"))
            HttpContext.Current.Session.Add("day1type", ds.Tables(0).Rows(0)("day1"))
            HttpContext.Current.Session.Add("benchmark", ds.Tables(0).Rows(0)("bm"))
            HttpContext.Current.Session.Add("covmtm", ds.Tables(0).Rows(0)("covmtm"))
            HttpContext.Current.Session.Add("covcancel", ds.Tables(0).Rows(0)("cover"))
            HttpContext.Current.Session.Add("mtmamtsel", ds.Tables(0).Rows(0)("mtmbx"))
            HttpContext.Current.Session.Add("dtsep", ds.Tables(0).Rows(0)("sep"))
            HttpContext.Current.Session.Add("impset", ds.Tables(0).Rows(0)("importset"))
        End If
        HttpContext.Current.Session.Add("datasheettype", ds.Tables(1).Rows(0)(0))
    Catch ex As Exception
    Finally
        ds = Nothing : da = Nothing
    End Try
    Return HttpContext.Current.Session.Item("benchmark")
End Function

【问题讨论】:

  • 不应该是 var bm=''
  • '';给出错误
  • 这里的问题是&lt;%=Session("benchmark")%&gt; 只在第一个页面加载时执行,总是给你留下相同的值。如果您想要基于setpfdetails2 所做的benchmark,那么您的WebMethod 应该返回该基准值,然后您应该在onSuc 中捕获它。看看这篇文章:semenoff.dk/en/Code-Corner/ASP.Net.AJAX/…
  • 好的,如果我从 onsuc 获取值,那么如何将其设置为会话? @RGraham
  • 您可以在服务器端随时在会话中设置它,但从会话中读取的代码只会在页面加载时执行。刷新页面,您会看到这些变量已更新

标签: javascript asp.net vb.net


【解决方案1】:

而不是会话变量,将其设置为隐藏字段

PageMethods.setpfdetails2(ddlpf.options[ddlpf.selectedIndex].value, userid,onSuc,onErr);
function onSuc(res)
{
   hbm.value=res;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2017-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2017-09-21
    相关资源
    最近更新 更多