We help IT Professionals succeed at work.
Get Started

Using VB.net and ASP.net with dynamic checkboxes to check the checked status within a Gridview where created with a placeholder

ThePrettyGeek
on
1,775 Views
Last Modified: 2012-08-13
I have a Gridview that pulls data from MySQL and then displays it along side a column of checkboxes that are created via a ItemTemplage in the html. this works wonderfully for creating it but problem lies in checking the status of these check boxes.

I first tried just using the checkbox.checked =  true or false method but because its created dynamically the VB code doesn't see it.

I thought I had figured it out at one pont with the EnabledViewState in the VB code where it checked - if CheckBox IsNot Nothing And CheckBox.EnableViewState = True Then bla bla bla but all that was checking was to see if view state was enabled and it was which would then select all of the items and run the code under the if statment up until the checkbox was ticked. so it was updating all sql entries above the correct one as well.


What I need it to check is still If CheckBox IsNot Nothing And CheckBox.Checked = True Then
but it constantly returns false.

I thought I had found somewhat of an answer with
http://stackoverflow.com/questions/1975548/reading-checked-property-of-dynamic-checkbox-on-page-postback
and other than nameing things a bit differently it may as well have been a direct copy and paste of the code but this too failed. I tried using the CheckChanged and the OnCheckChaned properties but VS didnt recognize them as valid properties of the command,

I thouht there may be someway to use the attributes property and played around with variations of CheckBox.Attributes(ViewState(Checked)) and Im still not convinced Im 100% wrong with going this direction, I just couldnt figure out the exact wording on it.

In any event, Im back here begging for help
' VB.NET Code
Dim tr As TableRow
Dim RowCounter As Integer = 0

For Each tr In GridViewID.Rows
  Dim cb As CheckBox = GridViewID.Rows(RowCounter).Cells(0).FindControl("CheckBoxID")
  If cb IsNot Nothing And cb.Checked = True Then
    'Code to pull data from GridViewID and compare to DB
    'Code to Update MySQL Database
    RowCounter = +1
  End If
next




<!--ASP / HTML CODE-->
                <asp:GridView ID="GridViewID" runat="server">
                    <Columns>
                       <asp:TemplateField HeaderText="CheckBox Column">
                        <ItemTemplate>                       
                         <asp:CheckBox runat="server" ID="CheckBoxID" Checked="false" EnableViewState="true" />                                            
                        </ItemTemplate>
                       </asp:TemplateField>
                    </Columns>
                </asp:GridView>

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 1 Answer and 19 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE