473,549 Members | 5,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Unable to selected checkbox

3 New Member
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
5 2176
Frinavale
9,735 Recognized Expert Moderator Expert
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 "ChkSelecto r".

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
Wally McKean
3 New Member
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 Recognized Expert Moderator Expert
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 Recognized Expert Moderator Expert
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
Wally McKean
3 New Member
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
3039
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 user's experience, i wanted to highlight selected choices. Does anyone know how to that ? I can successfully do that on a datagrid by adding some...
8
9222
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 "seen" and checked an order. In VB6 I would handle the Mouse Button Down event, test the target cell and set the mouse event arguements to nothing....
4
2383
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'> When I analyse $t, if first checkbox is not selected and second is, I have :
1
2620
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 called once the output is placed in to the .txt file. What i am attempting to do is have the user select the options for switches they wish to perform...
10
4420
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 the first time the user submits. However when user submits a second time while changing some of the selected boxes the page only re-submits the...
0
2234
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 selected checkbox of tree view control.... function UnCheckAllCheckBoxes() { //debugger
3
2517
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()); while($row=mysql_fetch_row($sql)) { echo"<tr> <td><input type='checkbox' name='arr'></td>
2
1957
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
8624
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, http://aspalliance.com/774_Maintaining_State_of_CheckBoxes_While_Paging_in_a_GridView_Control
1
1641
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
7723
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7962
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7480
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
769
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.