从后端的表中将数据检索到下拉列表中 [英] RETRIEVING DATA INTO DROPDOWNLIST FROM TABLE IN BACK END

查看:48
本文介绍了从后端的表中将数据检索到下拉列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从后端表中的列中检索项目列表,并在表单的下拉列表中显示这些项目..

HOW CAN I RETRIEVE A LIST OF ITEMS FROM A COLUMN IN A TABLE IN BACK END AND SHOW THESE ITEMS IN A DROP DOWN LIST IN THE FORM..??

推荐答案

ASP.Net DropDownList控件还支持数据绑定功能,该功能使您可以绑定从SQL数据库检索的数据项.在此示例中,我们使用了SQL Northwind数据库来将类别表与DropDownList控件绑定.有两种方法来绑定SQL数据,一种是使用内置的ASP.Net SQL数据源服务器控件,第二种是使用C#代码以编程方式连接和获取数据. SqlDataSource控件通过遵循其向导步骤提供了将数据与DropDownList控件绑定的功能.您可以轻松地连接到SQL数据库并指定SQL查询以获取所需的字段. DropDownList控件具有DataSourceID属性,该属性接受用于绑定SQL数据的DataSource控件的ID,例如此示例中的SQLDataSource控件的ID.

在此示例中,我们已使用C#代码连接到SQL数据库并获取类别.对于使用C#代码的下拉列表SQL数据绑定,我们使用了DataSource属性来传递具有从categorys表中检索到的SQL记录的数据对象.无论您使用SqlDataSource控件还是SQL数据绑定的C#代码,都必须使用DropDownList控件的以下2个属性来为其列表项指定text和value属性:

1. DataTextField
2. DataValueField

在这里,我们已将"CategoryName"字段指定为DataTextField以显示列表项的文本内容,将"CategoryID"字段指定为DataValueField来设置每个列表项的后端值.

请参阅下面的示例以了解ASP.Net DropDownList控件SQL DataBinding的输出结果:

查看普通副本到剪贴板吗?
< asp:dropdownlist id ="DropDownList1" runat =服务器" xmlns:asp =#unknown">



< asp:label id ="Label1" runat ="server" xmlns:asp =#unknown">
C#代码
查看普通副本到剪贴板吗?
受保护的void Page_Load(对象发送者,EventArgs e)
{
如果(!IsPostBack)
BindDropDownListData();
}

公共无效BindDropDownListData()
{
//连接字符串
字符串connectionString = ConfigurationManager.ConnectionStrings ["NorthwindConnectionString"].ConnectionString;

//用用于连接
的连接字符串初始化的Sql连接对象 //与Northwind SQL数据库一起使用
使用(SqlConnection mySqlConnection =新的SqlConnection(connectionString))
{
尝试
{
//打开Sql连接
mySqlConnection.Open();

//使用SQL查询初始化的Sql Command对象以检索类别
SqlCommand mySqlCommand =新的SqlCommand(从类别中选择CategoryID,CategoryName",mySqlConnection);

//通过传递Sql Command对象来初始化Sql Data Adapter对象
SqlDataAdapter mySqlDataAdapter =新的SqlDataAdapter(mySqlCommand);

//DataSet对象,用于存储检索到的SQL数据项
DataSet myDataSet =新的DataSet();

//填充数据集
mySqlDataAdapter.Fill(myDataSet);

//将DataSet对象设置为DropDownList的数据源
DropDownList1.DataSource = myDataSet;

//指定要显示为
的字段名称 //DropDownList项目的文本
DropDownList1.DataTextField =类别名称";

//指定要用作
的字段名称 //每个列表项的值
DropDownList1.DataValueField =类别ID";

//结束数据绑定
DropDownList1.DataBind();

}
catch(ex ex例外)
{
Label1.Text = ex.Message;
}
终于
{
//关闭Sql Connection
mySqlConnection.Close();
}
}
}
The ASP.Net DropDownList control also supports data binding feature that allows you to bind the data items retrieved from the SQL DataBase. In this sample we have used SQL Northwind DataBase to bind the categories table with DropDownList control. There are 2 ways to bind the SQL data with it, one is using in-built ASP.Net SQL datasource server control and second is using C# code to connect and fetch the data programmatically. The SqlDataSource control provides the functionality to bind the data with DropDownList control by following its wizard steps. You can easily connect to the SQL Database and specify the SQL query to fetch the desired fields. The DropDownList control has a DataSourceID property that accepts the ID of the DataSource control used to bind the SQL Data for example ID of SQLDataSource control in this sample.

In this sample we have used the C# code to connect to SQL Database and fetch the categories. For dropdownlist SQL databinding using C# code we have used the DataSource property to pass the data object having SQL records retrieved from the categories table. Whether you use SqlDataSource control or C# code for SQL DataBinding you have to use the following 2 properties of DropDownList control to specify the text and value property for its list items:

1. DataTextField
2. DataValueField

Here we have specified the "CategoryName" field as DataTextField to display the text content for list items and "CategoryID" field as DataValueField to set the back end value for each list item.

See the sample below to understand the output result of ASP.Net DropDownList control SQL DataBinding:

view plaincopy to clipboardprint?
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">



<asp:label id="Label1" runat="server" xmlns:asp="#unknown">
C# Code
view plaincopy to clipboardprint?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
BindDropDownListData();
}

public void BindDropDownListData()
{
// connection string
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

// Sql connection object initialized with connection string used to connect
// it with Northwind SQL database
using (SqlConnection mySqlConnection = new SqlConnection(connectionString))
{
try
{
// open the Sql connection
mySqlConnection.Open();

// Sql Command object initialized with SQL query to retrieve the categories
SqlCommand mySqlCommand = new SqlCommand("Select CategoryID, CategoryName from Categories", mySqlConnection);

// Sql Data Adapter object initialized by passing the Sql Command object
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(mySqlCommand);

// DataSet object to store the retrieved SQL data items
DataSet myDataSet = new DataSet();

// fill the DataSet
mySqlDataAdapter.Fill(myDataSet);

// Set DataSet object as DataSource for the DropDownList
DropDownList1.DataSource = myDataSet;

// Specify the Field Name that you want to display as
// text for DropDownList item
DropDownList1.DataTextField = "CategoryName";

// Specify the Field Name that you want to use as
// value for each list item
DropDownList1.DataValueField = "CategoryID";

// Finalize the DataBinding
DropDownList1.DataBind();

}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
finally
{
// close the Sql Connection
mySqlConnection.Close();
}
}
}


在页面加载中或要将数据加载到下拉列表中的任何位置
In page load or whereever you want to load data into dropdown
SqlCommand cmd=new SqlCommand("Select DepartmentName from Department",con);
SqlDataAdapter da=new SqlDataAdapter(cmd);
DataSet ds=new DataSet();
da.Fill(ds);
dropdownList1. DataSource=ds.Tables[0];
dropdownList1.DisplayMember ="DepartmentName";
dropdownList1.ValueMember ="DepartmentName";
dropdownList1.DataBind();


这篇关于从后端的表中将数据检索到下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆