[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Multiple Checkboxes in an ASP VB.Net page.

Posted on 2010-09-22
16
Medium Priority
?
503 Views
Last Modified: 2012-05-10
i have a database with several tables, one of them is called documents. its columns are "docId", "doc_name", "doc_owner", "doc_permission".

I want to have a list of all records of the 'documents' table (20 documents at this time) with a checkbox button at the left of every item with all its information ("docId", "doc_name", "doc_owner", "doc_permission"). so i can then take every id of a selected checkbox and do whatever i want it.

I tried using the checkboxlist control by itself, but it only displays 1 column with one record.

maybe there is a way to combine checkboxlist with another control and this may work, dont know.

i would really appreciatte your answers.


btw, i am using Visual WebDeveloper 2010 Express (ASP.NEt 4.0 and VB .Net 4.0)
0
Comment
Question by:José Perez
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 41

Expert Comment

by:guru_sami
ID: 33739050
1: Add a Checkbox as TemplateField:
 <asp:TemplateField>
           <ItemTemplate>
                   <asp:CheckBox ID="CheckBox1" runat="server" />            
        </ItemTemplate>
</asp:TemplateField>

2. Loop through gridview rows, find the checkbox in each row and see if it checked or not...
For Each row As GridViewRow In GridView1.Rows
      Dim chb As CheckBox = DirectCast(row.FindControl("CheckBox1"), CheckBox)
      If chb IsNot Nothing AndAlso chb.Checked = True Then
           ' do something
      End If
Next

3: If you want to get docId, you will have to use DataKeyNames and DataKeys for the row.
0
 
LVL 2

Author Comment

by:José Perez
ID: 33743289
where, in my code, do i have to put the templateField?
this is my gridview code:
                <asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
                    AutoGenerateColumns="False" DataSourceID="AccessDataSource1">
                    <Columns>
                        <asp:TemplateField HeaderText="nom_doc" SortExpression="nom_doc">
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Bind("nom_doc") %>'></asp:Label>
                            </ItemTemplate>
                            <EditItemTemplate>
                                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("nom_doc") %>'></asp:TextBox>
                            </EditItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="dueño" HeaderText="dueño" SortExpression="dueño" />
                        <asp:BoundField DataField="ext" HeaderText="ext" SortExpression="ext" />
                        <asp:BoundField DataField="tipo" HeaderText="tipo" SortExpression="tipo" />
                    </Columns>
                </asp:GridView>

Open in new window

0
 
LVL 2

Author Comment

by:José Perez
ID: 33743501
ok, i have added the checkbox as a templateField. it is working but it is displaying the id number to side of the checkbox. i would like not to be displayed but i am affraid tha if i delete de "Text" command i wont have a way to "know" what is the id corresponding to that row.

here is the code i have now:
               <Columns>
                        <asp:TemplateField HeaderText="ID" InsertVisible="False" SortExpression="ID">
                            <ItemTemplate>
                                 <asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Bind("ID") %>'></asp:CheckBox>
                            </ItemTemplate>

                        </asp:TemplateField>
                        <asp:BoundField DataField="nom_doc" HeaderText="nom_doc" 
                            SortExpression="nom_doc" />
                        <asp:BoundField DataField="ext" HeaderText="ext" SortExpression="ext" />
                        <asp:BoundField DataField="dueño" HeaderText="dueño" SortExpression="dueño" />
                    </Columns>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 41

Expert Comment

by:guru_sami
ID: 33744873
-->but i am affraid tha if i delete de "Text" command i wont have a way to "know" what is the id corresponding to that row.
The answer to that is my 3rd point.

1: Set the property of GV:  DataKeyNames="ID"
2: To retrieve in my step2 code:

 If chb IsNot Nothing AndAlso chb.Checked = True Then
           ' do something
           Dim id As String = GridView1.DataKeys(row.RowIndex).Value.ToString()
      End If

So you can remove the Text property of Checkbox.

Ref: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.datakeys.aspx
0
 
LVL 2

Author Comment

by:José Perez
ID: 33783527
in what 'event' should i add the following code?
If chb IsNot Nothing AndAlso chb.Checked = True Then
           ' do something
           Dim id As String = GridView1.DataKeys(row.RowIndex).Value.ToString()
End If

Open in new window

0
 
LVL 2

Author Comment

by:José Perez
ID: 33783561
i added to gridview1_SelectedIndexChange but it does not recognize 'chb'
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33784642
--->in what 'event' should i add the following code?
I understood you wanted to loop through checkboxes in all gridview rows and that's what my initial code does. Loop through each rows and that's where that new code was supposed to go.
And you execute that code upon some button click.

--->i added to gridview1_SelectedIndexChange but it does not recognize 'chb'
to use that code in gridview selectedIndexChanged event you need like this:

  Dim chb As CheckBox = DirectCast(gridview1.SelectedRow.FindControl("CheckBox1"), CheckBox)
      If chb IsNot Nothing AndAlso chb.Checked = True Then
           ' do something
           Dim id As String = GridView1.DataKeys(gridview1.SelectedRowIndex).Value.ToString()
      End If
0
 
LVL 2

Author Comment

by:José Perez
ID: 33790459
now it says "SelectedRowIndex is not member of the System.Web.UI.WebControls.Gridview".
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33790872
sorry my bad... it should have been SelectedIndex  i.e. gridview1.SelectedIndex
0
 
LVL 2

Author Comment

by:José Perez
ID: 33792259
Error in
"Dim chb As CheckBox = DirectCast(GridView1.SelectedRow.FindControl("CheckBox1"), CheckBox)"

it says "Object reference not set to an instance of an object."
and it still does not recognize 'chb'
0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33793224
I will have to see you complete code because I am giving you the basic idea and you will have adjust values accordingly.
0
 
LVL 2

Author Comment

by:José Perez
ID: 33793329

Partial Class Principal
    Inherits System.Web.UI.Page

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged


    End Sub

    Protected Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.Click

        Dim chb As CheckBox = DirectCast(GridView1.SelectedRow.FindControl("CheckBox1"), CheckBox)

        If chb IsNot Nothing AndAlso chb.Checked = True Then
            ' do something
            MsgBox("Seleccionado!")
            Dim id As String = GridView1.DataKeys(GridView1.SelectedIndex).Value.ToString()
        End If

    End Sub
End Class

Open in new window

0
 
LVL 41

Expert Comment

by:guru_sami
ID: 33793464
Yeah see, you should be placing that code inside SelectedIndexChanged event.

If above doesn't want you might want to also provide me more exact details of your issue.
Because I am still not clear what exactly is your issue as now you are trying to execute that thing in Button Click. So do you want to get it when user click's Select button in the GridView row or you want to loop through all rows.
0
 
LVL 2

Author Comment

by:José Perez
ID: 33793733
ok, well i'll explain...

what i want is that when someone selects a couple of checkboxes he or she clicks the button so the button generates an email with the 'id' of the selected checkboxes.

so the code should be executed whe the user clicks the button.

sorry if this wasn't clear.
0
 
LVL 41

Accepted Solution

by:
guru_sami earned 800 total points
ID: 33793844
We were on right track but things got diverted from the point where SelectedIndexChanged came into picture. Anyways, if you want to do that on a Buttonclick outside of GV.


rotected Sub Button7_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button7.Click

Dim AllIDs As New List(Of String)()


For Each row As GridViewRow In GridView1.Rows
      Dim chb As CheckBox = DirectCast(row.FindControl("CheckBox1"), CheckBox)
      If chb IsNot Nothing AndAlso chb.Checked = True Then
           ' do something
            MsgBox("Seleccionado!")
            Dim id As String = GridView1.DataKeys(row.RowIndex).Value.ToString()
            AllIDs.Add(id)
      End If
Next
 End Sub

Note:
1: AllIDs will contain all the IDs for rows for which checkbox is clicked.
2: Make sure you have Set the property of GV:  DataKeyNames="ID"
3: Set breakpoints in your button click event and observe the execution flow. You might want to share that if something doesn't work. Mainly not that the execution hits this line:  AllIDs.Add(id)

0
 
LVL 2

Author Closing Comment

by:José Perez
ID: 33798798
thanks a lot, this really helped me!
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question