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

F

Francisco Alvarado

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
}
}
 
P

Per Hornshøj-Schierbeck

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 said:
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
}
}
 
V

Victor Garcia Aprea [MVP]

Hi Francisco,
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 said:
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
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top