Intersoft Support Center

Create WebCombo At Runtime

You can create WebCombo programmatically from server-side.

In this topic, you will learn how to create WebCombo at runtime.

To create WebCombo at runtime

  1. Create a DIV container like following.

    <div runat="server" id="DivContainer">

  2. Switch to View Code. At first, you need to add the following namespace.

    using ISNet.WebUI.WebCombo;

  3. Put the following code in Page_Load.
    C# Copy ImageCopy Code
    protected void Page_Load(object sender, EventArgs e)
    {
       AccessDataSource access;
       access = new AccessDataSource(Server.MapPath("./App_Data/Northwind.mdb"), "Select * from Customers");
       access.ID = "AccessDataSource1";
    
       WebCombo combo = new WebCombo("WebCombo1");
       combo.Width = Unit.Pixel(200);
       combo.Height = Unit.Pixel(20);
       combo.UseDefaultStyle = true;
    
       // Setup Events
       combo.InitializeDataSource += new DataSourceEventHandler(combo_InitializeDataSource);
       combo.InitializeLayout += new LayoutEventHandler(combo_InitializeLayout);
    
       //this.Controls.Add(combo);
       this.DivContainer.Controls.Add(access);
       this.DivContainer.Controls.Add(combo);
    }

  4. Put the following code in InitializeDataSource server-side event to setup the DataSource.
    C# Copy ImageCopy Code
    protected void combo_InitializeDataSource(object sender, DataSourceEventArgs e)
    {
       // Setup DataSource
       WebCombo combo = (WebCombo)sender;
       combo.DataSourceID = "AccessDataSource1";        
    }

  5. Finally, put the following code in InitializeLayout server-side event to setup WebCombo's behavior.
    C# Copy ImageCopy Code
    protected void combo_InitializeLayout(object sender, LayoutEventArgs e)
    {
       // Setup Behavior
       WebCombo combo = (WebCombo)sender;
       combo.DataTextField = "ContactName";
       combo.DataValueField = "CustomerID";
    }

  6. Run the project.
Previous Next