locked
Problem extracting checkbox checked status from GridView RRS feed

  • Question

  • User615288127 posted

    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

                Response.Clear()
                Response.ContentType = "text/slk"
                Response.AddHeader("Content-Disposition", Convert.ToString("attachment;filename=" & Request.QueryString("Data") + "Cutouts and Holes" & ".slk"))
                Response.Write(builder.ToString())
                Response.[End]()

    Thanks!

    Saturday, February 4, 2017 11:30 PM

Answers

  • User-1509636757 posted

    all conditions show the checkboxes as unchecked

    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.

    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./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 5, 2017 8:13 AM

All replies

  • User664436781 posted

    1) .aspx C# code:

    Include checkbox in header template.

    <asp:TemplateField>
       <HeaderTemplate>
          <asp:CheckBox ID="checkAll" runat="server" onclick="checkAll(this);" />
       </HeaderTemplate>
       <HeaderStyle Width="25px" />
       <ItemTemplate>
          <asp:CheckBox ID="CheckBox1" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" />
       </ItemTemplate>
    </asp:TemplateField>

    2) .aspx.cs code beind:

    Include below methods to populate and repopulate check boxes.


    private void RePopulateCheckBoxes() { if (ViewState["SELECTED_INDEX"] != null) { foreach (GridViewRow row in GridView2.Rows) { var chkBox = row.FindControl("CheckBox1") as CheckBox; IDataItemContainer container = (IDataItemContainer)chkBox.NamingContainer; if (SelectedIndex != null) { if (SelectedIndex.Exists(i => i == container.DataItemIndex)) { chkBox.Checked = true; } } } } } private List<Int32> SelectedIndex { get { if (ViewState["SELECTED_INDEX"] == null) { ViewState["SELECTED_INDEX"] = new List<Int32>(); } return (List<Int32>)ViewState["SELECTED_INDEX"]; } } private void RemoveRowIndex(int index) { SelectedIndex.Remove(index); } private void PersistRowIndex(int index) { if (!SelectedIndex.Exists(i => i == index)) { SelectedIndex.Add(index); } } protected void CheckBox1_CheckedChanged(object sender, EventArgs e) { CheckBox CheckBox1 = (CheckBox)sender; int index = ((IDataItemContainer)CheckBox1.NamingContainer).DataItemIndex; if (!SelectedIndex.Exists(i => i == index)) SelectedIndex.Add(index); else SelectedIndex.Remove(index); }
    protected void GetSelectedRecords()
    {
        // row - GridView Row
        CheckBox chkRow = (row.Cells[0].FindControl("CheckBox1") as CheckBox);
        if (chkRow.Checked)
        {
           //---
        }
    }

    Above code is in c#, may help in logic.

    Sunday, February 5, 2017 6:41 AM
  • User-1509636757 posted

    all conditions show the checkboxes as unchecked

    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.

    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./.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 5, 2017 8:13 AM
  • User615288127 posted

    Thanks kaushalparik27,

    It turns out that I did inadvertently re-bind.  Once that line was removed everything worked fine.

    Sunday, February 5, 2017 12:26 PM