I have a web application I am programming that builds a SQL query based on user input. I am currently using a few check boxes without problems. However, instead of hard-coding the check boxes, I dynamically populated them on Page_Load
with the following code:
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();
}
Populating works perfectly. However, when I try to retrieve chkLst.Items.Selected
property, it's always set to false, and thus renders the list useless for filtering. Below is the relevant segment of my code that executes on submit:
foreach (ListItem li in chkLst.Items)
if (li.Selected)
cmd += li.Text;
Since the Selected property is always false, my command always comes up empty. What can I do to fix this?
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!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.