62,072
社区成员
发帖
与我相关
我的任务
分享
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string connStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
string cmdStr = "select top 5 CustomerID, CompanyName from Customers";
SqlCommand cmd = new SqlCommand(cmdStr, conn);
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sqlDataAdapter.Fill(ds);
this.DropDownList1.DataTextField = "CompanyName";
this.DropDownList1.DataValueField = "CustomerID";
this.DropDownList1.DataSource = ds.Tables[0];
this.DropDownList1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(this.DropDownList1.SelectedItem.Text);
}