Results 1 to 3 of 3

Thread: [RESOLVED] CheckBox on a web DataGrid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    95

    [RESOLVED] CheckBox on a web DataGrid

    I have a template column in my web datagrid for an ASP.NET page. In this template column, I have a CheckBox control. I want to be able to check off boxes and then hit a command button to start a script that iterates through the checkbox items on the datagrid, and if one of the rows has a checked box, it executes a SQL stored procedure using the ID from that row.

    Problem is, when I debug it shows the checkboxes as unchecked every time. Even if I checked them before clicking the command button. How can I save the status of the checkboxes when I execute the command button handler?

    Here's my code.

    VB Code:
    1. Private Sub cmdApprove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApprove.Click
    2.         ApproveList()
    3.     End Sub

    VB Code:
    1. Private Sub ApproveList()
    2.         Dim db As New Database
    3.         db.ApproveTorrent(Me.dgTorrent)
    4.         LoadList()
    5.     End Sub

    VB Code:
    1. Public Sub ApproveTorrent(ByVal datagrid As DataGrid)
    2.  
    3.         Dim con As New SqlConnection(ConnectionString)
    4.         Dim cmd As New SqlCommand("ApproveTorrent", con)
    5.         cmd.CommandType = CommandType.StoredProcedure
    6.         Dim param As SqlParameter
    7.  
    8.         Dim dgItem As DataGridItem
    9.         Dim chkStatus As CheckBox
    10.         Dim torrentId As Integer
    11.  
    12.         Try
    13.             con.Open()
    14.             For Each dgItem In datagrid.Items
    15.                 chkStatus = CType(dgItem.Cells(7).Controls(1), CheckBox)
    16.                 If chkStatus.Checked Then
    17.                     torrentId = CType(dgItem.Cells(0).Text, Integer)
    18.                     param = cmd.Parameters.Add("@TorrentID", SqlDbType.BigInt)
    19.                     param.Value = torrentId
    20.                     cmd.ExecuteNonQuery()
    21.                 End If
    22.             Next dgItem
    23.         Finally
    24.             con.Close()
    25.         End Try
    26.  
    27.     End Sub

    As it is, that If statement is never true, because none of the checkboxes are saved when the command executes so even if I checked them off before clicking the Approve button, immediately they are all changed to checked.false.

    I even tried doing a postback with the control but I'm not quite sure how to implement that. I realize I could have it execute the SQL command one check at a time but wouldn't that decrease performance?

    Any ideas?
    Last edited by Kt3; May 10th, 2004 at 11:20 AM.

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    95
    Anybody?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Apr 2004
    Posts
    95
    Nevermind, I resolved this problem.

    What happened was, I had the code that fills the datagrid run on the Page_Load event. So when I checked a checkbox, hit the command button to start the ApproveTorrent() subroutine that iterates over the datagrid items, it would run the Page_Load event first before doing so, which would rebind the datagrid and clear the checkbox values. By implementing an if statement to only bind the datagrid if the page is being loaded for the first time (and not on postback), I effectively saved the datagrid's state whenever i click my command button. Iterating over the items in the grid now shows which ones were checked off and which ones weren't prior to my clicking the command button.

    KT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width