Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

he who forgets will be destined to remember... (E.Vedder)
  • *Experts*
Posted

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.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Posted

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

he who forgets will be destined to remember... (E.Vedder)
Posted

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

he who forgets will be destined to remember... (E.Vedder)
Posted

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

he who forgets will be destined to remember... (E.Vedder)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...