473,320 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Unable to selected checkbox

I am unable to update table when the users clicks on the register button. My code is listed below.

vb:
Expand|Select|Wrap|Line Numbers
  1. Dim retstruct As RegisterCourseInfo
  2.         Dim uN As String
  3.  
  4.  
  5.         For Each row As GridViewRow In GridView1.Rows
  6.             Dim cb As CheckBox = CType(GridView1.FindControl("ChkSelect"), CheckBox)
  7.  
  8.             If cb IsNot Nothing AndAlso cb.Checked Then
  9.  
  10.                 uN = GridView1.DataKeys(row.RowIndex).Value
  11.                 retstruct = RegisterCourseIndividual(Request.QueryString("CourseID"), Request.QueryString("SessionID"), uN, False)
  12.                 cb.Checked = False
  13.  
  14.             End If
  15.         Next
Expand|Select|Wrap|Line Numbers
  1. <asp:TemplateField InsertVisible="False">
  2.             <ItemTemplate>
  3.                 <asp:CheckBox ID="ChkSelector" ViewStateMode="disabled" Checked="false" runat="server" />
  4.             </ItemTemplate>
  5.         </asp:TemplateField>
Would anyone have any ideas of why I am not getting the correct results?

Thank you in advance for looking.
Dec 15 '11 #1

✓ answered by Frinavale

A guess, off the top of my head, would be that you are calling the DataBind method on the GridView in the Page Load event.

If you do this, anything that the user provided will be lost because the GridView re-binds to the original data.

You should hold off calling the DataBind method until the PreRender event since it occurs after all of the other page events are done.

-Frinny

5 2167
Frinavale
9,735 Expert Mod 8TB
There are 2 reasons why you aren't retrieving the CheckBox.
First of all, the CheckBox exists each Row in the the GridView.
So, the following:
Expand|Select|Wrap|Line Numbers
  1.  Dim cb As CheckBox = CType(GridView1.FindControl("ChkSelect"), CheckBox)
  2.  
Should be:
Expand|Select|Wrap|Line Numbers
  1.  Dim cb As CheckBox = CType(row.FindControl("ChkSelect"), CheckBox)
The second problem is that you don't have a CheckBox with the ID "ChkSelect" in your row....you have a CheckBox with the ID "ChkSelector".

So, the following will fix the problem:
Expand|Select|Wrap|Line Numbers
  1.  Dim cb As CheckBox = CType(row.FindControl("ChkSelector"), CheckBox)

Here's the example that I used to debug your problem.
(ASP.NET code)
Expand|Select|Wrap|Line Numbers
  1. <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication1.WebForm1" %>
  2.  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6.     <title></title>
  7. </head>
  8. <body>
  9.     <form id="form1" runat="server">
  10.     <asp:GridView ID="GridView1" runat="server">
  11.         <Columns>
  12.             <asp:TemplateField InsertVisible="False">
  13.                 <ItemTemplate>
  14.                     <asp:CheckBox ID="ChkSelector" ViewStateMode="disabled" Checked="false" runat="server" />
  15.                 </ItemTemplate>
  16.             </asp:TemplateField>
  17.         </Columns>
  18.     </asp:GridView>
  19.     <asp:Button runat="server" ID="showCheckedRows" Text="show checked rows" />
  20.     <br />
  21.     <asp:Label runat="server" ID="checkedRows" />
  22.     </form>
  23. </body>
  24. </html>
(VB.NET code)
Expand|Select|Wrap|Line Numbers
  1. Public Class WebForm1
  2.     Inherits System.Web.UI.Page
  3.     Private _dt As DataTable
  4.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  5.         If Session("_dt") Is Nothing Then
  6.             RetrieveDataSource()
  7.         Else
  8.             _dt = DirectCast(Session("_dt"), DataTable)
  9.         End If
  10.     End Sub
  11.     Private Sub WebForm1_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
  12.         GridView1.DataSource = _dt
  13.         GridView1.DataBind()
  14.     End Sub
  15.  
  16.     Private Sub showCheckedRows_Click(sender As Object, e As System.EventArgs) Handles showCheckedRows.Click
  17.         Dim selectedRowText As New StringBuilder
  18.         Dim index As Integer = 0
  19.         For Each row As GridViewRow In GridView1.Rows
  20.             Dim cb As CheckBox = CType(row.FindControl("ChkSelector"), CheckBox)
  21.             If cb IsNot Nothing AndAlso cb.Checked Then
  22.                 selectedRowText.Append("row: ")
  23.                 selectedRowText.Append(index.ToString)
  24.                 selectedRowText.Append("<br />")
  25.             End If
  26.             index += 1
  27.         Next
  28.         checkedRows.Text = selectedRowText.ToString
  29.     End Sub
  30.  
  31.     Private Sub RetrieveDataSource()
  32.         _dt = New DataTable
  33.         _dt.Columns.Add("ID", GetType(Integer))
  34.         _dt.Columns.Add("Name")
  35.  
  36.         For i As Integer = 1 To 10
  37.             Dim dr As DataRow = _dt.NewRow
  38.             dr("ID") = i
  39.             dr("Name") = "Name " + i.ToString
  40.             _dt.Rows.Add(dr)
  41.         Next
  42.         Session("_dt") = _dt
  43.     End Sub
  44. End Class
Dec 16 '11 #2
Thank you for your help. I pasted the incorrect code after looking at your response and I had the correct findcontrol name. I have entered what you have supplied and when I run the page it bypasses the following lines:
Expand|Select|Wrap|Line Numbers
  1.  If cb IsNot Nothing AndAlso cb.Checked Then
  2.    selectedRowText.Append("row: ")
  3.    selectedRowText.Append(index.ToString)
  4.    selectedRowText.Append("<br />")
  5.  End If
Would you happen to know what may cause the code to bypass the if statement?

Thank you again for your advice.
Dec 16 '11 #3
Frinavale
9,735 Expert Mod 8TB
It will bypass that case if cb is nothing or it's not checked.

I recommend adding a new WebForm1.aspx to your project.
Copy the code that I have for the ASP part and paste it into the form. Then copy the VB.NET code into the code behind for the page.

I've tested it and it works fine.
Dec 16 '11 #4
Frinavale
9,735 Expert Mod 8TB
A guess, off the top of my head, would be that you are calling the DataBind method on the GridView in the Page Load event.

If you do this, anything that the user provided will be lost because the GridView re-binds to the original data.

You should hold off calling the DataBind method until the PreRender event since it occurs after all of the other page events are done.

-Frinny
Dec 16 '11 #5
Thank you for your help. I just figured it out. Once I put the checkbox if loop into a if Page.ispostback everything went through and updated as needed. You pointed me in the right direction and I really glad that there are people out there that help us that need it. Thanks again.
Dec 16 '11 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Francois Verbeeck | last post by:
Dear UseNet readers, Does anyone have any idea on how to colorize selected checkbox in checkboxlist control ? I've quite a huge checkboxlist (approximatively one full screen) and, to improve...
8
by: Mike | last post by:
Hi all, I have a DataGridViewCheckBoxColumn as one of my columns in a DataGridView. I want this checkbox to only be checked, but not unchecked - it's used by the user to audit that they have...
4
by: M | last post by:
Hello, I would like to get a value for a non selected checkbox in a form. Imagine my form contains : <input type='checkbox' name='t' value='1'> <input type='checkbox' name='t' value='1'> ...
1
by: snsit1 | last post by:
Hello - i am wondering if anyone can help, I am fairly new to javascript / html and am having difficulty getting selected options ticked using check boxes out to a .txt file and have another script...
10
by: LionsDome | last post by:
Hello, I have a vb.net page which a bunch of checkboxes. A user can select a checkbox(s) and hit the submit button to store those values in a SQL Server table. This works fine with no problem...
0
by: pankajprakash | last post by:
Hi, I have a treeview control which have checkbox control. I just need to fetch the selected checkbox value (id of selected node of tree view). I have used following code to get the no. of...
3
by: palanidharma | last post by:
hi , i am new to php.just i am writing checkbox coding. how to store the selected check box value in Mysql. $sql=mysql_query("select * from $ttablename") or die(mysql_error()); ...
2
by: palanidharma | last post by:
hi, i writing the code for selectd check box value deleted.but the code not working <?php include("conn.php"); $sel= mysql_select_db("sangoma"); //include("function.php"); ?>
2
by: Ananthu | last post by:
Hi, I have a gridview with paging option. I want to maintain the state of the checkbox column in gridview for each page. I used vb.net coding. I used the following link for this purpose, ...
1
by: vikaspa | last post by:
Hi In php count($_POST) gives me no of products selected by user i.e.no of check boxes checked How to get products for which the check box is not checked ?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.