I'm using Visual Studio 2015 and SQL Server 2014, VB.
I have a Gridview where the first two columns are checkboxes. In the routine below I would like to add a means of extracting each checkbox "checked" status and adding a "X" in the first and/or second position of variable Str, whenever a checkbox has been
checked. Thus far I have not been successful; all conditions show the checkboxes as unchecked. The boxes are set in another routine using this same method and autopostback = true. Does anyone know how I can do this? Extracting text from the remaining columns
in Gridview works fine.
Dim X As Integer = 0 Dim index As Integer = 0 Dim Str As String = String.Empty Dim Head As String = String.Empty Dim builder As New StringBuilder() Dim strFileName As String = "GridViewExcel_" & ".slk" GridView1.DataBind()
For X = 0 To 5 Step 1 Head = Head & GridView1.Columns(X).HeaderText & "|" Next Head = Head & Environment.NewLine builder.Append(Head)
For Each row As GridViewRow In GridView1.Rows Dim PDFB As CheckBox = DirectCast(row.Cells(0).FindControl("PDF"), CheckBox) Dim SLDB As CheckBox = DirectCast(row.Cells(1).FindControl("SLDPRT"), CheckBox)
If PDFB.Checked = True Then PCB = "X" & Convert.ToString("|") Else PCB = Convert.ToString("|") End If
If SLDB.Checked = True Then SLD = "X" & Convert.ToString("|") Else SLD = Convert.ToString("|") End If
Dim PCBF As String = row.Cells(2).Text & Convert.ToString("|") Dim DESC As String = row.Cells(3).Text & Convert.ToString("|") Dim HGT As String = row.Cells(4).Text & Convert.ToString("|") Dim CMNT As String = row.Cells(5).Text & Convert.ToString("|") Str = PCB & SLD & PCBF & DESC & HGT & CMNT Str = Str.Replace(" ", "") builder.Append(Str & Environment.NewLine) Next Dim index2 As Integer = 1
In which event the code (that you posted) is written? Please check if grid is not being re-binded again (that ultimately making checkbox unchecked making them as initial unchecked state.
Bulldog248
Dim PDFB As CheckBox = DirectCast(row.Cells(0).FindControl("PDF"), CheckBox)
Dim SLDB As CheckBox = DirectCast(row.Cells(1).FindControl("SLDPRT"), CheckBox)
Also try finding control in row directly as:
For Each row As GridViewRow In GRIDVIEW1.Rows
Dim PDFB As CheckBox = DirectCast(row.FindControl("PDF"), CheckBox)
Dim SLDB As CheckBox = DirectCast(row.FindControl("SLDPRT"), CheckBox)
Next
Make sure IDs of the Check boxes PDF, SLDPRT are correct.
hope that helps./.
नमस्ते,
[KaushaL] BlogTwitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
188 Points
352 Posts
Problem extracting checkbox checked status from GridView
Feb 04, 2017 11:30 PM|Bulldog248|LINK
I'm using Visual Studio 2015 and SQL Server 2014, VB.
I have a Gridview where the first two columns are checkboxes. In the routine below I would like to add a means of extracting each checkbox "checked" status and adding a "X" in the first and/or second position of variable Str, whenever a checkbox has been checked. Thus far I have not been successful; all conditions show the checkboxes as unchecked. The boxes are set in another routine using this same method and autopostback = true. Does anyone know how I can do this? Extracting text from the remaining columns in Gridview works fine.
Thanks!
None
0 Points
1 Post
Re: Problem extracting checkbox checked status from GridView
Feb 05, 2017 06:41 AM|dkavithavijay|LINK
1) .aspx C# code:
Include checkbox in header template.
2) .aspx.cs code beind:
Include below methods to populate and repopulate check boxes.
Above code is in c#, may help in logic.
All-Star
31362 Points
7055 Posts
Re: Problem extracting checkbox checked status from GridView
Feb 05, 2017 08:13 AM|kaushalparik27|LINK
In which event the code (that you posted) is written? Please check if grid is not being re-binded again (that ultimately making checkbox unchecked making them as initial unchecked state.
Also try finding control in row directly as:
Make sure IDs of the Check boxes PDF, SLDPRT are correct.
hope that helps./.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
188 Points
352 Posts
Re: Problem extracting checkbox checked status from GridView
Feb 05, 2017 12:26 PM|Bulldog248|LINK
Thanks kaushalparik27,
It turns out that I did inadvertently re-bind. Once that line was removed everything worked fine.