Example Summary

Server side scripting with C#, Create a DataTable object w/data. Binding 'Text' and 'Value' fields in the dropdown box to specific columns of the DataTable. Trapping events using AutoPostBack & AutoEventWireup


This example demonstrates creating a DataTable object
in server side script and binding fields in the table to the dropdown box.
The dropdown box uses AutoPostBack="True" attribute in the tag to enable its events
The 'DataSource' property is set to the DataView object (implements ICollection)
The 'DataTextField' is set to a field name in the table ('ColorTextField')
The 'DataValueField' is set to a field name in the table ('ColorValueField')
// Page Language="C#" AutoEventWireup="true" <-- thisi goes up top
void Selection_Change(Object sender, EventArgs e) Add a handler in a script tag and your ready to go
The following is the code in the event hanlder that updates the textbox when the selection is changed
txtTask.Text = ColorList.Items[ColorList.SelectedIndex].Value;

DropDownList Data Binding Example


  
      void Page_Load(Object sender, EventArgs e)
      {
         // Load data for the DropDownList control only once, when the 
         // page is first loaded.
         if(!IsPostBack)
         {
            // Specify the data source and field names for the Text 
            // and Value properties of the items (ListItem objects) 
            // in the DropDownList control.
            ColorList.DataSource = CreateDataSource(); - bind control to DataView object
            ColorList.DataTextField = "ColorTextField";	- this field is bound to the "Text" field
            ColorList.DataValueField = "ColorValueField"; - this field is bound to the "Value" field

            // Bind the data to the control.
            ColorList.DataBind();

            // Set the default selected item, if desired.
            ColorList.SelectedIndex = 0;

         }
      }
      ICollection CreateDataSource() 
      {
      
         // Create a table to store data for the DropDownList control.
         DataTable dt = new DataTable();
         
         // Define the columns of the table.
         dt.Columns.Add(new DataColumn("ColorTextField", typeof(String)));
         dt.Columns.Add(new DataColumn("ColorValueField", typeof(String)));
 
         // Populate the table with sample values.
         dt.Rows.Add(CreateRow("White", "White", dt));
         dt.Rows.Add(CreateRow("Silver", "Silver", dt));
         dt.Rows.Add(CreateRow("Dark Gray", "DarkGray", dt));
         dt.Rows.Add(CreateRow("Khaki", "Khaki", dt));
         dt.Rows.Add(CreateRow("Dark Khaki", "DarkKhaki", dt));
 
         // Create a DataView from the DataTable to act as the data source
         // for the DropDownList control.
         DataView dv = new DataView(dt);
         return dv;

      }

      DataRow CreateRow(String Text, String Value, DataTable dt)
      {

         // Create a DataRow using the DataTable defined in the 
         // CreateDataSource method.
         DataRow dr = dt.NewRow();
 
         // This DataRow contains the ColorTextField and ColorValueField 
         // fields, as defined in the CreateDataSource method. Set the 
         // fields with the appropriate value. Remember that column 0 
         // is defined as ColorTextField, and column 1 is defined as 
         // ColorValueField.
         dr[0] = Text;
         dr[1] = Value;
 
         return dr;

      }
    void Selection_Change(Object sender, EventArgs e)
    {
		txtTask.Text = ColorList.Items[ColorList.SelectedIndex].Value;
	}