I have been struggling trying to get the listview with 1 checkbox has an itemtemplate to show me either the datakey value or the Text value.
<ItemTemplate>
<asp:CheckBox ID="chkPreferences" runat="server" Text='<%#Eval ("DESCRIPTION") %>' />
</ItemTemplate>
I have the datakeynames property set on the listview to PreferenceID
then in my code behind I am binding the listvie with this code
if (Session["IDValue"] != null)
{
int gen = Convert.ToInt32(Session["IDValue"]);
lstPreferences.DataSource = mdb.GetPreferences(gen);
lstPreferences.DataBind();
}
Then I added a Button to the page which this code in the click event.
if (lstPreferences.Items.Count > 0)
{
for (int i = 0; i < lstPreferences.Items.Count; i++)
{
CheckBox listChecked = (CheckBox)lstPreferences.Items[i].FindControl("chkPreferences");
if (listChecked.Checked)
{
LabelResult.Text = listChecked.Text;
}
}
}
Basically I was trying to get the checked value of each checkbox checked and display it in a label for now. I have searched many people having this issue and tried to restructure the code a few different ways but in the end I cant seem to figure out what I am doing wrong. Also when I put listChecked.Checked it does not even recognized that the checkbox is checked.
Thanks