Live Chat Icon For mobile
Live Chat Icon

How to apply Style to a Web Server control programmatically

Platform: ASP.NET| Category: Controls

User Interface

<asp:TextBox id='TextBox1' style='Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 64px' runat='server'>Syncfusion</asp:TextBox>
<asp:Button id='Button1' style='Z-INDEX: 102; LEFT: 48px; POSITION: absolute; TOP: 120px' runat='server' Text='Button'></asp:Button>
<asp:CheckBox id='CheckBox1' style='Z-INDEX: 103; LEFT: 40px; POSITION: absolute; TOP: 200px' runat='server' Text='Syncfusion'></asp:CheckBox>

Create a function

Protected Function createStyleForControl(ByVal _forecolor As Color, ByVal _backcolor As Color, ByVal _fontbold As Boolean) As Style
	Dim s As Style = New Style
	s.ForeColor = _forecolor
	s.BackColor = _backcolor
	s.Font.Bold = _fontbold
	Return s
End Function

In Page_Load

Dim textboxStyle As Style = createStyleForControl(Color.Pink, Color.Yellow, False)
Dim buttonStyle As Style = createStyleForControl(Color.Blue, Color.Teal, True)
Dim checkboxStyle As Style = createStyleForControl(Color.AliceBlue, Color.PowderBlue, False)


Create a function

protected	Style createStyleForControl(Color _forecolor  ,Color _backcolor  ,bool _fontbold )
	Style s = new Style();
	s.ForeColor = _forecolor;
	s.BackColor =_backcolor;
	s.Font.Bold = _fontbold;
	return s;

In Page_Load

Style textboxStyle=createStyleForControl (Color.Pink, Color.Yellow, false  );
Style buttonStyle=createStyleForControl (Color.Blue, Color.Teal, true  );
Style checkboxStyle =createStyleForControl (Color.AliceBlue, Color.PowderBlue, false  );

TextBox1.ApplyStyle(textboxStyle );
Button1.ApplyStyle (buttonStyle );
CheckBox1.ApplyStyle (checkboxStyle );

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.