将DropDownList的返回对象绑定到asp:DropDownList webform

时间:2015-02-17 19:16:00

标签: c# asp.net object webforms

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

我似乎无法将我的对象(DropDownList)绑定到我的webform。这是正确的方法吗?有没有更好的办法?我知道我会使用这个下拉列表,很多人喜欢在其他网络表单上。我想将所有这些放在一个我可以打电话的课堂上。

这是我返回下拉对象的方法。

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和其他东西。

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

1 个答案:

答案 0 :(得分:2)

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

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启发