How to bind data to a listbox in a custom server control

Discussion in 'ASP .Net Web Controls' started by Francisco Alvarado, Jul 16, 2003.

  1. I am building a custom server control that will have 1 label, 1 text
    box and 1 listbox. How can I add the functionality to bind data to
    listbox?

    I am not looking for anything fancy. I just want to bind some data
    from the code behind just like you do with a regular listbox control.
    Ex.
    ListBox1.DataSource = daDataSource;
    ListBox1.DataTextField = "Name";
    ListBox1.DataValueField = "Customer_ID";
    ListBox1.DataBind();

    But i want to access the listbox with the right syntax..
    MyCustomControl.MyListBox.DataSource = daDataSource;
    etc.


    Can anyone guide me?
    Here is the code I have until now:

    //myCustomControl.cs

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.ComponentModel.Design;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Text;

    namespace HPIS.ServerControls
    {
    public class DualListBox : WebControl, INamingContainer
    {
    private TextBox _AvailableSearchTextBox;
    private Label _AvailableLabel;
    private ListBox _AvailableListBox;

    #region Overriden properties

    public override ControlCollection Controls
    {
    get
    {
    EnsureChildControls();
    return base.Controls;
    }
    }
    #endregion Overriden properties

    #region Properties delegated to child controls

    [
    Bindable(true),
    Category("Appearance"),
    DefaultValue(""),
    Description("The text for the avaiable label")
    ]
    public string AvailableLabel
    {
    get
    {
    EnsureChildControls();
    return _AvailableLabel.Text;
    }
    set
    {
    EnsureChildControls();
    _AvailableLabel.Text = value;

    }
    }

    [
    Browsable(false),
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
    ]
    public string Available
    {
    get
    {
    EnsureChildControls();
    return _AvailableListBox.SelectedValue;
    }
    set
    {
    EnsureChildControls();
    _AvailableListBox.SelectedIndex =
    _AvailableListBox.Items.IndexOf(_AvailableListBox.Items.FindByValue(value));
    }
    }
    #endregion Properties delegated to child controls

    #region Overriden methods
    protected override void CreateChildControls()
    {
    Controls.Clear();

    _AvailableLabel = new Label();

    _AvailableSearchTextBox = new TextBox();
    _AvailableSearchTextBox.ID = "AvaiableSearchTextBox";
    _AvailableSearchTextBox.Width = 150;

    _AvailableListBox = new ListBox();
    _AvailableListBox.ID = "AvailableListBox";
    _AvailableListBox.Rows = 10;
    _AvailableListBox.Width = 150;
    _AvailableListBox.SelectionMode =
    System.Web.UI.WebControls.ListSelectionMode.Multiple;
    _AvailableListBox.Items.Add(new ListItem("1st", "1"));
    _AvailableListBox.Items.Add(new ListItem("2nd", "2"));
    _AvailableListBox.Items.Add(new ListItem("3rd", "3"));
    _AvailableListBox.Items.Add(new ListItem("4th", "4"));

    this.Controls.Add(_AvailableLabel);
    this.Controls.Add(_AvailableSearchTextBox);
    this.Controls.Add(_AvailableListBox);
    }

    protected override void Render(HtmlTextWriter writer)
    {
    AddAttributesToRender(writer);

    writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"1",
    false);
    writer.RenderBeginTag(HtmlTextWriterTag.Table);

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    writer.AddAttribute(HtmlTextWriterAttribute.Align,"center");
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    _AvailableLabel.RenderControl(writer);
    writer.RenderEndTag(); // Td
    writer.RenderEndTag(); //Tr

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    _AvailableSearchTextBox.RenderControl(writer);
    writer.RenderEndTag(); // Td
    writer.RenderEndTag(); //Tr

    writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    writer.RenderBeginTag(HtmlTextWriterTag.Td);
    _AvailableListBox.RenderControl(writer);
    writer.RenderEndTag(); // Td
    writer.RenderEndTag(); // Tr

    writer.RenderEndTag(); // Table

    }

    #endregion Overriden methods
    }
    }
    Francisco Alvarado, Jul 16, 2003
    #1
    1. Advertising

  2. Hey :)

    I would make a property on MyCustomControl that takes a datasource for
    input. It would then set it's MyListBox DataSource to that input. To set the
    datasource you would then need to type:

    MyCustomControl.ListBoxDataSource = daDataSource;

    "Francisco Alvarado" <> wrote in message
    news:...
    > I am building a custom server control that will have 1 label, 1 text
    > box and 1 listbox. How can I add the functionality to bind data to
    > listbox?
    >
    > I am not looking for anything fancy. I just want to bind some data
    > from the code behind just like you do with a regular listbox control.
    > Ex.
    > ListBox1.DataSource = daDataSource;
    > ListBox1.DataTextField = "Name";
    > ListBox1.DataValueField = "Customer_ID";
    > ListBox1.DataBind();
    >
    > But i want to access the listbox with the right syntax..
    > MyCustomControl.MyListBox.DataSource = daDataSource;
    > etc.
    >
    >
    > Can anyone guide me?
    > Here is the code I have until now:
    >
    > //myCustomControl.cs
    >
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.ComponentModel.Design;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Text;
    >
    > namespace HPIS.ServerControls
    > {
    > public class DualListBox : WebControl, INamingContainer
    > {
    > private TextBox _AvailableSearchTextBox;
    > private Label _AvailableLabel;
    > private ListBox _AvailableListBox;
    >
    > #region Overriden properties
    >
    > public override ControlCollection Controls
    > {
    > get
    > {
    > EnsureChildControls();
    > return base.Controls;
    > }
    > }
    > #endregion Overriden properties
    >
    > #region Properties delegated to child controls
    >
    > [
    > Bindable(true),
    > Category("Appearance"),
    > DefaultValue(""),
    > Description("The text for the avaiable label")
    > ]
    > public string AvailableLabel
    > {
    > get
    > {
    > EnsureChildControls();
    > return _AvailableLabel.Text;
    > }
    > set
    > {
    > EnsureChildControls();
    > _AvailableLabel.Text = value;
    >
    > }
    > }
    >
    > [
    > Browsable(false),
    > DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
    > ]
    > public string Available
    > {
    > get
    > {
    > EnsureChildControls();
    > return _AvailableListBox.SelectedValue;
    > }
    > set
    > {
    > EnsureChildControls();
    > _AvailableListBox.SelectedIndex =
    >

    _AvailableListBox.Items.IndexOf(_AvailableListBox.Items.FindByValue(value));
    > }
    > }
    > #endregion Properties delegated to child controls
    >
    > #region Overriden methods
    > protected override void CreateChildControls()
    > {
    > Controls.Clear();
    >
    > _AvailableLabel = new Label();
    >
    > _AvailableSearchTextBox = new TextBox();
    > _AvailableSearchTextBox.ID = "AvaiableSearchTextBox";
    > _AvailableSearchTextBox.Width = 150;
    >
    > _AvailableListBox = new ListBox();
    > _AvailableListBox.ID = "AvailableListBox";
    > _AvailableListBox.Rows = 10;
    > _AvailableListBox.Width = 150;
    > _AvailableListBox.SelectionMode =
    > System.Web.UI.WebControls.ListSelectionMode.Multiple;
    > _AvailableListBox.Items.Add(new ListItem("1st", "1"));
    > _AvailableListBox.Items.Add(new ListItem("2nd", "2"));
    > _AvailableListBox.Items.Add(new ListItem("3rd", "3"));
    > _AvailableListBox.Items.Add(new ListItem("4th", "4"));
    >
    > this.Controls.Add(_AvailableLabel);
    > this.Controls.Add(_AvailableSearchTextBox);
    > this.Controls.Add(_AvailableListBox);
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > AddAttributesToRender(writer);
    >
    > writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"1",
    > false);
    > writer.RenderBeginTag(HtmlTextWriterTag.Table);
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    > writer.AddAttribute(HtmlTextWriterAttribute.Align,"center");
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    > _AvailableLabel.RenderControl(writer);
    > writer.RenderEndTag(); // Td
    > writer.RenderEndTag(); //Tr
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    > _AvailableSearchTextBox.RenderControl(writer);
    > writer.RenderEndTag(); // Td
    > writer.RenderEndTag(); //Tr
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    > _AvailableListBox.RenderControl(writer);
    > writer.RenderEndTag(); // Td
    > writer.RenderEndTag(); // Tr
    >
    > writer.RenderEndTag(); // Table
    >
    > }
    >
    > #endregion Overriden methods
    > }
    > }
    Per Hornshøj-Schierbeck, Jul 16, 2003
    #2
    1. Advertising

  3. Hi Francisco,

    >>>> MyCustomControl.MyListBox.DataSource

    You could add a public property to your custom control used to wrap the
    child listbox control, ie:
    [C#]
    public ListBox MyListBox{
    get{ return _myListBox;}
    }

    then you could write:
    [C#]
    YourCustomControlID.MyListBox.DataSource = ds;

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup

    and not by private mail.
    "Francisco Alvarado" <> wrote in message
    news:...
    > I am building a custom server control that will have 1 label, 1 text
    > box and 1 listbox. How can I add the functionality to bind data to
    > listbox?
    >
    > I am not looking for anything fancy. I just want to bind some data
    > from the code behind just like you do with a regular listbox control.
    > Ex.
    > ListBox1.DataSource = daDataSource;
    > ListBox1.DataTextField = "Name";
    > ListBox1.DataValueField = "Customer_ID";
    > ListBox1.DataBind();
    >
    > But i want to access the listbox with the right syntax..
    > MyCustomControl.MyListBox.DataSource = daDataSource;
    > etc.
    >
    >
    > Can anyone guide me?
    > Here is the code I have until now:
    >
    > //myCustomControl.cs
    >
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.ComponentModel.Design;
    > using System.Web.UI;
    > using System.Web.UI.WebControls;
    > using System.Text;
    >
    > namespace HPIS.ServerControls
    > {
    > public class DualListBox : WebControl, INamingContainer
    > {
    > private TextBox _AvailableSearchTextBox;
    > private Label _AvailableLabel;
    > private ListBox _AvailableListBox;
    >
    > #region Overriden properties
    >
    > public override ControlCollection Controls
    > {
    > get
    > {
    > EnsureChildControls();
    > return base.Controls;
    > }
    > }
    > #endregion Overriden properties
    >
    > #region Properties delegated to child controls
    >
    > [
    > Bindable(true),
    > Category("Appearance"),
    > DefaultValue(""),
    > Description("The text for the avaiable label")
    > ]
    > public string AvailableLabel
    > {
    > get
    > {
    > EnsureChildControls();
    > return _AvailableLabel.Text;
    > }
    > set
    > {
    > EnsureChildControls();
    > _AvailableLabel.Text = value;
    >
    > }
    > }
    >
    > [
    > Browsable(false),
    > DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
    > ]
    > public string Available
    > {
    > get
    > {
    > EnsureChildControls();
    > return _AvailableListBox.SelectedValue;
    > }
    > set
    > {
    > EnsureChildControls();
    > _AvailableListBox.SelectedIndex =
    >

    _AvailableListBox.Items.IndexOf(_AvailableListBox.Items.FindByValue(value));
    > }
    > }
    > #endregion Properties delegated to child controls
    >
    > #region Overriden methods
    > protected override void CreateChildControls()
    > {
    > Controls.Clear();
    >
    > _AvailableLabel = new Label();
    >
    > _AvailableSearchTextBox = new TextBox();
    > _AvailableSearchTextBox.ID = "AvaiableSearchTextBox";
    > _AvailableSearchTextBox.Width = 150;
    >
    > _AvailableListBox = new ListBox();
    > _AvailableListBox.ID = "AvailableListBox";
    > _AvailableListBox.Rows = 10;
    > _AvailableListBox.Width = 150;
    > _AvailableListBox.SelectionMode =
    > System.Web.UI.WebControls.ListSelectionMode.Multiple;
    > _AvailableListBox.Items.Add(new ListItem("1st", "1"));
    > _AvailableListBox.Items.Add(new ListItem("2nd", "2"));
    > _AvailableListBox.Items.Add(new ListItem("3rd", "3"));
    > _AvailableListBox.Items.Add(new ListItem("4th", "4"));
    >
    > this.Controls.Add(_AvailableLabel);
    > this.Controls.Add(_AvailableSearchTextBox);
    > this.Controls.Add(_AvailableListBox);
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > AddAttributesToRender(writer);
    >
    > writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"1",
    > false);
    > writer.RenderBeginTag(HtmlTextWriterTag.Table);
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    > writer.AddAttribute(HtmlTextWriterAttribute.Align,"center");
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    > _AvailableLabel.RenderControl(writer);
    > writer.RenderEndTag(); // Td
    > writer.RenderEndTag(); //Tr
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    > _AvailableSearchTextBox.RenderControl(writer);
    > writer.RenderEndTag(); // Td
    > writer.RenderEndTag(); //Tr
    >
    > writer.RenderBeginTag(HtmlTextWriterTag.Tr);
    > writer.RenderBeginTag(HtmlTextWriterTag.Td);
    > _AvailableListBox.RenderControl(writer);
    > writer.RenderEndTag(); // Td
    > writer.RenderEndTag(); // Tr
    >
    > writer.RenderEndTag(); // Table
    >
    > }
    >
    > #endregion Overriden methods
    > }
    > }
    Victor Garcia Aprea [MVP], Jul 17, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?R3lydXNHTQ==?=

    Using a ListBox in a custom server control

    =?Utf-8?B?R3lydXNHTQ==?=, May 18, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    286
    =?Utf-8?B?R3lydXNHTQ==?=
    May 18, 2004
  2. mr2_93
    Replies:
    1
    Views:
    3,580
    mr2_93
    Oct 2, 2005
  3. Fabio Visin
    Replies:
    3
    Views:
    1,219
    Steven Cheng
    Feb 21, 2008
  4. Matt Sokol
    Replies:
    2
    Views:
    365
    Theo Tillotson
    Aug 7, 2003
  5. Avneesh Kumar

    Facing problems with Dual listbox Custom Server Control

    Avneesh Kumar, Sep 4, 2003, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    239
    Andy Smith
    Oct 1, 2003
Loading...

Share This Page