Click to See Complete Forum and Search --> : Problem accessing DIV style properties from ASCX Code Behind page.


albertlse
04-25-2008, 12:07 AM
I am currently working on an ASCX page.

On the ASCX page, I have a DIV that looks like this:

<div id="divElement0"></div>

On the ASCX code behind page, I set the DIV style properties on Page_Load():

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim strScript As New StringBuilder

If Not Page.IsPostBack Then

strScript.Append("document.getElementById('divElement0').innerText = ""xxx"";")
strScript.Append("document.getElementById('divElement0').style.left = ""354px"";")
strScript.Append("document.getElementById('divElement0').style.top = ""194px"";")

End If

Page.ClientScript.RegisterStartupScript(Me.GetType(), "test", strScript.ToString, True)

End Sub
The page loaded successfully, with the DIV appeared as set. I can also change the position of the DIV by dragging it around.

Now, I am having problem when I try to retrieve the DIV style properties value via a button click.

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Page.ClientScript.RegisterStartupScript(Me.GetType(), "test2", "alert(document.getElementById('divElement0').style.left);", True)
End Sub

I expect it will show the DIV's left position as 354px or whatever it is depending on where I dragged it to. However it just show nothing in the message box.

How can I fixed this? Please help. Thank you.

chazzy
04-25-2008, 09:59 AM
Is it alerting anything, or nothing at all?

dajunkcollector
04-25-2008, 02:57 PM
You may have to convert the property to a string before it will show up in your div.