Asp.Net - Get Height of HtmlGenericControl

Posted on 2011-02-12
Last Modified: 2012-05-11

Using ASP.Net, how do you get the height of a HtmlGenericControl?

Non-Functional Example:

Dim ContentPaneControl As HtmlGenericControl = CType(Master.FindControl("ContentPane"), HtmlGenericControl)

Dim MyDivHeight As String = ContentPaneControl.Attributes("height").ToString

Thank you,
Question by:Beircheart
  • 3
  • 2
LVL 16

Expert Comment

ID: 34879952
If you change Attributes for Style?

Dim MyDivHeight As String = ContentPaneControl.Style("height").ToString

Author Comment

ID: 34880443
No go I'm afraid

"Object reference not set to an instance of an object"
LVL 16

Expert Comment

ID: 34880465

I think you have a problem with getting your control.

Get the ContentPlaceHolder first

Dim mContentPlaceHolder As ContentPlaceHolder = CType(Master.FindControl("ContentPlaceHolder1"), ContentPlaceHolder)
(where "ContentPlaceHolder1" is the ID of your ContentPlaceHolder.

Then try :

Dim ContentPaneControl As HtmlGenericControl = CType(mContentPlaceHolder .FindControl("ContentPane"), HtmlGenericControl)


Accepted Solution

Beircheart earned 0 total points
ID: 34896024
Hi Burnie,

Unfortunately that didn't work either

However, I have implemented a workaround using Javascript (See attached). This code is embedded on my Master Page

While I am satisfied with this workaround, I would prefer to avoid such a method due to the fact that this function is now being called on every page even though it is only needed on one of them

I will leave this question open until Thursday in case someone can provide a solution rather than a workaround

For the sake of clarity, I will summarise what I'm trying to do:

I have a div in my Master page (Lets call it MasterPageDIV). I have a conent page called MyContentPage.aspx. I would like to get the height of MasterPageDIV using .Net code in the MyContentPage.aspx Page_Load event.

All the best,
<script type="text/javascript">

        try {
            window.onresize = SetImagePosition
        catch (e) {
            // Ignore Error

        function SetImagePosition() {
            try {
                document.getElementById('CalendarStyle').style.marginTop = 0;
                if (document.getElementById('Background-Home')) {
                    document.getElementById('Background-Home').style.marginTop = 1;

                var ContentPaneHeight = document.getElementById('ContentPane').offsetHeight;
                var LinksPaneHeight = document.getElementById('LinksPane').offsetHeight;

                if (LinksPaneHeight < ContentPaneHeight) {
                    document.getElementById('CalendarStyle').style.marginTop = ContentPaneHeight - LinksPaneHeight + 10;
                    if (document.getElementById('Background-Home')) {
                        document.getElementById('Background-Home').style.marginBottom = 1;

                else if (LinksPaneHeight > ContentPaneHeight) {
                    if (document.getElementById('Background-Home')) {
                        document.getElementById('Background-Home').style.marginTop = LinksPaneHeight - ContentPaneHeight;
            catch (e) {
                // Ignore Error

<body onload="SetImagePosition()">

Open in new window


Author Closing Comment

ID: 34986373
This workaround will have to do

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Get id from json Data with NewtonSoft 3 32
WordPress Themes 10 34
Replace &lt; with < 14 54
Footer at the bottom of the page not centered 5 28
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now