accessing runat=server div in javascript

B

baldrick

Hi,

I have a div on my page as below and want to write the content of the
div to a cookie so information can be passed between two differnet
pages. With the runat=server I find that the div is not visible to the
rest of page as I get the cant 'find div message'.

The application works fine without the runat=server, but I need to be
able to store the contents of the div in a databse for later recall,
hence need the runat=server.

I also need the div to be accessible by the second page as it directly
reads the content using javascript...

parent.document.getElementById("coordinates").innerHTML = html ;


Does anyone have any simple solution or workaround as to how I can
keep the div accessible to javascript but also be able to keep the
runat=server bit?

Regards,
Phil

Code snippets....

<div id="coordinates" runat=server></div>

Me.MasterPageForm.Attributes.Add("onsubmit", "javascript: return
Polygon_Client();")



<script language="javascript" type="text/javascript">
// this writes coordinates to a cookie
function Polygon_Client(){

var element = document.getElementById("coordinates");
if (element) {
document.cookie = "coordinates=" +
document.getElementById("coordinates").innerHTML;
}
else
{
alert('javascript cant find div coordinates');
}
return true;
}
</script>
 
M

Mark Rae [MVP]

I have a div on my page as below and want to write the content of the
div to a cookie so information can be passed between two differnet
pages.

Does anyone have any simple solution or workaround

Yep - don't use cookies to persist state between pages - use Session
instead. That's what it's for...
 
S

Scott Roberts

I have a div on my page as below and want to write the content of the
div to a cookie so information can be passed between two differnet
pages. With the runat=server I find that the div is not visible to the
rest of page as I get the cant 'find div message'.

The application works fine without the runat=server, but I need to be
able to store the contents of the div in a databse for later recall,
hence need the runat=server.

When you add "runat=server" the control becomes a server control and is
subject to the naming convention of server controls. If you look at the html
source of your page, you'll notice that the control is not named
"coordinates" anymore.
I also need the div to be accessible by the second page as it directly
reads the content using javascript...

parent.document.getElementById("coordinates").innerHTML = html ;


Does anyone have any simple solution or workaround as to how I can
keep the div accessible to javascript but also be able to keep the
runat=server bit?

Try this:

parent.document.getElementById("<%= coordinates.ClientID %>").innerHTML =
html ;
 
S

Scott Roberts

Mark Rae said:
Persisting data between visits to the same site...

Do you consider a postback that occurs 8 hours after the initial page-load
to be another "visit"?

I guess I shouldn't beat around the bush. There are situations where using
cookies is preferable (even necessary) for persisting data between pages
(long delays between postbacks, redirects to another virtual directory,
etc.). Whether the OPs situation is one of those is unclear, as he didn't
provide enough information.
 
M

Mark Rae [MVP]

Do you consider a postback that occurs 8 hours after the initial page-load
to be another "visit"?


If you need to persist data for longer than the duration of session, then
you need to use a cookie...
 
B

baldrick

OK forget the cookie thing. The real issue is this

I have a div in an asp.net page as below

<div id="coordinates" ></div>

Within this page I have an inline frame that contains another page
that is NOT an asp.net page. It is just a page full of javascript.

I can do things on this second page that that that change the content
of the div on the ASP page quite easily using the javascript...

parent.document.getElementById("coordinates").innerHTML = something ;

This all works fine until I add the runat=server to the div

<div id="coordinates" runat=server></div>

I need to add this as the content of the div now also needs to be able
to be filled from a database using the asp.net page. The problem now
is that by adding the runat=server, my second page in the inline frame
cannot now change the parent page as the div is no longer recognised.

I suspect there is a simple solution or workaround. I would appreciate
any useful suggestions.

Thanks in advance

Phil
 
S

Scott Roberts

baldrick said:
OK forget the cookie thing. The real issue is this

I have a div in an asp.net page as below

<div id="coordinates" ></div>

Within this page I have an inline frame that contains another page
that is NOT an asp.net page. It is just a page full of javascript.

I can do things on this second page that that that change the content
of the div on the ASP page quite easily using the javascript...

parent.document.getElementById("coordinates").innerHTML = something ;

This all works fine until I add the runat=server to the div

<div id="coordinates" runat=server></div>

I need to add this as the content of the div now also needs to be able
to be filled from a database using the asp.net page. The problem now
is that by adding the runat=server, my second page in the inline frame
cannot now change the parent page as the div is no longer recognised.

I suspect there is a simple solution or workaround. I would appreciate
any useful suggestions.

There is. Did you see my other post?

Try this:

parent.document.getElementById("<%= coordinates.ClientId %>").innerHTML =
something ;


Oh, never mind, your inner page is not aspx. The problem is, when you add
"runat=server" then a different "name" attribute is assigned to the div when
the html is rendered. I'm not sure that there is a way around that. You
might try something along the lines of adding a non-server div inside a
server div, or vice-versa.
 
B

baldrick

There is. Did you see my other post?

Try this:

parent.document.getElementById("<%= coordinates.ClientId %>").innerHTML =
something  ;

Oh, never mind, your inner page is not aspx. The problem is, when you add
"runat=server" then a different "name" attribute is assigned to thedivwhen
the html is rendered. I'm not sure that there is a way around that. You
might try something along the lines of adding a non-serverdivinside a
serverdiv, or vice-versa.- Hide quoted text -

- Show quoted text -

Yes - just trying that now!
 
N

nick chan

most probably, asp.net changes the id="coordinates" to something else

u can do something like this
<span id="coordinates" style="display:none"></span>
<div id="coordinates_div"></div>

javascript :

var o = parent.document.getElementById('coordinates')
while(o.tagName!='DIV')
o=o.nextSibling

o.innerHTML = 'something'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top