Solved

ASP.NET get values from datagrid on a previous page

Posted on 2010-11-30
10
511 Views
Last Modified: 2012-05-10
In the previous page in my web app I have a gridview with a checkbox column and values in the other columns. How do I get the values from the gridview in the previous page from the gridview? I figured I'd loop through the gridview in the previous page like below and ones that are checked I'd just grab those values and put them in my current page.
For Each row As GridViewRow In myGV.Rows

                    Dim cb As CheckBox = row.FindControl("mycheckbox")

                    If cb IsNot Nothing AndAlso cb.Checked Then

               

'do my stuff

end if

Open in new window

0
Comment
Question by:cobolinx1
  • 5
  • 5
10 Comments
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34244833
Just to verify - you have a Gridview with AllowPaging turned on.  You are trying to access every row of the gridview, not just the current page but you are having a problem.  Is that correct?
0
 

Author Comment

by:cobolinx1
ID: 34257255
I turned allowpaging off. Pretty much I select rows by checking a checkbox column in the gridview and click a button so that I can go to a new page and display the ones that I checked.
0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34262195
OK, that makes sense.  So what are you using as your underlying datasource - is it a datatable?

Do you want to post your code?
0
 
LVL 11

Accepted Solution

by:
MajorBigDeal earned 500 total points
ID: 34271641
So if you are only displaying selected records in your gridview then you can maintain the checked state for all the records elsewhere.  Here is an example of how to do that - hope it helps!

http://www.experts-exchange.com/Programming/Languages/.NET/Q_23117312.html
0
 

Author Comment

by:cobolinx1
ID: 34283407
Thanks for the example (250 points) that really helps....but how do I reference the datatable on that page when I goto the next page?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:cobolinx1
ID: 34284278
Pretty much I was just binding the datagridview to a connection I already established. I took that example from above and grabbed data from the database then pretty much created a new datatable by reading in the table I already had to handle the itemstate but when I try and sort now it says it fired event Sorting which wasn't handled. Do I have to build that from scratch??


Below is the code that I had in my example that I created a new connection and bound the datagrid to it.
<asp:GridView ID="gvterm" runat="server" AllowSorting="True" 

                AutoGenerateColumns="False" BackColor="White" BorderColor="#000066" 

                BorderStyle="Solid" BorderWidth="1px" CellPadding="4" 

                DataSourceID="ORCCON" Width="775px">

                <RowStyle BackColor="White" ForeColor="#003399" />

                <Columns>

                    <asp:TemplateField HeaderText="Select">

                        <ItemTemplate>

                            <asp:CheckBox ID="Select" runat="server" />

                        </ItemTemplate>

                        <ControlStyle Font-Underline="False" />

                        <HeaderStyle Font-Underline="True" />

                    </asp:TemplateField>

                    <asp:BoundField DataField="EMPLOYEE_ID" DataFormatString="{0:F0}" 

                        HeaderText="EMPLOYEE_ID" ReadOnly="True" SortExpression="EMPLOYEE_ID" />

                    <asp:BoundField DataField="PFI" HeaderText="Employment" SortExpression="Employment" />

                    <asp:BoundField DataField="FIRST_NAME" HeaderText="FIRST_NAME" 

                        SortExpression="FIRST_NAME" />

                    <asp:BoundField DataField="LAST_NAME" HeaderText="LAST_NAME" 

                        SortExpression="LAST_NAME" />

                    <asp:BoundField DataField="DOB" DataFormatString="{0:d}" HeaderText="DOB" 

                        SortExpression="DOB" />

                </Columns>

                <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />

                <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />

                <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />

                <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />

            </asp:GridView>

        </div>

Open in new window

0
 
LVL 11

Expert Comment

by:MajorBigDeal
ID: 34287174
Excellent - sounds like you are on track.  Yes, you have to build the sorting from scratch and it is a bit of a pain but there are lots of good examples of how to do this floating around.  I don't know why there isn't some kind of default implementation - maybe there is and I don't know about it!
0
 

Author Comment

by:cobolinx1
ID: 34294269
But is there a way to go to a new webpage like Server.Transfer("~/default2.aspx", True) and get the values from the datagrid on default.aspx?
0
 
LVL 11

Assisted Solution

by:MajorBigDeal
MajorBigDeal earned 500 total points
ID: 34295383
You can store the values in Context.Items and after the Server.Transfer that info will still be in Context.Items for you to access.  Also, you had mentioned that you were persisting using a DB so of course you could use that as well.
0
 

Author Closing Comment

by:cobolinx1
ID: 34301988
Perfect!!!!!! I'll loop through and store them as items then can call them back....Now I just need to figure out how to sort the checkbox field....but I guess thats a differant question.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 110
Combobox, SelectedIndexChanged Get Index 2 21
asp.net mvc5 3 37
Setting location of a form 4 22
Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now