Mana
Mana

Reputation: 1929

get height of div from masterpage

How can I get the height of a div which is placed inside a masterpage?

I'm able to set the height as follow:

((HtmlControl)this.Master.FindControl("sidebar")).Style.Add("height", "600px");

Now I'm trying to get the height from my div called mainPage, how can I do this?

Upvotes: 2

Views: 6367

Answers (2)

UNeverNo
UNeverNo

Reputation: 583

MasterPage.Master:

<div id="_test" style="height: 100px" runat="server"></div>

MasterPage.Master.cs:

public HtmlGenericControl test
{
    get { return this._test; }
}

any other cs-file:

string width = ((MasterPage)this.Master).test.style["height"];

Edit: If you want to get any style information created dynamically you will need to store this information into a hidden field, because you won't be able to retrieve the data by calling style["x"] in the postback.

Edit2: Adjusted masterpage-name.

Upvotes: 1

Yannick Blondeau
Yannick Blondeau

Reputation: 9621

You will have to get the div's height on the client side as it may be rendered differently for each client...

Using jQuery, you can do

$("#sidebar").height();

but for this you will have to wait that the document is ready. So you can put this line in the ready function of the document:

$(document).ready(function(){});

EDIT

Another solution is adding runat="server" to your div

<div ID="sidebar" runat="server">...</div>

Then from your code you can do something like

HtmlGenericControl sb = this.form1.FindControl("sidebar") as HtmlGenericControl;
int sidebarHeight = sb.Height;

Upvotes: 1

Related Questions