calculating control width

J

Jeff

Hey

asp.net 2.0

I'm trying to calculate the width of the HyperLink control (see my code
below). But all I get is the value 0.0

PlaceHolder placeholder =
(PlaceHolder)myProfile.FindControl("PlaceHolder1");
double Rwidth = 0;

hyperlink = new HyperLink();
hyperlink.Text = "helloworld1";
hyperlink.Font.Size = 16;
hyperlink.NavigateUrl = "www.vg.no";
hyperlink.Target = "_blank";
placeholder.Controls.Add(hyperlink);
Rwidth = hyperlink.Width.Value;

after this line "Rwidth = hyperlink.Width.Value;" the Rwidth had the value
0.0. But this isn't the real width of the HyperLink control.

The HyperLink control is added dynamically in the Page_Load event of the
webpage

What should I do to get the real width of the HyperLink column?

Jeff
 
M

Mark Rae

The HyperLink control is added dynamically in the Page_Load event of the
webpage

I'm not 100% certain, but I think that may be your problem. Generally
speaking, dynamically creating controls is only truly reliable when they're
created in Page_Init - are you able to try that?
What should I do to get the real width of the HyperLink column?

Because the control is created dynamically, it's entirely possible that its
graphical properties aren't fully known until the "very last minute", if you
see what I mean i.e. when ASP.NET has actually constructed the page just
prior to sending it down to the client. Therefore, try interrogating the
width of the control in Page_PreRender instead...
 
J

Jeff

Hey

Thanks for replying to my post!

The reason I want to calculate the width of these dynamically added controls
are that these controls will be added in a PlaceHolder (the PlaceHolder is
in a table cell). This PlaceHolder can only be 595px wide. So I want to
caluculate the width of the controls I've added to the PlaceHolder and if
the sum of widths is about 595px then I must start adding controls on a new
line in PlaceHolder (placeholder.Controls.Add(new LiteralControl("<br>"));)
I'm not sure this can be done in Page_PreRender event....
 
J

Jeff

I tryed to detect the HyperLink width in the Page_PreRender widthout any
luck:

protected override void OnPreRender(EventArgs e)
{
PlaceHolder placeholder =
(PlaceHolder)myProfile.FindControl("PlaceHolder1");
double myWidth;
HyperLink hyperlink = (HyperLink)placeholder.FindControl("hyperlink1");
myWidth = hyperlink.Width.Value;
Unit unit = hyperlink.Width;
}

"myWidth = hyperlink.Width.Value;" set myWidth = 0.0...
and
unit.Value is also 0.0
 
M

Mark Rae

I tryed to detect the HyperLink width in the Page_PreRender widthout any
luck:

protected override void OnPreRender(EventArgs e)
{

Not quite sure what you're doing here... what exactly is OnPreRender in this
case...?

I'm talking about the PreRender method of the Page object:

private void Page_PreRender(object sender, System.EventArgs e)
{
//
}
 
J

Jeff

hmm let's see I thought the OnPreRender
(http://msdn2.microsoft.com/en-us/library/system.web.ui.control.onprerender(d=ide).aspx)
method was the same as the PreRender event
(http://msdn2.microsoft.com/en-us/library/system.web.ui.control.prerender(d=ide).aspx).

Anyway I tryed the Page_PreRender method:
private void Page_PreRender(object sender, System.EventArgs e)
{
PlaceHolder placeholder =
(PlaceHolder)myProfile.FindControl("PlaceHolder1");
double myWidth;
HyperLink hyperlink =
(HyperLink)placeholder.FindControl("hyperlink1");
myWidth= hyperlink.Width.Value;
Unit unit = hyperlink.Width;
}

Still myWidth is 0.0

maybe another approach to solve this is to specify a max width on the
PlaceHolder, or the table cell (the table cell in which the PlaceHolder is
placed with in). And let hyperlink controls float within that PlaceHolder.
If one line in the PlaceHolder is full the next HyperLink control will float
to a new line below.... I want these HyperLink controls to stick tight
together. However I'm not sure if this is possible?
 
G

Guest

I wouldn't worry about it too much, man. Ultimately, you really can not know
because the rendering, therefore the sizing, takes place on the client side.

from what I'm seeing, though, you can get the same result by setting the CSS
"display" attribute to "inline-block" for each of these controls. That will
keep them from breaking in the middle and if the following element has the
potential for breaking, the entire control (span/hyperlink, etc) will be
automatically pushed to the next line if i runs up against the right side of
the bounding box
 

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,774
Messages
2,569,599
Members
45,178
Latest member
Crypto Tax Software
Top