关于 c#:populate dropdown list from a list of objects | 珊瑚贝

populate dropdown list from a list of objects


在构建 3 层架构 c# asp.net 应用程序的尝试中,我开始构建一个数据库类,用于连接到数据库,另一个类是 City,每个类都有一个方法表中的列和一个 Cities 类,其中我有 GetCities 方法,该方法创建一个 City 对象列表,然后使用 DataSource 向导将控件设置为使用来自 GetCities() 的数据。
我得到的只是下拉列表中的空白。知道为什么吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        public List<City> GetCities()
    {
        List<City> cities = new List<City>();
        Database db = new Database();
        SqlConnection conn = db.GetConnection();
        String sql =“SELECT * FROM CITIES”;
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader reader = cmd.ExecuteReader();

        while (reader.Read())
        {
            City c = new City(reader.GetInt32(0), reader.GetString(1).ToString());
            cities.Add(c);
        }

        db.CloseConnection();
        return cities;
    }

谢谢

  • 问题出在您尚未共享的代码中!你如何填充列表? WIZARD 生成的代码是什么?
  • 您填写列表的代码看起来不错。问题一定是您如何绑定到下拉列表(也许您可以发布)?我知道你说你使用了一个巫师,这将是我首先要看的地方,看看它在做什么。此外,您正在关闭数据库连接,但如果出现异常,它不会被关闭。使用 try/finally 或在您对 ExecuteReader 的调用中指示连接将在阅读器关闭时关闭,并为您的阅读器使用 using() 语句。
  • 在 DropDownList 上单击”选择数据源”时访问我正在谈论的 WIZARD,并弹出一个窗口,提示我输入数据源。我选择新的数据源,然后弹出另一个窗口,我从中选择对象,其中包括”Access db、SQL db、Entity”等。之后另一个窗口弹出提示选择对象,在我的例子中是 Cities . Next > 选择一个方法,在我的例子中是 GetCities()。


您是否设置了 DataTextField、DataValueField 属性并调用了 DataBind?

在这一点上,我会尝试让这个概念尽可能简单地发挥作用,然后开始重新添加内容,直到找到问题所在。从一个全新的页面开始,添加一个 DropDownList 但不要触摸数据源或更改任何属性,直接进入代码隐藏并将其添加到 Page_Load:

1
2
3
4
5
6
7
8
DropDownList1.DataValueField =“ID”;
DropDownList1.DataTextField =“Name”;
DropDownList1.DataSource = new[] {
    new { ID = 1, Name =“Alice” },
    new { ID = 2, Name =“Mike” },
    new { ID = 3, Name =“John” }
};
DropDownList1.DataBind();

有用吗?它对我有用。然后尝试更改 DataValueField、DataTextField 和 DataSource 以使用您的客户列表。现在坏了吗?然后您就知道问题出在客户列表的某个地方,而不是您绑定数据的方式。

  • 我真的不知道,因为我从不使用向导,但在这种情况下它真的有帮助还是只是添加了一个不需要的层?我一直只是设置DataTextField,DataValueField,DataSource = someList,并调用DataBind。
  • 好点 J.D.,但即使将以下内容放入 page_load 中,结果也是相同的: protected void Page_Load(object sender, EventArgs e) { Cities c = new Cities(); ddlistLocation.DataSource = c.GetCities(); ddlistLocation.DataTextField =”名称”; ddlistLocation.DataBind(); }


您是否在要填充的对象上调用了 DataBind() 方法?

  • 我确实在 Page_Load() 中调用了 DropDownListCities.DataBind(),但没有。
  • 在从数据源中提取数据之前,您可能正在调用 DataBind()。只是为了检查这是否可能是问题尝试将 .DataBind() 移动到 Page_OnPrerender 事件处理程序中


问题出在 City 类中,经过仔细检查后,我意识到构造函数分配了错误接收的参数。它现在正在工作。谢谢!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class City
{
    int id;
    string name;

    public City(int id, string name)
    {
        this.id = id;
        this.name = name;

    }

    public int Id
    {
        get { return id; }
        set { id = value; }
    }
    public String Name
    {
        get { return name; }
        set { name = value; }
    }

}


来源:https://www.codenong.com/4490087/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_10046.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?