0

我有一个正在编程的 Web 应用程序,它基于用户输入构建 SQL 查询。我目前正在使用一些没有问题的复选框。但是,我没有对复选框进行硬编码,而是Page_Load使用以下代码动态填充它们:

protected void Page_Load(object sender, EventArgs e)
{
    //init();
    sql.Open();

    if (!Page.IsPostBack)
    {

        SqlCommand cmd = new SqlCommand("SELECT DISTINCT [value] FROM [table]", sql);
        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
            chkLst.Items.Add(rdr[0].ToString());
        rdr.Close();
    }
    sql.Close();
}

填充工作完美。但是,当我尝试检索chkLst.Items.Selected属性时,它总是设置为 false,从而使列表无法用于过滤。以下是我在提交时执行的代码的相关部分:

foreach (ListItem li in chkLst.Items)
    if (li.Selected)
        cmd += li.Text;

由于 Selected 属性始终为 false,因此我的命令始终为空。我能做些什么来解决这个问题?

4

1 回答 1

0

Someone else had answered my question, but it looks like his answer was deleted, either by him or SO's read-only period today.

The solution was to populate my list in Page_Init instead of Page_Load.

Thanks to the now anonymous respondent!

于 2013-01-30T00:28:40.020 回答