Checkbox CheckChanged Problem
Hello Everyone,
I am a newbie to VB and I am trying to Insert/Delete records in a MSSQL database based on if a checkbox is checked or unchecked. The checkbox is an item template in a datagrid, I can check/uncheck the checkbox but it doesn't do an insert or delete. Here is my code:
ASPX Page:
HTML Code:
<asp:TemplateColumn><ItemTemplate><asp:CheckBox runat="server" ID="chkMap" OnCheckedChanged="chkmap_CheckChanged" AutoPostBack="true"></asp:CheckBox></ItemTemplate></asp:TemplateColumn>
.VB Page:
Code:
Private Sub chkmap_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Data As New Stored.BusinessLogicLayer.CInput.DataInput
Dim Deletion As New Stored.BusinessLogicLayer.CInput.DataRemoval
Dim ceId As Integer = CInt(Session("ceid"))
Dim userId As Integer = CInt(Session("userid"))
Dim manchestStore As Integer = CInt(Session("mancheststore"))
Dim compId As Integer
Dim uComp As Integer
Dim assignCount As Integer = 0
Dim i As Integer
For i = 0 To dgCStores.Items.Count - 1
If CType(dgCStores.Items(i).Cells(0).FindControl("chkMap"), CheckBox).Checked = True Then
compId = CInt(dgCStores.Items(i).Cells(1).Text)
uComp = CInt(dgCStores.Items(i).Cells(3).Text)
Data.InputUserMap(userId, ceId, uComp, manchestStore)
assignCount += 1
ElseIf CType(dgCStores.Items(i).Cells(0).FindControl("chkMap"), CheckBox).Checked = False Then
compId = CInt(dgCStores.Items(i).Cells(1).Text)
uComp = CInt(dgCStores.Items(i).Cells(3).Text)
Deletion.DeleteUserMapping(userId, ceId, uComp, manchestStore)
End If
Next
End Sub
I even tried to erasing all the code & doing something along these lines in the code behind:
Code:
Private Sub chkmap_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Data As New Stored.BusinessLogicLayer.CInput.DataInput
Dim Deletion As New Stored.BusinessLogicLayer.CInput.DataRemoval
Dim ceId As Integer = CInt(Session("ceid"))
Dim userId As Integer = CInt(Session("userid"))
Dim manchestStore As Integer = CInt(Session("mancheststore"))
Dim compId As Integer
Dim uComp As Integer
If chkmap.Checked = True Then
Data.InputUserMap(userId, ceId, uComp, manchestStore)
Else
Deletion.DeleteUserMapping(userId, ceId, uComp, manchestStore)
But then I get the error that the control chkmap has not been declared. I am confused as to how this needs to be declared, do I set a variable to the FindControl("chkmap") and then set it as variable.checked = true ?
Hope you guys can help me out
Thanks
Re: Checkbox CheckChanged Problem
Did you debug the code. Does it run inside the checked changed event. I doubt it wont. As you said the checkbox is inside the datagrid, you may handle the Itemcommand event of the datagrid..
Re: Checkbox CheckChanged Problem
Are you expecting that clicking the CheckBox will result in an immediate PostBack to the server?
As per the documentation here:
http://msdn.microsoft.com/en-us/libr...opostback.aspx
The AutoPostBack property is false, meaning that you would need a button, or some other control, to initiate the PostBack, at which point the CheckedChanged event of the CheckBox would occur.
You can change the AutoPostBack property if that is what you want.
Gary