7.19. Programming Web Server Controls

All the control properties we've set interactively in the Properties window while using the Web Forms designer, such as ForeColor or input Mode for a TextBox, can also be set within our program. For example, to dynamically reset the Mode, Width, and ForeColor of a TextBox, we would write the following,

TextBox1.Mode = TextBoxMode.SingleLine;
TextBox1.Width = new Unit( 200, UnitType.Pixel );
TextBox1.ForeColor = Color.Red;

where TextBoxMode and UnitType are enumeration types of alternative property values, and Red is a static property of the Color class.

Color provides many named colors, such as Black, Blue, BlanchedAlmond, and BurlyWood.

In addition to changing the properties of an existing control, we can create controls during the execution of our program. Figure 7.8 pictures an application in which the user selects controls to be added to the page dynamically.

Figure 7.8. Adding Controls to a Page at Runtime


There must be a container into which to add a control created from within the program. The Panel control can serve as a container. We add the new control to the Controls collection property of the Panel class. For example, here is how we add the Calendar control:

case controlTypes.calendar:
{
   Calendar newCal = new Calendar();
   newCal.SelectionMode = CalendarSelectionMode.DayWeekMonth;
   newCal.ID = controls[ ix ] + id++.ToString();

   Panel1.Controls.Add( newCal );
   Panel1.Controls.Add( new LiteralControl( "<br>" ));

   break;
}

LiteralControl allows us to create static text. In this example we are inserting a line break to keep the controls from butting up against one another.

controlTypes is an enumeration identifying the control types the user can ask to have created. controls is an array of strings identifying those controls; it serves as the CheckBoxList data source. id is a static integer that allows us to set a unique ID for each control. The event handler for the check box list looks like this:

private void
CheckBoxList1_SelectedIndexChanged(object s, EventArgs e)
{
      // check if no item is selected ...
      if ( CheckBoxList1.SelectedIndex == -1 )
           return;

      for( int ix = CheckBoxList1.SelectedIndex;
               ix < CheckBoxList1.Items.Count; ++ix  )
      {
           if ( CheckBoxList1.Items[ ix ].Selected )
                switch( (controlTypes)ix )
                {
                    case controlTypes.button:
                         { ... }

                    case controlTypes.calendar:
                         { ... }

                    // and so on ...
      }
}

Dynamically added controls are not automatically saved as part of the view state of the page. Rather we must manage them ourselves or lose them after a round-trip. (For example, make a selection on the Calendar control within the Panel. The controls do not reappear following the post back.)

Typically, then, dynamic controls are created within the Page_Load() event handler in order to be restored with each round-trip. If program data within the controls needs to be restored as well, we'll have to manage that within the Session object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.141.115