Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datagrid and checkbox

Status
Not open for further replies.

orangelight

Technical User
Feb 17, 2005
20
GB
Hi i got this script to try and read a checkbo with a data grid. but i am getting this error
<b>System.NullReferenceException: Object reference not set to an instance of an object.
</b>
on this line
If giftCheckBox.Checked Then

here is my script

Sub pk_updateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Dim i As Integer
Dim _item As DataGridItem
Dim dr As DataRow

For i = 0 To dtrEntityList.Items.Count - 1

_item = dtrEntityList.Items(i)
Dim giftCheckBox As CheckBox = _item.FindControl("chkGIft")

If giftCheckBox.Checked Then

Response.Write(giftCheckBox)
End If
Next
vb_contentDetails()

End Sub


<asp:DataGrid id="dtrEntityList" runat="server" AutoGenerateColumns="False" DataKeyField="content_no_id"
OnEditCommand="hfm_editCommand" onCancelCommand="hfm_CancelCommand" OnUpdateCommand="pk_updateCommand" Width=520px>

<Columns>
<asp:BoundColumn DataField="content_no_id" HeaderText="ID" ReadOnly="true"></asp:BoundColumn>

<asp:TemplateColumn>
<ItemTemplate>
<%#Container.DataItem("content_name")%>
</ItemTemplate>
<EditItemTemplate>
<asp:Textbox id="txtEntityname" text='<%# container.DataItem("content_name") %>' runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn>
<ItemTemplate>
<%# subChge_active(DataBinder.Eval(Container.DataItem, "content_active"))%>
</ItemTemplate>
<EditItemTemplate>
<!-- <asp:Textbox id="txtEntitynumber" text=' <%# subChge_active(DataBinder.Eval(Container.DataItem, "content_active"))%>' runat="server" />/-->
<asp:CheckBox id="chkGIft" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "content_active") %>' />

</EditItemTemplate>
</asp:TemplateColumn>

<asp:EditCommandColumn EditText="Edit"
UpdateText="[Update]" CancelText="[Cancel]" ItemStyle-Width="120px" />
</Columns>
</asp:DataGrid>

can anyone help
thanks in advance
Hesh
 
Code:
Sub pk_updateCommand(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
   Dim i As Integer
   Dim _item As DataGridItem
   Dim dr As DataRow

   For i = 0 To dtrEntityList.Items.Count - 1
      _item = dtrEntityList.Items(i)
      Dim giftCheckBox As CheckBox = CType(_item.FindControl("chkGIft"), CheckBox)

      If not giftCheckBox is nothing then
         If giftCheckBox.Checked Then 
            Response.Write(giftCheckBox)
         End If
      end if
   Next
   vb_contentDetails()
End Sub
my vb is rusty, but it should look something like this.
1st you must cast the found control to a checkbox
2nd to avoid the error check if the checkbox is null/nothing

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top