• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 553
  • Last Modified:

Checkbox control for Datagrid in ASP .NET

i'm trying to get the control of the checkbox i had placed in a datagrid.
but for some reasons, the control does not seems to work.
i tried printing out the checked value, and i get all false even if i checked on the datagrid.

did i miss out on something, like setting the properties of the datagrid?

my codes are as follows:
--------------------------------------------------------------------------------------------------

        Dim GridItem As DataGridItem
        Dim chkExport As System.Web.UI.WebControls.CheckBox
        Dim oExArgs As New System.Collections.ArrayList
        Dim sID As String

        For Each GridItem In DataGrid1.Items
            chkExport = GridItem.FindControl("b")
            Response.Write(chkExport.Checked)

            If chkExport.Checked = False Then
                sID = CType(GridItem.FindControl("a"), Label).Text
                oExArgs.Add(sID)
            End If
        Next

-------------------------------------------------------------------------------------------------
0
jericho_law
Asked:
jericho_law
  • 5
  • 2
1 Solution
 
jericho_lawAuthor Commented:
Need URGENT help on this.

Please Help.
Thanks.
0
 
NetDeveloperCommented:
Hi jericho, u must cast the control to CheckBox - as u did later for the Label 'a'

 For Each GridItem In DataGrid1.Items

           ' do the cast to CheckBox
            chkExport = CType(GridItem.FindControl("b"),CheckBox)
            Response.Write(chkExport.Checked)

            If chkExport.Checked = False Then
                sID = CType(GridItem.FindControl("a"), Label).Text
                oExArgs.Add(sID)
            End If
        Next
0
 
jericho_lawAuthor Commented:
i tried adding the casting you mentioned.
it returned me a error stating that Checkbox is a type and cannot be used as an expression.

i tried using the codes i posted in my previous project and it work perfectly fine.
but now i'm having problems with the same exact coding.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jericho_lawAuthor Commented:
no more errors now, but it still doesn't work.
i get all false even if i checked on the checkbox.
prior to what i mentioned earlier, the codes work well in my previous project.
i'm thinking what could be the problem as it seems like it can't be able to get the status of the checkbox.

----------------------------------------------------------------------------------------------------------------

For Each GridItem In DataGrid1.Items
            chkExport = CType(GridItem.FindControl("b"), CheckBox)
            Response.Write(chkExport.Checked)

            If chkExport.Checked = False Then
                sID = CType(GridItem.FindControl("a"), Label).Text
                oExArgs.Add(sID)
            End If
Next
0
 
NetDeveloperCommented:
did u bind ur grid in Page_Load whe Page.IsPostBack = false ?
0
 
jericho_lawAuthor Commented:
where can i check that out?
0
 
jericho_lawAuthor Commented:
Thanks!
I managed to solve it now. I left out the page load binding:
----------------------------------------------------------------------------------
If Page.IsPostBack = False Then
....
End if
----------------------------------------------------------------------------------

Now i have another issue.
I wanted do a delete of my database.
But there's a error to it.

--------------------- my vb -----------------------------
        Dim conAuthors As OleDbConnection
        Dim strDelete As String
        Dim cmdDelete As OleDbCommand

        conAuthors = New OleDbConnection(strConn)
        strDelete = "Delete From prescription Where nric='" & Session("payment") & "'"
        cmdDelete = New OleDbCommand(strDelete, conAuthors)
        conAuthors.Open()
        cmdDelete.ExecuteNonQuery()
        conAuthors.Close()
---------------------------------------------------------------

--------------- error -------------------------

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.OleDb.OleDbException: Could not delete from specified tables.

Source Error:

Line 74:         cmdDelete = New OleDbCommand(strDelete, conAuthors)
Line 75:         conAuthors.Open()
Line 76:         cmdDelete.ExecuteNonQuery()
Line 77:         conAuthors.Close()
Line 78:
 
Source File: c:\inetpub\wwwroot\Clinic\payment.aspx.vb    Line: 76

Stack Trace:

   [OleDbException (0x80004005): Could not delete from specified tables.]
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(Int32 hr)
   System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
   System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
   System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
   System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
   System.Data.OleDb.OleDbCommand.ExecuteNonQuery()
   Clinic.payment.Button2_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\Clinic\payment.aspx.vb:76
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now