| |||||||||||||||||||||||||||
Setting Web Server Control Properties ProgrammaticallyASP.NET Web Forms Web Forms Server Controls Programming Web Forms Server Controls You can set Web server control properties programmatically to change the control's appearance and behavior at run time. Properties for Web server controls are strongly typed, so the exact technique you use to set properties depends on what type of property you are setting.
Setting Properties Based on Simple Values or EnumerationsIf an Web server control property's data type is a primitive a String, Boolean, or numeric type then you can set the property value by simply assigning it to the property. Similarly, if the property's values are defined in an enumeration class, you can simply assign the enumeration to the property. To set a property value based on simple values
To set a property value based on an enumeration
Setting Unit PropertiesSome properties, notably measurements, are set in units. Units are implemented as objects ( the Unit class ), which allows you to specify a value and the measurement unit in various ways. The Unit class allows you to specify measurement units using the UnitType enumeration or using a string consisting of a numeric value plus a standard HTML unit abbreviation:
Because units are manipulated in the Unit object, you must assign a reference from the Unit object to your object. To set unit-based properties
[ Visual Basic ] TextBox1.Width = New Unit ( 100 ) ' default is pixels TextBox1.Width = New Unit ( 100, UnitType.Pixel ) TextBox1.Width = New Unit ( "100px" ) TextBox1.Width = New Unit ( "2cm" ) ' Centimeters TextBox1.Width = New Unit ( 10, UnitType.Percentage ) TextBox1.Width = New Unit ( "10%" ) Setting Color PropertiesTo set a property to a color ( such as the BackColor property ), you assign a reference to the Color object. To set properties to a color
[ Visual Basic ] Button1.BackColor=Color.FromARGB ( "Red" ) Button1.BackColor=Color.FromARGB ( 255, 255, 255 ) ' white ( in RGB ) Button1.BackColor=Color.Red Button1.BackColor=Color.MediumSeaGreen ' HTML 4.0 color Setting Control Properties in CollectionsThe properties of some controls are not simple values or objects, but collections. For example, the individual values of a ListBox ASP.NET control are implemented as a collection of ListItem objects. To set a complex control property
[ Visual Basic ] Dim li As ListItem li=New ListItem li.Text = "Item 1" ListBox1.Items.Add ( li ) ' Create and add the items at the same time ListBox1.Items.Add ( New ListItem ( "Apples" ) ) ListBox1.Items.Add New ListItem ( "Oranges" ) ListBox1.Items.Add New ListItem ( "Lemons" ) See AlsoWeb Forms Server Controls and CSS Styles Setting HTML Server Control Properties Programmatically Setting Web Forms Server Control Properties |
| ||||||||||||||||||||||||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme