从数据库填充 DropDownList 的正确方法是什么? [英] What is the right way to populate a DropDownList from a database?

查看:42
本文介绍了从数据库填充 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 的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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