David Phillips (fuzzygoth) wrote in aspdotnet,
David Phillips

  • Location:
  • Mood:

C# CheckBoxes!

Okay, I am writing a small webapp in asp.net C# 2.0 after a few years
since I last done .net (which was vb.net by the way). I have a GridView
which is filtered by a dropdown, the gridview is listed below. I can't
remember for the life of me how to catch the checkboxes posted back from
the form? and I can't find any simple examples online. Help?
  • Post a new comment


    default userpic
there are a few ways to do this.

First of all, I'm assuming the checkboxes are a part of the grid. If so:

1) catch the rowevent that fires when the check event is happening.
2) put the checkboxes into a template field and make them autopostback. Create a function that hooks to this event and just be sure to identify which checkbox is firing and put the data there.
3) shut off autopostback on the checkboxes using the method above, put a button at the bottom of the grid that is something like "Submit Changes" and then cycle through each of the rows in the grid and make changes.

Good luck!
I am using a GridView with asp:TemplateField holding a checkbox. I tried CheckboxList and CheckBoxField but neither did what I wanted.

I don't want to do it with autopostback as I want to do multiple selects. Do I just use request.form(SelectPage); to get the values
or like in asp/php do I need to asign a unique value to the ID, I
seem to remember that in .net you don't have to but I lost all my
old vb.net 1.1 code and can't remember what to use to call it out.

[asp:SqlDataSource ID="getPages" ConnectionString="[%$ ConnectionStrings:IATool_Conn %]" ProviderName="System.Data.SqlClient" runat="server" /]
[asp:GridView ID="pagesID" DataSourceID="getPages" width="60%" BorderWidth="1px" BorderColor="#000000" CssClass="PagesGrid" runat="server" AutoGenerateColumns="false" AllowPaging="false" CellPadding="4" GridLines="None"]
[asp:CheckBox ID="SelectPage" runat="server" /]
[asp:boundfield datafield="PageID" headertext="PageID" /]
[asp:boundfield datafield="Section" headertext="Top Level Section" /]
[asp:HyperLinkField DataNavigateUrlFields="ExistingURL" DataTextField="ExistingURL" Target="_blank" /]
[asp:Button ID="AddButton" OnClick="AddButton_Click" Text="Add Records" TabIndex="10" runat="server" /]
In the "AddButton_Click" function call be sure to do all of the work.

You should be able to then do something like this (Forgive my VB.Net Code, as it is what I have handy)
Dim checkedIDs as string = ""
Dim gridCheckbox as Checkbox = nothing
For each row as GridViewRow in pageID.Rows
if row isnot nothing
gridCheckbox = CType(row.FindControl("SelectPage"), checkbox)
if gridChekbox.checked
checkedIDs += row(1).ToString 'Gives Page ID I believe
end if
end if

I believe this may be what you need. Right?
no problem, thanks for your help mate :)

Deleted comment

that was rather usefull actually, cheers :)