将返回的 DropDownList 对象绑定到 asp:DropDownList 网络表单

Bind a returned object of DropDownList to an asp:DropDownList webform

为了帮助重用一些代码,我想生成 DropDownList 对象并将它们分配给 ASP.NET C# 中的 Web 表单字段。

我似乎无法将我的对象 (DropDownList) 绑定到我的网络表单。这是正确的方法吗?有没有更好的办法?我知道我会在其他 Web 表单上使用这个下拉菜单,而且很多人都喜欢它。我想将所有这些放在一个我可以调用的 class 中。

这是我 return 下拉对象的方法。

protected DropDownList ddNames() {

            DropDownList dd = new DropDownList();
            dd.Items.Clear();

            string selectSQL = "mysql stuff";

            string connString = "my string";
            SqlCommand cmd = new SqlCommand(selectSQL, conn);
            SqlDataReader reader;

            try  {
                conn.Open(); 
                ListItem newItem;
                reader = cmd.ExecuteReader();
                while ( reader.Read() )  {
                    newItem = new ListItem();
                    newItem.Text = DataHelpers.GetUserFirstLastFromID(Convert.ToInt32(reader["someid"]));
                    newItem.Value = reader["someid"].ToString();
                    dd.Items.Add(newItem);
                }
                reader.Close();
                newItem = new ListItem();
                newItem.Text = "Unassigned";
                newItem.Value = "999999";
                dd.Items.Add(newItem);
            }   catch (Exception ex)   {
                 throw ex;
            }   finally    {

                    if (conn != null)  {
                        conn.Dispose();
                        conn.Close();
                    }
             }

        return dd;

        }

在我的网络表单上,它是一个简单的

<asp:DropDownList ID="dd_name" runat="server"></asp:DropDownList>

然后在我的代码隐藏中,我尝试调用类似的东西但它不起作用:

dd_name.DataSource = ddNames();

我还尝试了 Bind 和其他东西。

这可能吗?有没有更好的方法?

我已经调整了您的代码,以便您能够将数据绑定到下拉列表

protected ICollection ddNames() {

        string selectSQL = "mysql stuff";

        string connString = "my string";
        SqlCommand cmd = new SqlCommand(selectSQL, conn);
        SqlDataReader reader;
    // Create a table to store data for the DropDownList control.
     DataTable dt = new DataTable();

     // Define the columns of the table.
     dt.Columns.Add(new DataColumn("NewItemTextField", typeof(String)));
     dt.Columns.Add(new DataColumn("NewItemValueField", typeof(String)));

     dt.Rows.Add(CreateRow("Unassigned", "999999", dt));
        try  {
            conn.Open(); 
            reader = cmd.ExecuteReader();
            while ( reader.Read() )  {
                    // Populate the table with sample values.
                    dt.Rows.Add(CreateRow(DataHelpers.GetUserFirstLastFromID(Convert.ToInt32(reader["someid"])), reader["someid"].ToString(), dt));
            }
            reader.Close();
        }   catch (Exception ex)   {
             throw ex;
        }   finally    {

                if (conn != null)  {
                    conn.Dispose();
                    conn.Close();
                }
         }

    return dt;

    }

  DataRow CreateRow(String Text, String Value, DataTable dt)
  {

     // Create a DataRow using the DataTable defined in the 
     // CreateDataSource method.
     DataRow dr = dt.NewRow();

     // This DataRow contains the NewItemTextField and NewItemValueField 
     // fields, as defined in the CreateDataSource method. Set the 
     // fields with the appropriate value. Remember that column 0 
     // is defined as NewItemTextField, and column 1 is defined as 
     // NewItemValueField.
     dr[0] = Text;
     dr[1] = Value;

     return dr;

  }

最后,您对该方法的调用和对下拉列表的绑定将是:

 // Specify the data source and field names for the Text 
        // and Value properties of the items (ListItem objects) 
        // in the DropDownList control.
        dd_name.DataSource = ddNames();
        dd_name.DataTextField = "NewItemTextField";
        dd_name.DataValueField = "NewItemValueField";

        // Bind the data to the control.
        dd_name.DataBind();

        // Set the default selected item, if desired.
        dd_name.SelectedIndex = 0;

灵感来自 DropDownList Class