Setting Width or Font size from code behind / dynamically

When we are declaring any Label/button from code behind then we can also set the font size/width for the same

For Width its considered in Unit & Font Size is considered in FontUnit

For Label

We can’t use directly –

lblParameterName.Font.Size = “10px”;                                                                                                                                                              you will get error – Cannot implicitly convert type ‘string’ to ‘System.Web.UI.WebControls.FontUnit’

We can use the following way –  lblParameterName.Font.Size = new FontUnit(10); Here we are setting the fontsize to 10;

For Button –

We can’t use directly –

btnParameterName.width = “10px”;     you will get error – Cannot implicitly convert type ‘string’ to ‘System.Web.UI.WebControls.Unit’

We can use the following way –  btnParameterName.width = Unit.Pixel(10); Here we are setting the widthto 10px;

Detail Ref – MSDN Unit

2 thoughts on “Setting Width or Font size from code behind / dynamically”

  1. Yes this is exactly what I needed. Thank you so much. I’m a newbie with asp.net so these simple things get me.

  2. cool logic is here bro just try it

    button2.Font= new Font(“FONT NAME”,size,FONT STYLE);

    that code help you not just set the font size but also change font style and font also.

    chirag tyagi (india)

Leave a comment