I am developing two pages.
On the first page, if user click a button("Download word file"), client side gets the html code in the specific <div>-tag then using ajax json to assign this value to session variable. script code is like this:
<script type="text/javascript">
var myDivObj = document.getElementById("testhtmltoword");
var data = "{Name:'myPageHTML'" + ", Value:'" + myDivObj.innerHTML + "'}";
$.ajax({
type: "POST",
url: "WebService1.asmx/SetSessionValue",
data: data,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert("success");
},
error: function (err) {
alert(data);
}
});</script>In the web service WebService1.asmx) I created a function(SetSessionValue(Name, Value)), code like below:
<WebMethod(EnableSession:=True)> _
Public Sub SetSessionValue(ByVal Name As String, ByVal Value As String)
Dim sessionVal As String = String.Empty
'If Not Session Is Nothing Then
Session(Name) = Value
'End If
End SubHowever, the session variable Session("myPageHTML") can't be set.
P.S : I solve it myself.
The solution is very simple, I did not put declaration on the top of the code.
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.<System.Web.Script.Services.ScriptService()>
After I put this in the webservice file, the ajax could visit this function and I got what I wanted.
Thank you all.