how to bind checkbox in a gridview

W

waldy

Hi,

I am new to gridview and asp.net and would like to know how to bind a
check box in a gridview.

Basically, I have a gridview where each row in the gridview has several
columns and the first column is a checkbox. When user check the
checkbox, I would like to be able to get the specific row information
where the check box is being checked. However, I couldn't figure out
how to do that. Any helps will be appreciated. Should I do it in code
behind ? Any code snippet on how to do this will greatly appreciated.
Below is gridview:

<asp:GridView ID="ContactGridView" runat="server"
AutoGenerateColumns="False" DataKeyNames="Id" Width="100%"
AllowPaging="True" AllowSorting="True"
OnPageIndexChanging="ContactListPageHandler" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox runat="server"
ID="ContactCheckBox" EnableViewState="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact name">
<ItemStyle HorizontalAlign="Center"
CssClass="contentsmall" />
<HeaderStyle HorizontalAlign="Center"
CssClass="contentsmall" />
<ItemTemplate>
<asp:Label ID="ContactNameLabel"
runat="server" Text='<%# Eval("Contact.FullName")%>'
CssClass="contentsmall" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Contact ID">
<ItemStyle HorizontalAlign="Center"
CssClass="contentsmall" />
<HeaderStyle HorizontalAlign="Center"
CssClass="contentsmall"/>
<ItemTemplate>
<asp:Label ID="ContactIDLabel"
runat="server" Text='<%# Eval("Code")%>' CssClass="contentsmall" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Company name">
<ItemStyle HorizontalAlign="Center"
CssClass="contentsmall" />
<HeaderStyle HorizontalAlign="Center"
CssClass="contentsmall"/>
<ItemTemplate>
<asp:Label ID="CompanyNameLabel"
runat="server" Text='<%# Eval("Contact.CompanyName")%>'
CssClass="contentsmall" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Address">
<ItemStyle HorizontalAlign="Center"
CssClass="contentsmall" />
<HeaderStyle HorizontalAlign="Center"
CssClass="contentsmall"/>
<ItemTemplate>
<asp:Label ID="Address1Label"
runat="server" Text='<%# Eval("Contact.ContactAddress.Line1")%>'
CssClass="contentsmall" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City">
<ItemStyle HorizontalAlign="Center"
CssClass="contentsmall" />
<HeaderStyle HorizontalAlign="Center"
CssClass="contentsmall" />
<ItemTemplate>
<asp:Label ID="CityLabel"
runat="server" Text='<%# Eval("Contact.ContactAddress.City")%>'
CssClass="contentsmall" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E6E6E6" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="DarkGray"
Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#660099" ForeColor="White"
HorizontalAlign="Center" Font-Bold="True" />
<HeaderStyle BackColor="Gray" Font-Bold="True"
ForeColor="White" HorizontalAlign="Center" />
<FooterStyle BackColor="Gray" Font-Bold="True"
ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<PagerSettings Mode="NextPreviousFirstLast"
Position="Top" />
</asp:GridView>

Thanks
Waldy
 
T

Tasos Vogiatzoglou

for (int i=0;i<ContactListGridView.Items.Count;i++){
bool checked =
ContactListGridView.Items.FindControl("ContactCheckBox").Checked;
}

I think this should do the trick.

Regards,
Tasos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top