locked
Cannot Access checkbox control inside gridview in asp.net RRS feed

  • Question

  • User1544368840 posted

    My need is to compare rows from two tables. Loaded 1 from first table and 1 from last table. binded it together in gridview(2 rows). added checkbox for all columns using template field programmatically.

    I couldnt access the checkboxes inside the gridview.

    Please help thanks in advance.

    Dim dset as new Dataset
            dset = objPatient.FetchPatientDetailsForCrossCheck(Session("phoneno"))
    
    
    
            For Each col As DataColumn In dset.Tables(0).Columns
    
                'Declare the bound field and allocate memory for the bound field.
    
                Dim bfield As New TemplateField()
                'Initalize the DataField value.
                bfield.HeaderTemplate = New GridViewTemplate(ListItemType.Header, col.ColumnName)
                'Initialize the HeaderText field value.
    
                bfield.ItemTemplate = New GridViewTemplate(ListItemType.Item, col.ColumnName)
                'Add the newly created bound field to the GridView.
                gvCrossCheck.Columns.Add(bfield)
    
            Next
    
            'Initialize the DataSource
            gvCrossCheck.DataSource = dset.Tables(0)
            'Bind the datatable with the GridView.
    
    
    
            Dim dtable As New DataTable("duplicate")
            dtable = objPatient.FetchDuplicatesPatientBasedOnPhoneNo(Session("phoneno")).Tables(0)
    
            Dim drow As DataRow = Nothing
    
            drow = dset.Tables(0).NewRow
            drow(0) = dtable.Rows(0).Item(0) 'PT_FIRSTNAME
            drow(1) = dtable.Rows(0).Item(1) 'PT_MIDDLENAME
            drow(2) = dtable.Rows(0).Item(2) 'PT_LASTNAME
            drow(3) = dtable.Rows(0).Item(3) 'PT_ADDRESS
            drow(4) = dtable.Rows(0).Item(4) 'PT_ADDRESS2
            drow(5) = dtable.Rows(0).Item(5) 'PT_CITY
     dset.Tables(0).Rows.Add(drow)
    
            gvCrossCheck.DataBind()
    

    Now am getting two rows in one gridview with checkbox in all columns. Used a class to add checkbox column programatically. class added below.

    Imports Microsoft.VisualBasic
    
    Public Class GridViewTemplate
        Implements ITemplate
        'A variable to hold the type of ListItemType.
        Private _templateType As ListItemType
        'A variable to hold the column name.
        Private _columnName As String
        'Constructor where we define the template type and column name.
    
        Public Sub New(ByVal type As ListItemType, ByVal colname As String)
            'Stores the template type.
            _templateType = type
            'Stores the column name.
            _columnName = colname
        End Sub
    
        Private Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
    
            Select Case _templateType
    
                Case ListItemType.Header
                    'Creates a new label control and add it to the container.
                    Dim lbl As New Label()
                    'Allocates the new label object.
                    lbl.Text = _columnName
                    'Assigns the name of the column in the lable.
                    container.Controls.Add(lbl)
                    'Adds the newly created label control to the container.
                    Exit Select
    
                Case ListItemType.Item
                    'Creates a new text box control and add it to the container.
                    'Dim tb1 As New TextBox()
                    Dim chk As New CheckBox
                    'Allocates the new text box object
    
                    AddHandler chk.DataBinding, AddressOf tb1_DataBinding
    
                    'Attaches the data binding event.
                    'tb1.Columns = 4
                    'Creates a column with size 4.
                    container.Controls.Add(chk)
                    'Adds the newly created textbox to the container.
                    Exit Select
    
                Case ListItemType.EditItem
                    'As, I am not using any EditItem, I didnot added any code here.
                    Exit Select
    
                Case ListItemType.Footer
    
                    Dim chkColumn As New CheckBox()
    
                    chkColumn.ID = "Chk" + _columnName
    
                    container.Controls.Add(chkColumn)
    
                    Exit Select
    
            End Select
    
        End Sub
    
    
    
        '''' <summary>
    
        '''' This is the event, which will be raised when the binding happens.
    
        '''' </summary>
    
        '''' <param name="sender"></param>
    
        '''' <param name="e"></param>
    
    
        Private Sub tb1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
    
    
            'Dim txtdata As TextBox = DirectCast(sender, TextBox)
            Dim chkData As CheckBox = DirectCast(sender, CheckBox)
            chkData.ID = _columnName
            'AddHandler chkData.CheckedChanged, AddressOf Chk_checkedChanged
    
            Dim container As GridViewRow = DirectCast(chkData.NamingContainer, GridViewRow)
    
            Dim dataValue As String
    
            If IsDBNull(DataBinder.Eval(container.DataItem, _columnName)) Then
    
                dataValue = ""
    
            Else
    
                dataValue = DataBinder.Eval(container.DataItem, _columnName)
    
            End If
    
            If container.RowIndex = 0 Then
                chkData.Checked = True
            End If
    
    
            chkData.Text = dataValue.ToString()
    
    
    
        End Sub
    
    
    
    
    
    
    End Class




    Tuesday, April 24, 2012 1:19 AM

Answers

  • User1866467575 posted

    You can try using the row data bound event as below and use the find control method to get your control instance

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkBox = ((CheckBox)e.Row.FindControl("checkBoxID"));
                }
            }

    Sorry for the C# code. Hope you can translate to VB.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2012 1:30 AM

All replies

  • User1866467575 posted

    You can try using the row data bound event as below and use the find control method to get your control instance

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    CheckBox chkBox = ((CheckBox)e.Row.FindControl("checkBoxID"));
                }
            }

    Sorry for the C# code. Hope you can translate to VB.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 24, 2012 1:30 AM
  • User3866881 posted

    You can try using the row data bound event as below and use the find control method to get your control instance

    protected void grid_RowDataBound(object sender, GridViewRowEventArgs e)
           
    {
               
    if (e.Row.RowType == DataControlRowType.DataRow)
               
    {
                   
    CheckBox chkBox = ((CheckBox)e.Row.FindControl("checkBoxID"));
               
    }
           
    }

    Sorry for the C# code. Hope you can translate to VB.

    My addition:)

    In fact you can use this tool to convert from C# to vb.net:http://www.developerfusion.com/tools/convert/csharp-to-vb/

    【VB.NET codes】

    Protected Sub grid_RowDataBound(sender As Object, e As GridViewRowEventArgs)
    	If e.Row.RowType = DataControlRowType.DataRow Then
    		Dim chkBox As CheckBox = DirectCast(e.Row.FindControl("checkBoxID"), CheckBox)
    	End If
    End Sub
    Wednesday, April 25, 2012 9:11 PM