DropDownList in Asp.net
In this article I am going to tell you about Dropdownlist in asp.net.Dropdownlist is control that use to select the value from the list.You can select the value from the drop down list and use that value any where you want.You can add data in list item from its property or you can bind data from database.Its all depend on you how you want to add data in your dropdownlist. Well First of all i want to tell all of you that is the beginner's article. So I assume that you are a fresher in asp.net.So Lets start some practicle here. It is very simple example of the databinding with dropdownlist.Select the dropdownlist from the toolbox in Visual studio and place it on your page. I will bind the data with dropdownlist from database. So now come to coding part . Go to the aspx.cs file of your page and add the following code in your cs file.
Here is the code :
Here is the code :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public partial class Admin_City : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindStates();
}
private void BindStates()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmdFetchStates = new SqlCommand("select * from Hm_StateMaster",con);
cmdFetchStates.CommandType = CommandType.Text;
SqlDataAdapter daFetchStates = new SqlDataAdapter(cmdFetchStates);
DataSet dsFetchStates = new DataSet();
daFetchStates.Fill(dsFetchStates);
return dsFetchStates;ddlstate.DataSource =dsFetchStates;
ddlstate.DataTextField = "statename";
ddlstate.DataValueField = "id";
ddlstate.DataBind();
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
BindStates();
}
private void BindStates()
{
con.ConnectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
if (con.State == ConnectionState.Closed)
{
con.Open();
}
SqlCommand cmdFetchStates = new SqlCommand("select * from Hm_StateMaster",con);
cmdFetchStates.CommandType = CommandType.Text;
SqlDataAdapter daFetchStates = new SqlDataAdapter(cmdFetchStates);
DataSet dsFetchStates = new DataSet();
daFetchStates.Fill(dsFetchStates);
return dsFetchStates;ddlstate.DataSource =dsFetchStates;
ddlstate.DataTextField = "statename";
ddlstate.DataValueField = "id";
ddlstate.DataBind();
}
}
Now Let us discuss some points here. I hope you know about SqlCommand class,SqlDataAdapter and Dataset. If not then you can learn about these classes in my upcoming articles. Presently we are talking about dropdownlist so here ddlstate is the name of dropdownlist. I use two properties here first is DataTextField and DataValueField
DataTextField- DataTextField is use to fetch the text from the datasource
DataValueField- DataValueField is use to fetch the value from the datasource
now for simplicity take a example.
We have a list all the states of a India. Each state is store into database and and every state have one id. Here is the Sample table
id statename
1 Punjab
2 Haryana
3 Haryana
4 Uttrakhand
5 Maharashtra
6 Madhya Pardesh
7 Gujrat
8 Jammu and Kashmir
9 Rajsthan
10 Uttar Pardesh
11 Tamil Nadu
12 Tamil Nadu
1 Punjab
2 Haryana
3 Haryana
4 Uttrakhand
5 Maharashtra
6 Madhya Pardesh
7 Gujrat
8 Jammu and Kashmir
9 Rajsthan
10 Uttar Pardesh
11 Tamil Nadu
12 Tamil Nadu
Now see here every state has an id. Now suppose you want to show name of state in dropdownlist and also want to use its id in back end code. For this you can use both the properties DataTextField and DataValueField
Now I can use DataTextField for display the name of states into dropdownlist and DataValueField to store id of each state. But you cannot see the id from front end.You can work with it from coding part only. But if you want to display id then use id in DataTextField.
ddlstate.DataTextField = "statename";
ddlstate.DataValueField = "id";
ddlstate.DataValueField = "id";
see here You have to set the value same as the table column here. I want to display statename so i use statename for DataTextField and i want id for use in my backend only so i use it in DataValueField.
so i hope you understand the concept of DataTextField and DataValueField. Now here one function is also in use that function is Databind()
Databind function bind the datasource with the dropdownlist and display the data.
So This is the small example of Dropdownlist. I hope you learn something from this article.Your comments and feedback is important for me so please comment for this article how do you like my this article
0 comments:
Post a Comment