Solved

ASP.NET get values from datagrid on a previous page

Posted on 2010-11-30
10
513 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!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
.net and XML report from SCCM 5 25
Write to file when application closes (by logoff) - VB.NET 1 32
Reading the Web.Config using IIS 7.5? 4 36
C# bracket error 3 31
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

778 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