Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > How to bind data to a listbox in a custom server control

Reply
Thread Tools

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

 
 
Francisco Alvarado
Guest
Posts: n/a
 
      07-16-2003
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
}
}
 
Reply With Quote
 
 
 
 
Per Hornshøj-Schierbeck
Guest
Posts: n/a
 
      07-16-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
> }
> }



 
Reply With Quote
 
 
 
 
Victor Garcia Aprea [MVP]
Guest
Posts: n/a
 
      07-17-2003
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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
> }
> }



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Bind data in a ListBox with multiple selectionmode Fabio Visin ASP .Net 3 02-21-2008 09:33 AM
Using a data-bind dropdownlist to populate another data-bind dropdownlist mr2_93 ASP .Net 1 10-02-2005 05:07 PM
Using a ListBox in a custom server control =?Utf-8?B?R3lydXNHTQ==?= ASP .Net 0 05-18-2004 02:41 AM
ControlDesigner not invoked on custom control when control is rendered within another custom control Matt Sokol ASP .Net Building Controls 2 08-07-2003 07:13 AM



Advertisments