Answered by:
Issue getting checkbox checked in rowdatabound event

Question
-
User-1767698477 posted
Here below is my code. My program stops on the line
If cb.Checked = True Then
saying object not set to an instance. I don't get this and I tried this as a template field as well as a boundata field. I'm trying to check the checkbox to see if it is checked or not checked. If it's checked it is replaced with an X and if not then the cell is set to a space.
<asp:BoundField DataField="propertytiho" HeaderText="propertytiho" SortExpression="propertyitho" ItemStyle-Width="125px" /> <asp:BoundField DataField="propertyrentalinc" HeaderText="propertyrentalinc" SortExpression="propertyrentalinc" ItemStyle-Width="125px" /> <asp:TemplateField ItemStyle-Width="125px" HeaderText=""> <ItemTemplate> <asp:Label ID="lblnetrent" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="propertysubject" SortExpression="propertysubject"> <ItemTemplate> <asp:CheckBox ID="propertysubject" runat="server" Checked='<%# Bind("propertysubject") %>' Enabled="false" /> </ItemTemplate> <ItemStyle Width="125px" /> </asp:TemplateField> </Columns> </asp:GridView>
Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView2.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then 'Dim value As String = e.Row.Cells(3).Text If e.Row.Cells(3).Text <> "" Then e.Row.Cells(3).Text = String.Format("{0:###,###}", Convert.ToDecimal(e.Row.Cells(3).Text)) End If Dim status As String = e.Row.Cells(4).Text If status = "1" Then e.Row.Cells(4).Text = "Sold" ElseIf status = "2" Then e.Row.Cells(4).Text = "Pending Sale" Else e.Row.Cells(4).Text = "Retained" End If If e.Row.Cells(5).Text <> "" Then e.Row.Cells(5).Text = String.Format("{0:###,###.00}", Convert.ToDecimal(e.Row.Cells(5).Text)) End If If e.Row.Cells(6).Text <> "" Then e.Row.Cells(6).Text = String.Format("{0:###,###.00}", Convert.ToDecimal(e.Row.Cells(6).Text)) End If Dim cb As CheckBox = CType(e.Row.FindControl("propertysubject"), CheckBox) If cb.Checked = True Then e.Row.Cells(8).Text = "X" End If Dim delete As ImageButton = CType(e.Row.FindControl("deletebtnre"), ImageButton) delete.OnClientClick = "return confirm('Are you sure you want to delete this item from " & e.Row.Cells(2).Text & "?')" End If End Sub
Thursday, May 20, 2021 2:32 AM
Answers
-
User-1767698477 posted
I changed my gridview template field to a boundfield which is a boolean in the database When it loads from the sqldatasource, it places a True or False in the column, and then I just change it to an X if it is true and a blank space if false. I don't know why I could not access the checkbox within the template field.
<asp:BoundField DataField="propertynetinc" HeaderText="propertynetinc" SortExpression="propertynetinc" ItemStyle-Width="125px" /> <asp:BoundField DataField="propertysubject" HeaderText="propertysubject" SortExpression="propertysubject" ItemStyle-Width="125px" />
I have removed this below and replaced it due to the null reference:
'Dim cb As CheckBox = CType(e.Row.FindControl("propertysubject"), CheckBox)
'Dim cb As CheckBox = CType(e.Row.FindControl("propertysubject"), CheckBox)
'If cb.Checked = True Then
' e.Row.Cells(8).Text = "X"
' End If
If e.Row.Cells(8).Text = "True" Then
e.Row.Cells(8).Text = "X"
Else
e.Row.Cells(8).Text = ""
End IfWere you able to access the checkbox with your code?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 22, 2021 1:19 PM
All replies
-
User-939850651 posted
Hi sking,
saying object not set to an instance. I don't get this and I tried this as a template field as well as a boundata field. I'm trying to check the checkbox to see if it is checked or not checked. If it's checked it is replaced with an X and if not then the cell is set to a space.What is the value in the propertysubject field? Boolean value?
I created a simple example based on the existing code, but failed to reproduce your problem.
<body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="GV1" AutoGenerateColumns="false"> <Columns> <asp:BoundField DataField="propertytiho" HeaderText="propertytiho" SortExpression="propertyitho" ItemStyle-Width="125px" /> <asp:BoundField DataField="propertyrentalinc" HeaderText="propertyrentalinc" SortExpression="propertyrentalinc" ItemStyle-Width="125px" /> <asp:TemplateField ItemStyle-Width="125px" HeaderText=""> <ItemTemplate> <asp:Label ID="lblnetrent" runat="server" Text=""></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="propertysubject" SortExpression="propertysubject"> <ItemTemplate> <asp:CheckBox ID="propertysubject" runat="server" Checked='<%# Bind("propertysubject") %>' Enabled="false" /> </ItemTemplate> <ItemStyle Width="125px" /> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not Page.IsPostBack Then Dim dt As DataTable = New DataTable() dt.Columns.Add("propertytiho") dt.Columns.Add("propertyrentalinc") dt.Columns.Add("propertysubject") dt.Rows.Add("propertytiho1", "propertyrentalinc1", True) dt.Rows.Add("propertytiho2", "propertyrentalinc2", False) dt.Rows.Add("propertytiho3", "propertyrentalinc3", True) dt.Rows.Add("propertytiho4", "propertyrentalinc4", False) GV1.DataSource = dt GV1.DataBind() End If End Sub Protected Sub GridView2_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GV1.RowDataBound If e.Row.RowType = DataControlRowType.DataRow Then 'Dim value As String = e.Row.Cells(3).Text 'If e.Row.Cells(3).Text <> "" Then ' e.Row.Cells(3).Text = String.Format("{0:###,###}", Convert.ToDecimal(e.Row.Cells(3).Text)) 'End If 'Dim status As String = e.Row.Cells(4).Text 'If status = "1" Then ' e.Row.Cells(4).Text = "Sold" 'ElseIf status = "2" Then ' e.Row.Cells(4).Text = "Pending Sale" 'Else ' e.Row.Cells(4).Text = "Retained" 'End If 'If e.Row.Cells(5).Text <> "" Then ' e.Row.Cells(5).Text = String.Format("{0:###,###.00}", Convert.ToDecimal(e.Row.Cells(5).Text)) 'End If 'If e.Row.Cells(6).Text <> "" Then ' e.Row.Cells(6).Text = String.Format("{0:###,###.00}", Convert.ToDecimal(e.Row.Cells(6).Text)) 'End If Dim cb As CheckBox = CType(e.Row.FindControl("propertysubject"), CheckBox) If cb.Checked = True Then e.Row.Cells(2).Text = "X" End If 'Dim delete As ImageButton = CType(e.Row.FindControl("deletebtnre"), ImageButton) 'delete.OnClientClick = "return confirm('Are you sure you want to delete this item from " & e.Row.Cells(2).Text & "?')" End If End Sub
This test can normally change the text content of lblnetrent according to the status of the checkBox.
If possible, could you provide more details?
Best regards,
Xudong Peng
Thursday, May 20, 2021 8:42 AM -
User-1767698477 posted
I changed my gridview template field to a boundfield which is a boolean in the database When it loads from the sqldatasource, it places a True or False in the column, and then I just change it to an X if it is true and a blank space if false. I don't know why I could not access the checkbox within the template field.
<asp:BoundField DataField="propertynetinc" HeaderText="propertynetinc" SortExpression="propertynetinc" ItemStyle-Width="125px" /> <asp:BoundField DataField="propertysubject" HeaderText="propertysubject" SortExpression="propertysubject" ItemStyle-Width="125px" />
I have removed this below and replaced it due to the null reference:
'Dim cb As CheckBox = CType(e.Row.FindControl("propertysubject"), CheckBox)
'Dim cb As CheckBox = CType(e.Row.FindControl("propertysubject"), CheckBox)
'If cb.Checked = True Then
' e.Row.Cells(8).Text = "X"
' End If
If e.Row.Cells(8).Text = "True" Then
e.Row.Cells(8).Text = "X"
Else
e.Row.Cells(8).Text = ""
End IfWere you able to access the checkbox with your code?
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, May 22, 2021 1:19 PM -
User-939850651 posted
Hi sking,
Were you able to access the checkbox with your code?Yes, in my example, there is no null reference exception. I can access these CheckBoxes through FindControl().
Result like this:
I used the GridView structure you provided, and I just filled in the data. Does rebuilding the project work?
Best regards,
Xudong Peng
Monday, May 24, 2021 8:23 AM