Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 24th, 2009, 04:48 PM
Registered User
Points: 36, Level: 1
Points: 36, Level: 1 Points: 36, Level: 1 Points: 36, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Question Adding Gridview Column of Checkboxes and Retrieving Values

http://www.asp.net/learn/data-access...ial-52-vb.aspx

I am trying to use the tutorial provided but I am having trouble tweaking this for my application:

I need to be able to use checkboxes on a Gridview control to pull information from the first column of the Gridview and populate a Label control.

So basically I should be able to click multiple checkboxes, hit a button control and the data binded to each checkbox to column should appear where I can collect and do other things with.

My Gridview allows Editing and Sorting:

This is what i have so far in my ASPX page: (the Barcode field is the data i need to pull)

Code:
 
<asp:GridViewID="GridView1"runat="server"AllowPaging="True"
AllowSorting="True"AutoGenerateColumns="False"DataKeyNames="Barcode"DataSourceID="SqlDataSource1"Font-Size="Small"PageSize="20">
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBoxID="CheckBox1"runat="server"/>
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBoxID="CheckBox1"runat="server"AutoPostBack="True"Checked='<%# Bind("Barcode") %>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandFieldShowEditButton="True"/>
<asp:BoundFieldDataField="Barcode"HeaderText="Barcode"ReadOnly="True"SortExpression="Barcode"/>
<asp:BoundFieldDataField="Media_Type"HeaderText="Media_Type"SortExpression="Media_Type"/>
<asp:BoundFieldDataField="Make"HeaderText="Make"SortExpression="Make"/>
<asp:BoundFieldDataField="Backup_Server"HeaderText="Backup_Server"SortExpression="Backup_Server"/>
<asp:BoundFieldDataField="Media_Set"HeaderText="Media_Set"SortExpression="Media_Set"/>
<asp:BoundFieldDataField="Backup_Date"HeaderText="Backup_Date"SortExpression="Backup_Date"/>
<asp:BoundFieldDataField="Location"HeaderText="Location"SortExpression="Location"/>
<asp:BoundFieldDataField="Archive"HeaderText="Archive"SortExpression="Archive"/>
<asp:BoundFieldDataField="Container"HeaderText="Container"SortExpression="Container"/>
<asp:BoundFieldDataField="Notes"HeaderText="Notes"SortExpression="Notes"/>
</Columns>
</asp:GridView>
Thanks for your help.
  #2 (permalink)  
Old April 24th, 2009, 06:27 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

It is not clear at all what you are trying to do here.

Is Barcode a Boolean value? Because that's the way you have it set up.

Also, you'd normally use Bind in the EditTemplate, and Eval in the ItemTemplate.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #3 (permalink)  
Old April 24th, 2009, 07:50 PM
Registered User
Points: 36, Level: 1
Points: 36, Level: 1 Points: 36, Level: 1 Points: 36, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding Gridview Column of Checkboxes and Retrieving Values

Hi Lee,

Barcode is the name of my first column where I have data. This data is not boolean but is alphanumeric.

The checkbox is probably boolean as it can only be either checked, or unchecked.

I need to construct some VB code in my page behind which will do the following:

If the checkbox (or checkboxes) are checked then retrieve row value under the barcode column and paste these values to a label control.

I hope im making sense. I found some more info on how this can be done at: http://aspnetteam.wordpress.com/2008/10/20/dynamic-checkboxlist-control-nested-within-a-gridview-control/

Im going to try put some of this code together and let you know what I have.
  #4 (permalink)  
Old April 24th, 2009, 08:44 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Quote:
Originally Posted by nelper703 View Post
Hi Lee,

Barcode is the name of my first column where I have data. This data is not boolean but is alphanumeric.
In that case, then what is this for?

Code:
<asp:CheckBoxID="CheckBox1"runat="server"AutoPostBack="True"Checked='<%# Bind("Barcode") %>'/>
The Checked property can only be true or false, so binding this to a non-Boolean value doesn't make sense.
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #5 (permalink)  
Old April 25th, 2009, 05:48 PM
Registered User
Points: 36, Level: 1
Points: 36, Level: 1 Points: 36, Level: 1 Points: 36, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Question Adding Gridview COlumn of Checkboxes and Retrieving Values to a Textbox Control

Thanks for pointing that out, I have just fixed that and also added the following VB to my code behind page:

This code basically sets up an array, then goes through each row checking if the checkbox is checked off.. If the checkbox is checked off then it pulls the value from the Barcode cell.

The array then prints to a textbox control labeled textbox1.

My problem is that only 1 value is getting printed out the textbox control and not all the rows that I have selected by checking off the checkbox...

What am I missing here?

Code:
 
Dim arr AsNew ArrayList()
For i AsInteger = 0 To GridView1.Rows.Count - 1
Dim cb As CheckBox = DirectCast(GridView1.Rows(i).Cells(0).FindControl("CheckBox1"), CheckBox)
If cb IsNotNothingThen
If cb.Checked Then
'get the value of BarCode here
Dim barCode AsString = GridView1.Rows(i).Cells(2).Text
' contains the column for BarCode
'add the value to ArrayList
arr.Add(barCode)
'TextBox1.Text = barCode.ToString()
Dim j AsInteger
For j = 0 To arr.Count - 1
Console.WriteLine(CStr(arr.Item(j)))
Dim output AsString
output = (CStr(arr.Item(j)))
TextBox1.Text = output
 
Next
 
EndIf
EndIf
 
Next
 
EndSub
Thanks for your help guys!
  #6 (permalink)  
Old April 26th, 2009, 09:44 PM
Registered User
Points: 36, Level: 1
Points: 36, Level: 1 Points: 36, Level: 1 Points: 36, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2008
Location: , , .
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Smile Adding Gridview COlumn of Checkboxes and Retrieving Values to a Textbox Control

Solution Found!

After researching some more I found a great example which I was able to easily modify to serve my purpose: http://vijaymodi.wordpress.com/2007/...-using-aspnet/

Here is the code I used, tested and working for reference:

Code:
 
ProtectedSub SelectedBarcodes_Click(ByVal sender AsObject, ByVal e As System.EventArgs) Handles SelectedBarcodes.Click
If IsPostBack = TrueThen
Dim str As StringBuilder = New StringBuilder()
' Select the checkboxes from the GridView control
Dim i AsInteger
For i = 0 To GridView1.Rows.Count - 1 Step i + 1
Dim row As GridViewRow = GridView1.Rows(i)
Dim isChecked AsBoolean = (CType(row.FindControl("CheckBox1"), CheckBox)).Checked
If (isChecked) Then
str.Append(GridView1.Rows(i).Cells(2).Text & ",")
EndIf
Next
' prints out the result
TextBox1.Text = (str.ToString())
EndIf
EndSub
Thank you all for your help.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
retrieving comma separated values from column abhishekkashyap27 SQL Server 2005 6 April 21st, 2008 09:27 AM
Adding JSCalendar in Gridview Template Column ??? maulik33 ASP.NET 2.0 Basics 1 August 28th, 2007 10:21 AM
Gridview with checkboxes kgaughan1 ASP.NET 2.0 Professional 0 February 11th, 2007 11:31 PM
Calculating Gridview Column Values rit01 ASP.NET 2.0 Professional 1 October 18th, 2006 03:46 PM
Adding up Column Values within a Datatable rit01 ASP.NET 2.0 Basics 1 May 31st, 2006 11:27 AM



All times are GMT -4. The time now is 02:39 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.