I am attempting to figure out how to use a datagrid with checkboxes. I found some information for using an item template and the FindControl method. I am using Dreamweaver to program so I'm sure I'm not getting all the bells and whistles that VB.NET has to offer. I'm stumped because my code looks like so much that I have found on the web. I am getting this message when I click the submit button:
System.NullReferenceException: Object reference not set to an instance of an object.

Any advice will be unbelievably appreciated. ( put a space after the colon between ASP and DATAGRID to take out the face)

Thank you

Here's my script code:

<script language="vb" runat="server">
sub check_clicked(byVal sender as object, byVal e as eventArgs)
    Dim dgItem as DataGridItem
    Dim strSelected as String
    Dim ckSelected as System.Web.UI.WebControls.CheckBox
		
    For Each dgItem in dgsvcPacks.Items
        ckSelected = dgsvcPacks.FindControl("ckUpdate")
           if ckSelected.Checked then
             strSelected = CType(dgsvcPacks.FindControl("name"),Label).Text
             strSelected = strselected & "<br>"
           end if
    next
    label1.Text = strSelected
end sub
</script>

Here's my datagrid code:

<form runat="server">
<asp: DataGrid id="dgsvcPacks" runat="server"
	AutoGenerateColumns="false"
    Font-Name="Vernada" Width="75%"
    HorizontalAlign="Center">
    
    <headerstyle BackColor="#21609b" ForeColor="#FFFFFF" Font-Bold="true"/>
    <columns>
    	<asp:BoundColumn DataField="name" HeaderText="Module"/>
        <asp:BoundColumn DataField="update" HeaderText="Update No" />
        <asp:BoundColumn DataField="file" HeaderText="Update File Name"/>
        <asp:TemplateColumn HeaderText="ckbox">
            <ItemTemplate>
                <asp:CheckBox ID="ckUpdate" runat="server"/>
            </ItemTemplate>
        </asp:TemplateColumn>
    </columns>
    
</asp: DataGrid>
<asp:Button OnClick="check_clicked" Text="Submit" runat="server"/>
<asp:Label ID="label1" runat="server" />
</form>

hi,

try like this

For Each dgItem in dgsvcPacks.Items
ckSelected = dgItem.FindControl("ckUpdate") as checkbox
if ckSelected.Checked then
strSelected = CType(dgItem.FindControl("name"),Label).Text
strSelected = strselected & "<br>"
end if

Thanks.. you helped me realize I goofed and was doing the grid instead of the item. I'm almost there. I just realized I need templateitems for the rest of the columns as there is no "ID" field for the boundcolumn.

The whole thing works now. YIPPEE!
Thank you so much!

hi,

If you solved your issue,i suggest to mark this thread as solved as it saves time for fellow posters to concentrate on other posts.

In general set the @Page attribute trace="true" to locate the control. Then you'll easily figure out how to "FilndControl()" it. Read MSDN on FindControl() also. This should be enough!

comment out Dim ckSelected as System.Web.UI.WebControls.CheckBox and within your loop

For Each dgItem As GridViewRow in dgsvcPacks.Rows
  Dim cb As CheckBox = row.FindControl("ckUpdate")

Make that:

comment out Dim ckSelected as System.Web.UI.WebControls.CheckBox and within your loop

For Each dgItem As GridViewRow in dgsvcPacks.Rows
  Dim cb As CheckBox = dgItem .FindControl("ckUpdate")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.