# Monday, 23 October 2006
« Inline Search for Internet Explorer | Main | Paper, glue, origami, fun »

Setting the Width property of a control in ASP.NET is mostly done in the properties pane or by declaratively setting it in the markup. However sometimes you want to set it in code. This can be done by using the Unit structure.

I crafted some small example that you can run to play around with the different UnitType enumerations. Possible choises are:

Cm Measurement is in centimeters. 
Em Measurement is relative to the height of the parent element's font. 
Ex Measurement is relative to the height of the lowercase letter x of the parent element's font. 
Inch Measurement is in inches. 
Mm Measurement is in millimeters. 
Percentage Measurement is a percentage relative to the parent element. 
Pica Measurement is in picas. A pica represents 12 points. 
Pixel Measurement is in pixels. 
Point Measurement is in points. A point represents 1/72 of an inch. 

The sample provided here loops over the possible unit types and fills up the dropdownlist. The second textbox is used to fill in an amount to set the Width of the first textbox control to. If you don't fill in an amount it automatically defaults to 30.

    1 <%@ Page Language="C#" %>

    2 

    3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    4 

    5 <script runat="server">

    6 

    7     protected void Page_Load(object sender, EventArgs e)

    8     {

    9         if (!Page.IsPostBack)

   10         {

   11             // Set the original width to 300 pixels

   12             TextBox1.Width = new Unit(300);

   13 

   14             // Loop over the possible unit types

   15             foreach (string s in Enum.GetNames(typeof(UnitType)))

   16                 DropDownListSelectUnitType.Items.Add(Enum.Format(typeof(UnitType), Enum.Parse(typeof(UnitType), s), "G"));

   17         }

   18     }

   19 

   20     protected void Button1_Click(object sender, EventArgs e)

   21     {

   22         // Obtain the chosen width, if it's not filled it default to 30

   23         int width = !String.IsNullOrEmpty(TextBoxSetWidth.Text) ? Convert.ToInt32(TextBoxSetWidth.Text) : 30;

   24 

   25         // Obtain the chosen unit type

   26         UnitType type = (UnitType)Enum.Parse(typeof(UnitType), DropDownListSelectUnitType.SelectedItem.Text, true);

   27 

   28         // Use the Unit structure to set the width of the textbox

   29         TextBox1.Width = new Unit(width, type);

   30     }

   31 </script>

   32 

   33 <html xmlns="http://www.w3.org/1999/xhtml" >

   34 <head runat="server">

   35     <title>Untitled Page</title>

   36 </head>

   37 <body>

   38     <form id="form1" runat="server">

   39     <div>

   40         <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />

   41         <br />

   42         <br />

   43         Width: <asp:TextBox ID="TextBoxSetWidth" runat="server" Width="40px"></asp:TextBox><br />

   44         Type: <asp:DropDownList ID="DropDownListSelectUnitType" runat="server">

   45         </asp:DropDownList><br />

   46         <asp:Button ID="Button1" runat="server" Text="Set Textboxes width" OnClick="Button1_Click" />

   47     </div>

   48     </form>

   49 </body>

   50 </html>

Grz, Kris.

Monday, 23 October 2006 11:05:53 (GMT Daylight Time, UTC+01:00)  #    Disclaimer  |  Comments [0]  |