To create a data-bound dropdownlist we have to follow several steps. This time we are using Business entity which is CSharp Business Entity template and it is same as creating the business object. Suppose a business entity Category having three fields.
Category:
CatID
CatDescription
Active
Now we want to make a databound dropdownlist(DDL). Lets call the dropdownlist is CatDropDownList. So we have 2 steps:
1. Creating databound DDL
2. Inserting select text
Here we go:
1. Creating databound DDL.
try
{
Category category=new Category();
/*Generating and executing query*/
category.Query.AddResultColumn(Category.ColumnNames.CatID);
category.Query.AddResultColumn(Category.ColumnNames.CatDescription);
category.Query.Load();
/*databinding*/
this.CatDropDownList.Datasource=category.DefaultView;
this.CatDropDownList.DataTextField=Category.ColumnNames.CatDescription;
this.CatDropDownList.DataValueField=Category.ColumnNames.CatID;
this.CatDropDownList.DataBind();
2.Inserting Select text:
Generally if you add item now then DDL will not retain that item. this time you have to insert the values in the DDL. The required code is:
this.CatDropDownList.Items.Insert(0,"select a category");
}
catch(Exception exc){}
Now comes how use the value selected by the DDL.This is quite simple.
int selectedCatID=Convert.ToInt32(CatDropDownList.SelectedValue);
here selectedCatID will give the selected CatID of the category you selected....
Thanks.
Masud (5.8.9)
No comments:
Post a Comment