accessing session objects using Javascript

inzo21

Regular
Joined
Nov 5, 2003
Messages
74
Hello all,

I'm trying to do the followingIn the Page_Load event of my web app, I have created some session variables that store string values for user.

I need to access these values (both get and set) from a user control using Javascript. Thus the options I am considering are:

1. Write properties in the code behind the user control that would progammatically set these and then access those properties from the JavaScript code .

2. Access the session variables directly from the script code in the page without using code-behind.

I know not where to begin with either - other then the code behind stuff. I could not find an object in java to access the session variable either.

any help would be greatly appreciated.

thanx in advance.

inzo
 
In the ASP.NET design layout, under the "HTML Controls" tab of
the toolbox, add a Hidden control to the page. You can
programmatically access this control on the server-side
(through ASP.NET's code-behind), and then access it similarly
in JavaScript scripts on the client-side.

Make sure that, after you add the control to the page, you
right-click it an choose "Run as server control" so that is posts
back to the server properly.
 
thanks Bucky - I've got half of it. I placed the hidden control in the parent form and can access and set its value from the code behind of the page if need be.

The issue now is accessing it from the user control. I've tried to following code which isn't working (I'm get a null reference object for the var "theform".

function reloadcontrol(value)
{
var theform = this.parent;
theform.thecontent.value = value;
theform.submit();
}

</script>

I call this on an onClick function and pass a value (i.e. onclick="reloadcontrol('avalue')".

Thus, the problem seems to be accessing the hiddne field of the parent window - to which I tried window.parent, parent, this.parent and document.parentWindow - all with the same error message.

any ideas?

inzo
 
thanks Bucky - I've got half of it. I placed the hidden control in the parent form and can access and set its value from the code behind of the page if need be.

The issue now is accessing it from the user control. I've tried to following code which isn't working (I'm get a null reference object for the var "theform".

function reloadcontrol(value)
{
var theform = this.parent;
theform.thecontent.value = value;
theform.submit();
}

</script>

I call this on an onClick function and pass a value (i.e. onclick="reloadcontrol('avalue')".

Thus, the problem seems to be accessing the hiddne field of the parent window - to which I tried window.parent, parent, this.parent and document.parentWindow - all with the same error message.

any ideas?

inzo
 
Got It!

Thanks again for your help:

I found the solution by using hidden fields. For those of you following the thread, this is how I used an HTML image object (to be able to use a rollover) in a user control as a menu that changed the state of another user control by using a hidden field - confused... read on.

function reloadcontrol(value)
{
var theform = parent.document.formidname
theform.thehiddenfield.value = value;
theform.submit();
}


The javascript code above was placed in the menu user control. Replace the formidname variable with the id of the form in your base page. This gets called using the onclick command of an image. (i.e. onclick="reloadcontrol('value-to-pass')")

In the base form, I'm collecting the hidden value in the code behind page using the following code:

Protected WithEvents myusercontrol As UserControl
Public WithEvents thechiddenfield As HtmlInputHidden

declare this with your other variables.

In the PreRender method I used the following code to set the visibility of my user control.

Private Sub Page_PreRender(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.PreRender

Dim activecontrolname As String = thecontent.Value

myusercontrol = Me.FindControl(activecontrolname)
myusercontrol.Visible = True

End Sub

It took a couple of days and a siz pack of Foster to get this - I hope it helps for those searching. If you know an easier way, please post.

thanx again,

inzo
 
Back
Top