0

I have a fairly simple page which queries a web service to pull data from a SQL server, returning a generic list of varying size. What I'm trying to do, is adjust the height of the div at runtime, adding a certain number of pixels to an existing CSS key on each iteration of a foreach loop, so that the projected list does not overflow the container div.

The code I am using to put the list on the page is as follows:

int total = 0;
lstUsageServices.Text = string.Empty;
lstUsageRequests.Text = string.Empty;
if (txtAccount.Text != string.Empty)
{
soapClient = new UsageService.ServiceSoapClient();
foreach (UsageClient.UsageService.Usage current in soapClient.GetUsage(txtAccount.Text, startPicker.SelectedDate, endPicker.SelectedDate))
{
lstUsageServices.Text += current.Service + "<br />";
lstUsageRequests.Text += current.Requests.ToString() + "<br />";
total += current.Requests;                        
}
lstUsageServices.Text += "<strong>Total</strong>";
lstUsageRequests.Text += "<strong>" + total.ToString() + "</strong>";

What I would like to do, is add a line inside the foreach to reference a div:

<div class="main" runat="server" id="mainDiv">

and increase the CSS "height:Xpx;" with each iteration.

I hope someone can help, it doesn't seem like this should be a difficult thing to do! Thanks in advance :)

2
  • If you want the div's height to increase as it gets more content, can't you just leave its height as automatic? Commented Oct 24, 2012 at 8:00
  • tried leaving as auto, doesn't seem to be honoured, even when i add a !important Commented Oct 24, 2012 at 9:12

1 Answer 1

1

A simple way can be

mainDiv.Attributes.CssStyle.Add("height", "Xpx");
Sign up to request clarification or add additional context in comments.

4 Comments

The idea is correct; the OP will need to calculate X then assign it after the loop.
that's fine, so I'll just keep a count of the number of rows, and multiply the count by the number of pixels a row occupies. Will it not be an issue that I'll be constantly adding more attributes, will the most recent simply replace the previous one?
no joy on this unfortunateley, added the below to now avail mainDiv.Attributes.CssStyle.Add("height", (420 + (rowCount * 10)).ToString());
ended up instead using an <asp:Literal> control, which DOES force the height of a div if set to auto.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.