Ajax e vb

martedì 14 ottobre 2008 - 10.27

memmo77 Profilo | Expert

Salve. Ho bisogno di valorizzare una session nel mio vb per controllare una funzione nella pagina aspx.
Alla fine della funzione io imposto:
Session("CONTROLLO_STAMPA_PDF") = 1

e nella mia funzione lato aspx:

<asp:ScriptManager ID="ScriptManager1" runat="server" AsyncPostBackTimeOut="20">
</asp:ScriptManager>
<script language="JavaScript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
var CONTROLLO_STAMPA_PDF = "<%=session("CONTROLLO_STAMPA_PDF")%>";

function InitializeRequest(sender, args) {
document.body.style.cursor = "wait";
}
function EndRequest(sender, args) {
//controllaProgres("100%");
document.body.style.cursor = "default";
//CONTROLLO APERTURA PDF FINALE
debugger;
if (CONTROLLO_STAMPA_PDF == '1'){
var ip_machine = "<%=Session("IP_MACHINE")%>";
var Path_Applicazione = "<%=Session("Path_Applicazione")%>";
var NomePdf = "<%=Session("NomePdf")%>";
var MESE = "<%=Session("MESE")%>";
var ANNO = "<%=Session("ANNO")%>";
var file = "cartella/pdf/COMPLESSIVI/" + NomePdf;
window.open(file);
}
}
</script>


Solo che le altre session le valorizzo nell'evento Page_Load e funzionano, mentre la session("CONTROLLO_STAMPA_PDF") si trova in una altra sub e non viene impostata.
Come posso risolvere?
Grazie

rossimarko Profilo | Guru

Ciao,

ti suggerisco di usare i pagemethods, ovvero dei metodi della pagina richiamati dal tuo codice javascript. Invece che fare il check sulla variabile richiami la funzione della pagina e guardi cosa ti ritorna.
A questo indirizzo: http://www.asp.net/ajax/documentation/live/tutorials/ExposingWebServicesToAJAXTutorial.aspx trovi un esempio nella sezione "Calling Static Methods in an ASP.NET Web Page"

Nell'esempio (http://www.asp.net/ajax/documentation/live/ViewSample.aspx?sref=Sys.Net.PageMethod%23PageMethod.aspx) sono stati creati appunto due metodi (con attributo WebMethod) che consentono di leggere e scrivere dalla session
-----------------------------------------
Rossi Marco
http://blogs.dotnethell.it/rossimarko

memmo77 Profilo | Expert

Ho aggiunto nella mia pagina:

<script runat="server">
' Get session state value.
<WebMethod()> _
Public Shared Function GetSessionValue(ByVal key As String) As String
Return DirectCast(HttpContext.Current.Session(key), String)
End Function
</script>

....
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" AsyncPostBackTimeOut="600">
<Scripts>
<asp:ScriptReference Path="scripts/PageMethod.js"/>
</Scripts>
</asp:ScriptManager>
<script language="JavaScript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);

function InitializeRequest(sender, args) {
document.body.style.cursor = "wait";
}
function EndRequest(sender, args) {
//controllaProgres("100%");
document.body.style.cursor = "default";
//CONTROLLO APERTURA PDF FINALE
debugger;
var CONTROLLO_STAMPA_PDF = GetSessionValue('CONTROLLO_STAMPA_PDF') //questa è la session che vorrei recuperare, ma non funziona.
if (CONTROLLO_STAMPA_PDF == '1'){
var ip_machine = "<%=Session("IP_MACHINE")%>";
var Path_Applicazione = "<%=Session("Path_Applicazione")%>";
var NomePdf = "<%=Session("NomePdf")%>";
var MESE = "<%=Session("MESE")%>";
var ANNO = "<%=Session("ANNO")%>";
var file = "pdf/COMPLESSIVI/" + NomePdf;
window.open(file);
}
}
</script>

....


Ho modificato il file PageMethod.js in questo modo:

// Gets the session state value.
function GetSessionValue(key)
{
PageMethods.GetSessionValue(key,
OnSucceeded, OnFailed);
}

// Callback function invoked on successful
// completion of the page method.
function OnSucceeded(result, userContext, methodName)
{
if (methodName == "GetSessionValue")
{
return(result);
}
}

// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
if(error !== null)
{
alert('errore');
}
}
if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

rossimarko Profilo | Guru

Avviando il debug il metodo nella pagina aspx viene richiamato?

Considera inoltre che il codice:

if (CONTROLLO_STAMPA_PDF == '1'){ var ip_machine = "<%=Session("IP_MACHINE")%>"; var Path_Applicazione = "<%=Session("Path_Applicazione")%>"; var NomePdf = "<%=Session("NomePdf")%>"; var MESE = "<%=Session("MESE")%>"; var ANNO = "<%=Session("ANNO")%>"; var file = "pdf/COMPLESSIVI/" + NomePdf; window.open(file);

dovrai inserirlo nell'evento di callback che gestisce la chiamata con successo. Questo perchè il metodo GetSessionValue è asincrono e non ottieni subito il valore della risposta, ma potrai leggerlo nella callback (se guardi l'esempio è il metodo onSucceeded)


-----------------------------------------
Rossi Marco
http://blogs.dotnethell.it/rossimarko

memmo77 Profilo | Expert

Allora, ho modificato la mia funzione sulla pagina aspx, remmando tutto il codice che dovrebbe andare nel file PageMethod.js

---------------------------------------------------------------------------------------------------------------
function EndRequest(sender, args) {
//controllaProgres("100%");
document.body.style.cursor = "default";
//CONTROLLO APERTURA PDF FINALE
debugger;
GetSessionValue('CONTROLLO_STAMPA_PDF');
//var CONTROLLO_STAMPA_PDF = GetSessionValue('CONTROLLO_STAMPA_PDF');//"<%=session("CONTROLLO_STAMPA_PDF")%>";
//if (CONTROLLO_STAMPA_PDF == '1'){
// var ip_machine = "<%=Session("IP_MACHINE")%>";
// var Path_Applicazione = "<%=Session("Path_Applicazione")%>";
// var NomePdf = "<%=Session("NomePdf")%>";
// var MESE = "<%=Session("MESE")%>";
// var ANNO = "<%=Session("ANNO")%>";
// var file = "pdf/COMPLESSIVI/" + NomePdf;
// window.open(file);
//}
}
---------------------------------------------------------------------------------------------------------------

Poi ho modificato il file js:
---------------------------------------------------------------------------------------------------------------
// Gets the session state value.
function GetSessionValue(key)
{
PageMethods.GetSessionValue(key,
OnSucceeded, OnFailed);
}

function OnSucceeded(result, userContext, methodName)
{
if (methodName == "GetSessionValue")
{
//return(result);
// var ip_machine = "<%=Session("IP_MACHINE")%>";
// var Path_Applicazione = "<%=Session("Path_Applicazione")%>";
// var NomePdf = "<%=Session("NomePdf")%>";
// var MESE = "<%=Session("MESE")%>";
// var ANNO = "<%=Session("ANNO")%>";
// var file = "pdf/COMPLESSIVI/" + NomePdf;
// window.open(file);
}
}

// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
if(error !== null)
{
alert('errore');
//displayElement.innerHTML = "An error occurred: " +
// error.get_message();
}
}

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
---------------------------------------------------------------------------------------------------------------

Solo che va sempre e solo nella funzione OnFailed stampandomi errore. Ma il nome della mia sessione che su vb sarebbe:
Session("CONTROLLO_STAMPA_PDF")

Alla funzione GetSessionValue, la passo bene così?
GetSessionValue('CONTROLLO_STAMPA_PDF');

Grazie della pazienza

rossimarko Profilo | Guru

che errore ti viene ritornato? Se scommenti il codice
if(error !== null) { alert('An error occurred: ' + error.get_message()); }

dovrebbe visualizzartelo
-----------------------------------------
Rossi Marco
http://blogs.dotnethell.it/rossimarko

memmo77 Profilo | Expert

Era un problema di tipo variabile che passavo, la funzione voleva stringa e io passavo una int32 . Comunque ho modificato la mia pagina aggiungendo sulla parte aspx:

// Gets the session state value.
function GetSessionValue(key)
{
PageMethods.GetSessionValue(key,
OnSucceeded, OnFailed);
}

function OnSucceeded(result, userContext, methodName)
{
if (methodName == "GetSessionValue")
{
//alert(result);
if (result == '1')
{
var ip_machine = "<%=Session("IP_MACHINE")%>";
var Path_Applicazione = "<%=Session("Path_Applicazione")%>";
var NomePdf = "<%=Session("NomePdf")%>";
var MESE = "<%=Session("MESE")%>";
var ANNO = "<%=Session("ANNO")%>";
var file = "pdf/COMPLESSIVI/" + NomePdf;
window.open(file);
}
}
}
// Callback function invoked on failure
// of the page method.
function OnFailed(error, userContext, methodName)
{
if(error !== null)
{
//alert('An error occurred: ' + error.get_message());
}
}


E la richiamo con GetSessionValue('CONTROLLO_STAMPA_PDF');

Funziona perfettamente. Grazie mille dell'aiuto. Ciao
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5