Solved

ASP.NET get values from datagrid on a previous page

Posted on 2010-11-30
10
515 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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
 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

680 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