1

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

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jay
  • 501
  • 2
  • 6
  • 17
  • http://stackoverflow.com/questions/9523263/how-can-i-get-the-checkboxlist-selected-values-what-i-have-doesnt-seem-to-work – Learner Jul 10 '13 at 14:28
  • Thanks for the response however that answer is for a checkboxlist I am using just a plane checkbox in my ListView. So this did not work for me. – Jay Jul 10 '13 at 15:31
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 10 '13 at 17:57
  • ListView is a tag so should i re-edit yours? – Jay Jul 10 '13 at 18:55
  • The consensus around here is that tags are okay if they are organic / natural / necessary for the title (which "ListView" is - it fits). John was just letting you know that it's not necessary to tack tags onto the end of your title (in fact, it's mildly frowned upon). – Josh Darnell Jul 10 '13 at 19:00
  • I just know that a listview exist in many languages and did not want to get the answer in any other language than c#. I guess im from a time where not telling people in your question what language your coding in usually means they will overlook your question. I will try and keep my questions more generic – Jay Jul 10 '13 at 19:02

1 Answers1

0

It sounds like you need to take your databinding code and make sure it only fires on the initial page load (not on postbacks). Otherwise, you're reloading the ListView with the original data from your database before the click event has a chance to fire.

Something like this should work:

If(!Page.IsPostBack)
{
    if (Session["IDValue"] != null)
    {
        int gen = Convert.ToInt32(Session["IDValue"]);
        lstPreferences.DataSource = mdb.GetPreferences(gen);
        lstPreferences.DataBind();          
    }
}
Josh Darnell
  • 11,304
  • 9
  • 38
  • 66
  • 1
    Thanks for your help it was a couple things aside from that causing the error but this helped as I forgot to add it. Thanks again – Jay Jul 10 '13 at 19:03
  • @Jay Awesome! Glad I could at least point you in the right direction =) – Josh Darnell Jul 10 '13 at 19:06