Reputation: 329
I am new to web programming and need some help. I am using this CSS code to control look of a label dynamically. how can I change the value of the attribute "width"from code behind dynamically?Also I need to flash the label text. Please find my code below
**strong text**
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css">
.bar {
position: relative;
width: 250px;
height: 25px;
text-align: center;
line-height: 25px;
background: #003458;
background: linear-gradient(to bottom, #005478 0%,#003747 100%);
color: white;
}
.bar:before {
position: absolute;
display: block;
top: 0;
left: 0;
bottom: 0;
width: 40%;**** this value needs to be changed from code behind.
content: '';
background: rgba(0, 255, 0, 0.1);
}
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="Label18" runat= "server" CssClass="bar">20%</asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
Solutions would be appreciated.
Thank you in advance!
Upvotes: 2
Views: 2723
Reputation: 15797
You could set that width
to a variable that you control:
.bar:before {
position: absolute;
display: block;
top: 0;
left: 0;
bottom: 0;
width: <%= BarWidth %>;
content: '';
background: rgba(0, 255, 0, 0.1);
}
Then in your code behind, set it:
private string barWidth = "40%";
public string BarWidth
{
get { return barWidth; }
set { barWidth = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
barWidth="70%";
}
UPDATE: following your comment... here is a different approach. I renamed .bar:before
to .barStatus
and removed the width
line from the CSS. You won't need the HiddenField
like I have in the example... this is just an example so you can watch it work, and then fix it to be what you need for your code.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="_bar" class="bar"><span id="_barStatus" style="width:0%;" class="barStatus" runat="server"></span></div>
<asp:Timer runat="server" id="Timer1" Interval="100" OnTick="UpdateBar"></asp:Timer>
<asp:HiddenField ID="_barState" Value="10" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
And the OnTick function:
protected void UpdateBar(object sender, EventArgs e)
{
int barStatus = Convert.ToInt32(_barState.Value);
if (barStatus < 100)
{
barStatus += 2;
_barState.Value = barStatus.ToString();
_barStatus.Attributes["style"] = String.Format("width:{0}%;", barStatus);
_barStatus.InnerText = String.Format("{0}%", barStatus);
}
}
Upvotes: 3
Reputation: 25
For Label Width Label18.Style.Add("width","200px"); Label18.Width = 200; [ this will also work ]
For text display Label18.Text ="Whatever you want to display";
Upvotes: 0