Solved

ASP.NET get values from datagrid on a previous page

Posted on 2010-11-30
10
514 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

831 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