如何在运行时从代码隐藏中更改CSS键的值?

时间:2022-11-20 21:20:00

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.

我有一个相当简单的页面,它查询Web服务从SQL服务器提取数据,返回不同大小的通用列表。我正在尝试做的是,在运行时调整div的高度,在foreach循环的每次迭代中向现有的CSS键添加一定数量的像素,以便投影列表不会溢出容器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:

我想做的是在foreach中添加一行来引用div:

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

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

并增加CSS“height:Xpx;”每次迭代。

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

我希望有人可以提供帮助,这似乎不应该是一件困难的事情!提前致谢 :)

1 个解决方案

#1


2  

A simple way can be

一个简单的方法可以

mainDiv.Attributes.CssStyle.Add("height", "Xpx");

#1


2  

A simple way can be

一个简单的方法可以

mainDiv.Attributes.CssStyle.Add("height", "Xpx");