asp.net ObjectDataSource example: how to populate DropDownList
DropDownListObjectDataSourceExample.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net ObjectDataSource example: how to populate DropDownList</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Navy">DropDownList ObjectDataSource Example</h2>
<asp:ObjectDataSource
ID="ObjectDataSource1"
runat="server"
TypeName="ProductList"
SelectMethod="GetProducts"
>
</asp:ObjectDataSource>
<asp:DropDownList
ID="DropDownList1"
runat="server"
DataSourceID="ObjectDataSource1"
DataTextField="ProductName"
DataValueField="ProductID"
BackColor="Crimson"
ForeColor="AliceBlue"
>
</asp:DropDownList>
</div>
</form>
</body>
</html>
ProductList.cs
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
/// <summary>
/// Summary description for ProductList
/// </summary>
public class ProductList
{
public ProductList(){}
public DataSet GetProducts()
{
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = WebConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlCommand myCommand = new SqlCommand();
myCommand.CommandText = "SELECT TOP 15 ProductID, ProductName From Products";
myCommand.Connection = myConnection;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet productsDataSet = new DataSet();
myAdapter.Fill(productsDataSet);
return productsDataSet;
}
}
- asp.net ObjectDataSource example: how to populate BulletedList
- asp.net ObjectDataSource example: how to populate CheckBoxList
- asp.net ObjectDataSource example: how to populate RadioButtonList
- asp.net ObjectDataSource example: how to populate ListBox
- asp.net ObjectDataSource example: populate FormView
- asp.net ObjectDataSource example: populate GridView
- asp.net ObjectDataSource example: populate DetailsView