| Author: Patel 04 Dec 2008 | Member Level: Silver | Rating:  Points: 6 |
It is a common and repetitive task in web application development that a developer has to bind data to a DropDownList, a ListBox, a CheckBoxList, a RadioButtonList or a BulletedList. Data can be programmatically bound to a DropDownList control using the following code:
DropDownList1.DataSource = SomeDataSourc; DropDownList1.DataValueField = ValueDataName; DropDownList1.DataTextField = TextDataName; DropDownList1.DataBind(); ____________________________________________________________ //bind a list with a table data source public void PopulateList(System.Web.UI.WebControls.ListControl list, string valueDataName, string textDataName, DataTable dataTable) { list.DataSource = dataTable; list.DataValueField = valueDataName; list.DataTextField = textDataName; list.DataBind();
//DropDownList is a special case, which needs an empty item if (list.GetType().ToString().IndexOf("DropDownList")>-1) { list.Items.Add(new ListItem("", "")); list.SelectedIndex = list.Items.IndexOf(list.Items.FindByValue("")); } } ____________________________________________________________________ //build a comma seperated string for all selections (values or text) public string GetListSelections(System.Web.UI.WebControls.ListControl list, string collectValueOrText) { ListItemCollection ListItems = list.Items; string SelectedItems = ""; foreach (ListItem itm in ListItems)
{ if (itm.Selected) { if (collectValueOrText.ToLower() == "value") SelectedItems += itm.Value + ", "; else SelectedItems += itm.Text + ", "; } }
if (SelectedItems.Length > 0) { //remove trailing ", " SelectedItems = SelectedItems.Substring(0,SelectedItems.Length - 2); }
return SelectedItems; } ________________________________________________ For Multiple Col
Dim MyDT As New DataTable Dim MyRow As DataRow MyDT.Columns.Add(New DataColumn("DepartmentID", _ GetType(Int32))) MyDT.Columns.Add(New DataColumn("DepartmentName", _ GetType(String))) MyRow = MyDT.NewRow() MyRow(0) = 1 MyRow(1) = "Marketing" MyDT.Rows.Add(MyRow) MyRow = MyDT.NewRow() MyRow(0) = 2 MyRow(1) = "Sales" MyDT.Rows.Add(MyRow) MyRow = MyDT.NewRow() _____________________
<asp:CheckBoxList id="cbl1" runat="server" CellPadding="5" CellSpacing="5" RepeatColumns="3" RepeatDirection="Vertical" RepeatLayout="Table" TextAlign="Right" AutoPostBack="True" OnSelectedIndexChanged="cbl1_Clicked" >
|
| Author: raj 04 Dec 2008 | Member Level: Bronze | Rating:  Points: 1 |
Hi, Can u explain me your problem in some detail.
regards rajkumar
|