从webmethod获取会话值会产生错误

时间:2014-03-26 11:31:49

标签: javascript asp.net vb.net

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

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

它的工作原理和ds.Tables(0).Rows(0)(“bm”)给出2.以下是我的脚本,它无法获取会话值并将值赋予0。

var bm ='<%= Session(“benchmark”)。toString%>';

我的完整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

1 个答案:

答案 0 :(得分:0)

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

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