从数据库填充 DropDownList 的正确方法是什么?

编程入门 行业动态 更新时间:2024-10-26 12:32:01
本文介绍了从数据库填充 DropDownList 的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在从 SQL Server 数据库填充 DropDownList,如下所示.它工作正常,但我不确定这是一个好方法.有人可以对这种方法有所了解,并进行一些改进吗?

I am populating a DropDownList from a SQL Server database as shown below. It works fine, but I'm not sure it's a good way. Can someone shed some light on this method, and give some improvements?

private void LoadSubjects() { ddlSubjects.Items.Clear(); string selectSQL = "SELECT SubjectID,SubjectName FROM Students.dbo.Subjects"; SqlConnection con = new SqlConnection(connectionString); SqlCommand cmd = new SqlCommand(selectSQL, con); SqlDataReader reader; try { ListItem newItem = new ListItem(); newItem.Text = "<Select Subject>"; newItem.Value = "0"; ddlSubjects.Items.Add(newItem); con.Open(); reader = cmd.ExecuteReader(); while (reader.Read()) { newItem = new ListItem(); newItem.Text = reader["SubjectName"].ToString(); newItem.Value = reader["SubjectID"].ToString(); ddlSubjects.Items.Add(newItem); } reader.Close(); } catch (Exception err) { //TODO } finally { con.Close(); } }

推荐答案

您可以将 DropDownList 绑定到数据源(DataTable、List、DataSet、SqlDataSource 等).

You could bind the DropDownList to a data source (DataTable, List, DataSet, SqlDataSource, etc).

例如,如果您想使用数据表:

For example, if you wanted to use a DataTable:

ddlSubject.DataSource = subjectsTable; ddlSubject.DataTextField = "SubjectNamne"; ddlSubject.DataValueField = "SubjectID"; ddlSubject.DataBind();

编辑 - 更完整的示例

private void LoadSubjects() { DataTable subjects = new DataTable(); using (SqlConnection con = new SqlConnection(connectionString)) { try { SqlDataAdapter adapter = new SqlDataAdapter("SELECT SubjectID, SubjectName FROM Students.dbo.Subjects", con); adapter.Fill(subjects); ddlSubject.DataSource = subjects; ddlSubject.DataTextField = "SubjectNamne"; ddlSubject.DataValueField = "SubjectID"; ddlSubject.DataBind(); } catch (Exception ex) { // Handle the error } } // Add the initial item - you can add this even if the options from the // db were not successfully loaded ddlSubject.Items.Insert(0, new ListItem("<Select Subject>", "0")); }

要通过标记而不是代码隐藏设置初始值,请指定选项并将 AppendDataBoundItems 属性设置为 true:

To set an initial value via the markup, rather than code-behind, specify the option(s) and set the AppendDataBoundItems attribute to true:

<asp:DropDownList ID="ddlSubject" runat="server" AppendDataBoundItems="true"> <asp:ListItem Text="<Select Subject>" Value="0" /> </asp:DropDownList>

然后您可以将 DropDownList 绑定到代码隐藏中的数据源(请记住删除:

You could then bind the DropDownList to a DataSource in the code-behind (just remember to remove:

ddlSubject.Items.Insert(0, new ListItem("<Select Subject>", "0"));

从代码隐藏开始,否则您将有两个 "" 项.

from the code-behind, or you'll have two "" items.

更多推荐

从数据库填充 DropDownList 的正确方法是什么?

本文发布于:2023-10-22 23:15:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   数据库   方法   DropDownList

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!