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?




Reply With Quote