A question regarding my checkbox control within my gridview.

Last post 06-13-2008 8:43 AM by xyz789. 7 replies.

Sort Posts:

  • A question regarding my checkbox control within my gridview.

    06-11-2008, 4:33 PM
    • Member
      95 point Member
    • xyz789
    • Member since 01-29-2007, 12:43 PM
    • Posts 145

    I have a situation when I select multiple rows from a gridview, I would like for the values to be displayed in a listbox. However, when I click the checkboxes inside my gridview, the last row selected is the only data that is being displayed.

     

    Here is the code behind being used. The chkAgent is the name of the checkbox inside the gridview.

         Protected Sub chkAgent_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
             For index As Integer = 0 To gvAgents.Rows.Count - 1
                Dim chk As CheckBox = CType(gvAgents.Rows(index).FindControl("chkAgent"), CheckBox)
                If chk.Checked Then
                    Dim keyid As Label = gvAgents.Rows(index).FindControl("lblKeyID")
                    SqlDataSource2.SelectParameters("KEYID").DefaultValue = keyid.Text
                    SqlDataSource2.DataBind()
                    ListBox2.DataBind()
                End If

            Next

    Why will it add only one row and not multiple rows if checked?

    Thanks,

    xyz789

  • Re: A question regarding my checkbox control within my gridview.

    06-11-2008, 4:41 PM
    Answer
    • Contributor
      3,561 point Contributor
    • mellamokb
    • Member since 02-09-2007, 10:44 PM
    • Posts 644

    Hi,

    As your code stands now, you are re-binding the ListBox2 to a single row over and over for as many rows as there are.  When the page loads, you see the result of the last row being bound to ListBox2.  You need to build an array of the keyID and bind the array to ListBox2 after you have built it: 

    Dim keyidArray As ArrayList = New ArrayList()
    
    For index As Integer = 0 To gvAgents.Rows.Count - 1
      Dim chk As CheckBox = CType(...)
      If chk.Checked Then
        Dim keyid As Label = gvAgents.Rows(...)
        keyidArray.Add(keyid.Text)
      End If
    Next
    
    ListBox2.DataSource = keyidArray
    ListBox2.DataBind()

    Regards,

    ~ mellamokb

  • Re: A question regarding my checkbox control within my gridview.

    06-11-2008, 4:57 PM
    Answer
    • Participant
      1,238 point Participant
    • jchandra
    • Member since 05-15-2008, 9:36 AM
    • Jakarta, Indonesia
    • Posts 197

    Is ListBox2.DataSource = SqlDataSource2 ???

    I can only assume this since I don't see any code in there where you assign ListBox2.Items.

    I am assuming SqlDataSource2 will only retrieve one item at a time, yes?

    Well, basically your code is rebinding ListBox2 to whatever currently is in SqlDataSource2, and you are doing it every single time the code loop around. 

    Try to see it in your mind, say you have item 1, 3 and 5 checked in your gridview (let's just pretend the key is also 1, 3, and 5).

    The first time the code enter the loop

    you retrieve some key pair value using SqlDataSource2 where the key value is 1 and you bind it to ListBox2 (this is basically clearing all the current item in ListBox2 and reload the data in SqlDataSource2 into its item collection).

    Now the code loop around,

    you retrieve some other key pair value using SqlDataSource2 where the key value is 3 and you bind it to ListBox2 (again, this is basically clearing all the current item in ListBox2 and reload the data in SqlDataSource2 into its item collection). 

    Can you see the problem?

     What you want to do is probably can be done without automatically binding ListBox2 and just do it manually.

    Before you enter the loop, manually clear ListBox2.Items (ListBox2.Items.Clear())

    While you are in the loop, do whatever you need to do to get the key value pair based on the current selected / checked item key 

    and assign it to ListBox2.Items (ListBox2.Items.Add(new ListItem(valueToBeDisplayed, keyForTheDisplayedValue)

     

     

    Jimmy Chandra
    Blogging at Incoherent Rambling

    Mark this post as Answer if you think it helped you solve the problem.

  • Re: A question regarding my checkbox control within my gridview.

    06-12-2008, 12:12 PM
    • Member
      95 point Member
    • xyz789
    • Member since 01-29-2007, 12:43 PM
    • Posts 145

    Thank you for responding back. Actually I caught the binding of the ListBox2 right after I sent the message, oops.

    That is not what I wanted to do of course, but my main issue is that I am still unable to get the values I need in ListBox2

    I should have explain a little more about the code yesterday. You are right jchandra about the ListBox being equal to SqlDataSource2. I would like to get my values from SqlDataSource2.

    The sequel code in SqlDataSource2 is like this "SELECT KEY_ID, FIRST_NAME + ' ' + LAST_NAME AS FULL_NAME WHERE (KEY_ID = @KEYID)"

    The ListBox2 is set to this DataSourceID="SqlDataSource2" DataTextField="FULL_NAME" DataValueField="KEY_ID", however I am still unable to see the DataTextField in the listbox.

    Here is the revised code from yesterday.

        Protected Sub chkAgent_CheckedChanged1(ByVal sender As Object, ByVal e As System.EventArgs)
            ListBox2.Items.Clear()
            For index As Integer = 0 To gvAgents.Rows.Count - 1
                Dim chk As CheckBox = CType(gvAgents.Rows(index).FindControl("chkAgent"), CheckBox)
                If chk.Checked Then
                    Dim keyid As Label = gvAgents.Rows(index).FindControl("lblKeyID")
                    SqlDataSource2.SelectParameters("KEYID").DefaultValue = keyid.Text
                    ListBox2.Items.Add(New ListItem("FULL_NAME", "KEY_ID"))
                End If
               

            Next

     

    I am not sure that I have the right syntax for ListBox2.Items.Add when using "FULL_NAME" and "KEY_ID". What can I do?

     

    thanks,

     

    xyz789

     

     

     

     

  • Re: A question regarding my checkbox control within my gridview.

    06-12-2008, 1:29 PM
    Answer
    • Contributor
      3,561 point Contributor
    • mellamokb
    • Member since 02-09-2007, 10:44 PM
    • Posts 644

     Hi,

    Is the FULL_NAME field available from the GridView?  Can you use something like Dim txt As TextBox = CType(gvAgents.Rows(index).FindControl(txtFULL_NAME), TextBox) to get the TextBox, and then use ListBox2.Items.Add(New ListItem(txtFULL_NAME.Text, keyid))?  Then you wouldn't need the ObjectDataSource2 at all.

    Otherwise, if you want to use the ObjectDataSource2, then you have to retrieve the record from the ObjectDataSource each time and apply that to the ListItem, like so:  

                    Dim keyid As Label = gvAgents.Rows(index).FindControl("lblKeyID")
    
                    SqlDataSource2.SelectParameters("KEYID").DefaultValue = keyid.Text
                    Dim enumerable As IEnumerable = SqlDataSource2.Select(New DataSourceSelectArguments())
                    Dim enumerator As IEnumerator = enumerable.GetEnumerator()
    
                    enumerator.MoveNext()
                    Dim drv As DataRowView = DirectCast(enumerator.Current, DataRowView)
    
                    ListBox2.Items.Add(New ListItem(drv("FULL_NAME").ToString(), drv("KEY_ID").ToString()))
     

     Another way this could be done would be to generate the query dynamically, to be something like "SELECT KEY_ID, FIRST_NAME + ' ' + LAST_NAME AS FULL_NAME FROM MY_TABLE_NAME WHERE (KEY_ID IN (1, 2, 5, 7, 8))" where the list (1,2,5,7,8) is created by looping through all of the checkboxes that are checked.

    Of course the most efficient method of them all would be to only modify the single row in the ListBox that should be modified according to the checkbox that was checked or unchecked, rather than building the entire contents of the ListBox on every postback.  I can demonstrate that with some code maybe later tonight if I have some time. 

    (Note that you are missing the table name in your sequel query -- i don't know if that was intentional or if you left it out. )

    Regards,

    ~ mellamokb 

  • Re: A question regarding my checkbox control within my gridview.

    06-12-2008, 1:59 PM
    • Member
      95 point Member
    • xyz789
    • Member since 01-29-2007, 12:43 PM
    • Posts 145

    oops! Yeah I forgot to say which table I am getting my information from.

    I see what you are saying about building the listbox on every postback. 

    If you have time to share that code with me, that would be great!

    Thank you for the help.

    xyz789

     

    P.S.

    The alias FULL_NAME is a part of the gridview.

  • Re: A question regarding my checkbox control within my gridview.

    06-13-2008, 12:22 AM
    • Contributor
      3,561 point Contributor
    • mellamokb
    • Member since 02-09-2007, 10:44 PM
    • Posts 644

    Hi,

    I'm thinking something like this (assuming your checkbox is in a templatecolumn): 

    Dim cb As CheckBox = DirectCast(sender, CheckBox)
    Dim gvr As GridViewRow = cb.Parent.Parent
    Dim txtFullName As TextBox = DirectCast(gvr.FindControl("FULL_NAME"), TextBox)
    Dim keyid As Label = gvr.FindControl("lblKeyID")
    
    Dim li As ListItem = new ListItem(txtFullName.Text, keyid.Text)
    If cb.Checked Then
        ListBox1.Items.Add(li)
    Else
        ListBox1.Items.Remove(li)
    End If
    

     The ListBox should keep track of its contents automatically from postback to postback and not lose the previous items.

     Regards,

    ~ mellamokb

  • Re: A question regarding my checkbox control within my gridview.

    06-13-2008, 8:43 AM
    • Member
      95 point Member
    • xyz789
    • Member since 01-29-2007, 12:43 PM
    • Posts 145

    Thanks, that helped! Much better then going through the loop I had.

    Thank you,

    xyz789

Page 1 of 1 (8 items)