通过数据库填充复选框

时间:2010-04-15 15:31:47

标签: c# asp.net data-binding

我必须使用来自数据库的数据填充复选框,但我的页面上没有显示复选框。请让我知道正确的方法。在C#中,我编写的page_load方法是:

public partial class dbTest1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string Server = "al2222";
        string Username = "hshshshsh";
        string Password = "sjjssjs";
        string Database = "database1";

        string ConnectionString = "Data Source=" + Server + ";";
        ConnectionString += "User ID=" + Username + ";";
        ConnectionString += "Password=" + Password + ";";
        ConnectionString += "Initial Catalog=" + Database;
        string query = "Select * from Customer_Order where orderNumber = 17";

        using (SqlConnection conn = new SqlConnection(ConnectionString))
        {
            using (SqlCommand cmd = new SqlCommand(query, conn))
            {
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (!IsPostBack)
                    {
                        Interests.DataSource = dr;
                        Interests.DataTextField = "OptionName";
                        Interests.DataValueField = "OptionName";
                        Interests.DataBind();
                    }
                }
                conn.Close();
                conn.Dispose();
            }
        }
    }
}

在.aspx中,我有这个:

<asp:CheckBoxList ID="Interests" runat="server"></asp:CheckBoxList>

请告诉我完成此任务的正确方法。

1 个答案:

答案 0 :(得分:1)

虽然您的问题已经得到解答(通过连接字符串注释),但我认为我可以用一种可能的方法来重写它。我作为评论开始这个,但它有点长而且笨拙。请注意,这并不能直接回答您的问题,但需要考虑代码清洁度以及回发时可能(可能非常温和)的性能提升。

protected void Page_Load(object sender, EventArgs e)
{
    // If we're in postback, let's not poll the database.
    if (Page.IsPostback)
        return; // Change this if you do need some postback processing here.

    // I assume that in the real world you pull this info from web.config
    string Server = "al2222";
    string Username = "hshshshsh";
    string Password = "sjjssjs";
    string Database = "database1";

    string ConnectionString = "Data Source=" + Server + ";";
    ConnectionString += "User ID=" + Username + ";";
    ConnectionString += "Password=" + Password + ";";
    ConnectionString += "Initial Catalog=" + Database;
    string query = "Select * from Customer_Order where orderNumber = 17";

    using (SqlConnection conn = new SqlConnection(ConnectionString))
    {
        using (SqlCommand cmd = new SqlCommand(query, conn))
        {
            conn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            // Going to assume that you're only getting 1 record 
            // due to apparent key (orderNumber = 17) in query?
            // You can also consider "if (dr.Read())", but fundamentally
            // they will do the same thing.
            while (dr.Read())
            {
                Interests.DataSource = dr;
                Interests.DataTextField = "OptionName";
                Interests.DataValueField = "OptionName";
                Interests.DataBind();
            }
            // I've excised the calls to .Close() and .Dispose(),
            // as the using block covers them for you.
        }
    }
}

为什么我们会走这条路?

  1. 在您的原始代码中,您正在轮询数据库(并且可能循环,如果我的假设是单记录查询是错误的)每个页面加载,无论您是否处于回发。你没有检查回发,直到你进入循环,其中损坏大部分已经完成。在我列出的代码中,如果您处于回发状态,则会完全缩短Page_Load()。当然,如果您还需要对回发进行一些加载事件处理,您可以将其更改为if / else并将这些组括起来。这也简化了您的循环代码。
  2. 您的using块涵盖了为您处理/关闭连接的问题。因此,您不需要额外的代码。
  3. 正如OrbMan在评论中所述,希望在您的实际代码中,您从web.config文件中检索所有连接字符串信息而不是硬编码,对吗?
  4. 最终最终无关的注释:这是很多数据访问代码,.NET Framework的新版本通过Entity Framework和LINQ-to-SQL等工具大大简化了。还有第三方数据访问层工具(如SubSonic和ActiveRecord)可以简化这一过程。使用这些工具将大大减少您在此处编写的代码量 - 我猜您在整个应用程序中也使用了相当多的类似代码,因此这些工具将为开发人员提供相当的代码提高生产力。 (而且更简单的道路维护。)

    只是值得深思。

相关问题