Oct 12, 2015

Posted in , , , ,

Binding the Drop Down List Form Database

In the web development the drop down list is most vital tool, we have a tendency to Use several time and lots of places to use this for showing the list in hidden format. And to fill date in to this list, after we need on page the choose a particular item in this drop down list in to ASP.NET code.Now, I want to fill all class names in to drop down list that are store in the data base. For this a have a table that has classes name.

Binding the Drop Down List Form Database

Next step, create a web page. Take a drop down list in to page. Write the following code
<asp:DropDownList ID="ddlclassId"
runat="server" Width="128px" AutoPostBack="True">                     
</asp:DropDownList>

In below code we will give the drop down list id is "ddlclassId" for understanding to drop down. Then go to the coding page and make a function for selecting the value form data base.
private void FillClass()
{
DataSet ds=new DataSet();
ds=objc1.Select_Class();

/*
in the brown color coded part use for fetching data form table in to Dataset use some class file and Store processor for this .if you have other way then put here.
we select two values Class Name and Section Name in Class and Class Id ,Class id is primary key ..
*/

ddlclassId.DataTextField = "Class";
//dataTextField hold those value which are show in list .
ddlclassId.DataValueField = "ClassId";
//dataValueField use for get unicq value of selected item.
ddlclassId.DataBind();
ddlclassId.Items.Insert(0, new ListItem("Select", "0"));
//this line use for inserting "Select" in to list .
}

Call this function in to page load Event .
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
 
    FillClass();
 
}
}
HostForLIFE.eu
After that, "right click" on the design page and view in browser. I hope it works for you!

0 comments:

Post a Comment

thanks for your comment!