views:

439

answers:

2

I need to get the actual number of pixels that an IHTMLElements margin is taking up, in my c# code. I've looked in it's style and currentStyle members, but the relevant parts are either null or set to "auto".

Searching the internet has shown up the getCurrentStyle function in javascript, but I need these numbers in my c# code.

Obviously I'm a bit new to js and html and everything...

So, is there any way to get at the actual calculated number of pixels being used for an elements margin (or any of it's other measurements for that matter) in c# code?

I forgot to add: This will only be getting used in Internet Explorer for the foreseeable future

+1  A: 

Does it have to be in the C# code? It may be easier to do this at the client through script - for example, with jQuery some combination of width(), innerWidth() and outerWidth() should return the margin.

Marc Gravell
I need to be able to access the numbers in c# I think. I'm working with a lot of existing code that I can't rearchitecture or anything like that. Is it possible to generate the numbers I need in script and then access that in c#?
I'm a complete novice too, so beware what I say: but one way to access JS results from C# would be for the JS to write its results into the DOM (e.g. as the value of a hidden text node in the DOM).
ChrisW
A: 

Javascript Section

var elementToCalc = document.getElementById("your element");

document.getElementById('txtHidMarginTop').value = parseInt ( elementToCalc.currentStyle.marginTop );
document.getElementById('txtHidMarginLeft').value = parseInt ( elementToCalc.currentStyle.marginLeft;
document.getElementById('txtHidMarginRight').value = parseInt ( elementToCalc.currentStyle.marginRight;
document.getElementById('txtHidMarginBottom').value = parseInt ( elementToCalc.currentStyle.marginBottom );

HTML Section

<input type='hidden' id='txtHidMarginTop' runat='server' />
<input type='hidden' id='txtHidMarginLeft' runat='server' />
<input type='hidden' id='txtHidMarginBottom' runat='server' />
<input type='hidden' id='txtHidMarginRight' runat='server' />

Attach these variables to input type='hidden' elements with runat='server' and then you can access these variable in C# using the DOM elements Value property.

C# Secition

 string marginTop = int.Parse ( txtHidMarginTop.Value );
    string marginLeft = int.Parse ( txtHidMarginLeft.Value );
    string marginRight = int.Parse ( txtHidMarginRight.Value );
    string marginBottom = int.Parse ( txtHidMarginBottom.Value );
rahul