摘要:[ASP.NET](note)DropDownList的資料繫結
DropDownList與資料庫的資料連結,一般可以SqlDatasource小精靈來處理,
但身為程式設計師總不能事事靠小精靈,有時候會遇上小精靈無法做到的關卡,
寫ADO.NET吧,以下為我測試利用ADO.NET撈出資料後如何丟給DropDownList
來呈現資料的方式。
//1.利用前台DropDownList的DataSource屬性與後台的ADO.NET配合來做。
後端:
protected void Page_Load(object sender, EventArgs e)
{
Page.DataBind();
}
public SqlDataReader DLfrom1() //這裡是用「public SqlDataReader」
{
string ds = WebConfigurationManager.ConnectionStrings["MailToConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ds);
SqlDataReader dr = null;
string dc = "select * from FromSetting ";
SqlCommand cmd = new SqlCommand(dc, conn);
conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//加上CommandBehavior.CloseConnection自動關閉連線,就能安心return
dr.Read();
return dr;
}
前台:
<asp:DropDownList ID="DropDownList1" runat="server" DataSource="<%# DLfrom1()%>"
DataTextField="useFrom" DataValueField="usePort" >
</asp:DropDownList>
//2.在後端直接用ADO.NET處理(方式1)(迴圈去read)。
protected void DLfrom()
{
string ds = WebConfigurationManager.ConnectionStrings["MailToConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ds);
SqlDataReader dr = null;
string dc = "select * from FromSetting ";
SqlCommand cmd = new SqlCommand(dc, conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList_from.Items.Add(new ListItem(dr["useFrom"].ToString(), dr["id"].ToString()));
}
cmd.Cancel();
dr.Close();
conn.Close();
conn.Dispose();
//http://msdn.microsoft.com/zh-tw/library/cc464128(v=vs.71).aspx
}
//3.在後端直接用ADO.NET處理(方式2)(用DataSource)。
protected void DLfrom()
{
string ds = WebConfigurationManager.ConnectionStrings["MailToConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ds);
SqlDataReader dr = null;
string dc = "select * from FromSetting ";
SqlCommand cmd = new SqlCommand(dc, conn);
conn.Open();
dr = cmd.ExecuteReader();
//dr.Read(); 不須加上這句,不然會失效, 因為已有.DataSource了,不須再跑迴圈去read
DropDownList_from.DataSource = dr;
DropDownList_from.DataValueField = "id";
DropDownList_from.DataTextField = "useFrom";
DropDownList_from.DataBind();
cmd.Cancel();
dr.Close();
conn.Close();
conn.Dispose();
//http://msdn.microsoft.com/zh-tw/library/cc464128(v=vs.71).aspx
}
//4.在後端直接用ADO.NET(方式3)(迴圈去read)。
protected void DLfrom()
{
string ds = WebConfigurationManager.ConnectionStrings["MailToConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(ds);
SqlDataReader dr = null;
string dc = "select * from FromSetting ";
SqlCommand cmd = new SqlCommand(dc, conn);
conn.Open();
dr = cmd.ExecuteReader();
while (dr.Read())
{
DropDownList_from.DataValueField =dr["id"].ToString();
DropDownList_from.DataTextField = dr["useFrom"].ToString();
DropDownList_from.Items.Add(dr["useFrom"].ToString());
}
cmd.Cancel();
dr.Close();
conn.Close();
conn.Dispose();
//http://msdn.microsoft.com/zh-tw/library/cc464128(v=vs.71).aspx
}
PS:有 dr.read()就不須再設定.datasourcedd的屬性,
反之,有設定.datasource id的屬性,就不須再 dr.read()
另有關資料繫結,可參考我另一篇:
[ASP.NET][ADO.NET](note)資料繫結運算式(DataBinding Expression)專論
--
強烈建議購物網店或實體店家都必須使用關鍵字廣告or原生廣告來 將Yahoo上與聯播網的廣大流量導至自己的網站!
●Yahoo關鍵字廣告/原生廣告
◆Yahoo廣告方案介紹 : https://goo.gl/5k8FHW
◆Yahoo廣告剖析與運用 : http://goo.gl/4xjUJD