|
-
May 9th, 2004, 01:27 PM
#1
Thread Starter
Lively Member
[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:
Private Sub cmdApprove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdApprove.Click
ApproveList()
End Sub
VB Code:
Private Sub ApproveList()
Dim db As New Database
db.ApproveTorrent(Me.dgTorrent)
LoadList()
End Sub
VB Code:
Public Sub ApproveTorrent(ByVal datagrid As DataGrid)
Dim con As New SqlConnection(ConnectionString)
Dim cmd As New SqlCommand("ApproveTorrent", con)
cmd.CommandType = CommandType.StoredProcedure
Dim param As SqlParameter
Dim dgItem As DataGridItem
Dim chkStatus As CheckBox
Dim torrentId As Integer
Try
con.Open()
For Each dgItem In datagrid.Items
chkStatus = CType(dgItem.Cells(7).Controls(1), CheckBox)
If chkStatus.Checked Then
torrentId = CType(dgItem.Cells(0).Text, Integer)
param = cmd.Parameters.Add("@TorrentID", SqlDbType.BigInt)
param.Value = torrentId
cmd.ExecuteNonQuery()
End If
Next dgItem
Finally
con.Close()
End Try
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.
-
May 9th, 2004, 11:35 PM
#2
Thread Starter
Lively Member
-
May 10th, 2004, 11:20 AM
#3
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|