DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Accessing DIV style properties from Code Behind page.

  1. #1
    Join Date
    Apr 2008

    Problem accessing DIV style properties from ASCX Code Behind page.

    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') = ""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.
    Last edited by albertlse; 04-23-2008 at 05:26 AM.

  2. #2
    Join Date
    Apr 2008
    Try using ASP:Panel rather than a DIV; you can then access it in the Code Behind.

  3. #3
    Join Date
    Oct 2009
    You need to set the runat="server" attribute of the control in order to access it from the code behind so:
    <div id="divElement0"></div>
    <div id="divElement0" runat="server"></div>

Similar Threads

  1. Replies: 2
    Last Post: 07-12-2007, 11:41 AM
  2. Replies: 0
    Last Post: 05-11-2007, 06:16 PM
  3. Replies: 1
    Last Post: 09-30-2005, 09:14 AM
  4. Replies: 0
    Last Post: 01-19-2001, 04:16 AM
  5. Printing an html page from code
    By Craig in forum Web
    Replies: 2
    Last Post: 08-07-2000, 02:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center