How to evaluate the dimensions of a <div>

J

Jeff

Hello, I'm fairly new to asp.net programming (I come from a C++ background)
and am still finding some things very confusing. I wondered if anyone could
help me with the following issue.

I managed to add a handler function to my project which uses GDI+ to resize
a largish image down to an arbitray size. Now I'd like to be able to get the
required sizes programatically from the enclosing <div> but I'm struggling
to see how to do that.

For example, I have the following in a test.aspx page.

<div id="Frame" style="width:200px; height:200px;">

<img src="Resize.ashx?Image=Images/BigImage.jpg&Width=<% ???? %>&Height=<%
???? %>" >

</div>

This works fine if I just substitute = "200" for ???? but is there anything
I can put inside the <% %> which will evaluate to the actual width and
height of any enclosing div?

Many Thanks for any help
Jeff
 
J

Jeff

Sorry to reply to my own post....

I figured that it probably isn't possible to get at the run time size of the
image from within the page itself, so I added some public variable which I
evaluate at page load. Now I have something like this :-

<div>
Image size = <%=Width %> x <%=Height %>
<img id="myImage" runat="server"
src="Resize.ashx?Image=Images/BigImage.jpg&Width=<%=Width %>" style="width:
400px; height: 400px" />
<\div>

Stepping through the code I see that the values for Width and Height are
correctly evaluated in Page_Load and when I run the page the correct values
are substituted into the text "Image size= 400 x 400".

But unfortunately in my handler, context.Request["Width"] evaluates to
<%=Width%> instead of 400.

So where am I going wrong?

Thanks for any help

Jeff
 
B

bruce barker

<%= %> can not be used to set properties of controls with runat=server,
only binding expression which require you also do a bind. just take the
runat=server off or set the properties in the codebehind.

by default a div is the size of its content, so there is no need to set
it. you can get its size at runtime with javascript.

-- bruce (sqlwork.com)
 
J

Jeff

Patrice said:
This is just literal values. If I remember you'll have to do soemthign
such :

src='<%="Resize.ashx?Image=...&Width"+Width.ToString%>' to get the effect
you want. so that ASP.NET recogniez that you introduce an expression into
this property.

I don't guarantee this syntax as my personal preference is to avoid
expression in ASPX markup and to go in this my code behind file :

MyImage.src=MyExpression...

Thanks Patrice

I've tried every variation on this sort of thing

I can add something like

<%="Resize.ashx?Image=Images/BigImage.jpg&Width="+Width%>

outside of the object tags and it evaluates just as I want on the page.

But as soon as I put this sort of thing inside the < > tags I get a message
like
Build (web): Server tags cannot contain <% ... %> constructs.
Oh well - maybe I'm not meant to do this!

Jeff
 
J

Jeff

bruce barker said:
<%= %> can not be used to set properties of controls with runat=server,
only binding expression which require you also do a bind. just take the
runat=server off or set the properties in the codebehind.

by default a div is the size of its content, so there is no need to set
it. you can get its size at runtime with javascript.

Thanks a lot.

Yes, removing the server tag means that the <%=%> are correctly evaluated :-

<img id="myImage" src="Resize.ashx?Image=Images/BigImage.jpg&Width=<%=Width
%>&Height=<%=Height %>" style="width: 319px; height: 313px" />

The problem now is that the image object is not available to the Page_Load
method so I can't set the variables Width and Height when the page loads.

I'm really just playing around at the moment in order to learn how this
stuff works. I thought it would be nice to be able to use the Design Space
to set the size of the img (319x313 in the above example) and then have the
actual image resized to this size using a good quality resampler - rather
than being sent at a larger size and resized by th browser. If I send the id
of the img into the handler as part of the query string, is it easy to
convert this to an object and get it's attributes - in the way I'm presently
doing in Page_Load - eg myImage.Attributes["style"]

Thanks again
Jeff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top