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: om...
> 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(DesignerSerializat ionVisibility.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.Multip le;
> _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.Cellpa dding,"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
> }
> }