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