C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

Fill dropdownlist


Posted Date: 04 Dec 2008      Posted By: saravanan      Member Level: Bronze     Points: 1   Responses: 2



How to fill dropdownlist with AutoPostback option(Exclude AJAX) in asp.net




Responses

Author: Patel    04 Dec 2008Member Level: SilverRating: 2 out of 52 out of 5     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 2008Member Level: BronzeRating: 2 out of 52 out of 5     Points: 1

Hi, Can u explain me your problem in some detail.

regards
rajkumar



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : Create XML File
Previous : windows control in web Application
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use