Results 1 to 10 of 10

Thread: Data between forms

  1. #1

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Data between forms

    I do most of my dev work in Windows, and don't do very much ASP development. However I have a contract doing a webapp right now, and I am brushing up on what I have missed since the last one (which was .NET 1.1/VS2003).

    I am just wondering if there are any ways to pass data to a given aspx page. I can think of all the traditional methods such as using the query string or a session, but I figured maybe there have been some new things in the ASP.NET runtime since I last used it.

    The site is using a DBML (linq2sql) for data access, and I was trying to make use of some of these databound controls like the ListView and FormView.

    So on 1 page I have a listing of all records in a given DB table. If the user clicks to edit a given record, I then was going to navigate to another aspx page that would hold the FormView to edit the data.

    However I am wondering what my options are for passing, lets say the record ID over to the second page. Should I just use the query string? Is there a better method now?

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Data between forms

    note: i just re-read and saw you using listview in page one, you can do the same for listviews as i did for fromview in my example below.

    You have few options and the request.querystring is the last one to choose, one method is to use late-binding, since you didn't mentioned it i assume you use master page, here is a very basic example of formview in default.aspx:

    Code:
       <asp:FormView ID="FormView1" runat="server" ondatabound="FormView1_DataBound">
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text="RecordID"></asp:TextBox>
            </ItemTemplate>
            </asp:FormView>
        <asp:Button ID="btnPostBack" PostBackUrl="~/Default2.aspx" runat="server" Text="Button" />
    to get it's value from the next page (see the button at the bottom with the postbackurl property) this is what you should do:

    default2.aspx code behind:
    Code:
    if (Page.PreviousPage != null) // check if the user is entered the page from another page 
            {
                ContentPlaceHolder cp =
     (ContentPlaceHolder)Page.PreviousPage.Master.FindControl("MainContent"); // get the previous page master ContentPlaceHolder control
                FormView fv =
     (FormView)cp.FindControl("FormView1"); // get  previous page FormView control
    
    // now that you have the formview control you can use it to get to all other controls that inside the formview from the previous page (default.aspx) like so:          
      TextBox myTextBox =
     (TextBox)fv.FindControl("TextBox1"); 
    
    // now that you the textbox from the previous page you can use it inside default2.aspx just as any other text box
                Label1.Text = myTextBox.Text;
            }
    note: if you're not sure if some control exist it is always good idea to check if the control is null:

    Code:
         ContentPlaceHolder cp =
     (ContentPlaceHolder)Page.PreviousPage.Master.FindControl("MainContent"); // get the previous page master ContentPlaceHolder control
    
    if(cp != null) {
                FormView fv =
     (FormView)cp.FindControl("FormView1"); // get  previous page FormView control
    }
    
    etc . . .
    Last edited by motil; Oct 28th, 2010 at 05:37 PM.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Data between forms

    Hey,

    The only thing to be aware of with the approach that has been suggested (not a criticism motil, just wanted to point it out), is that you are closely coupling two pages together. i.e. any changes in one page, will likely result in changes to the other, and you will need to be aware of this going forward.

    You can argue that this is the same if you are posting the values through to the next page, or adding them to the querystring, of a session variable, however, PreviousPage is a more explicit coupling.

    That are certainly, nicer ways to do it now. For instance, the ASP.Net Routing capabilities that have come from ASP.Net MVC, and are now in ASP.Net 4.0, make the url "nicer" looking, and you can directly get information out of a correctly formatted url without having to do the normal inspection of the querysting.

    What version of ASP.Net are you using?

    Gary

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Data between forms

    Hi Gary,

    I must disagree on this one. one could say them same about using the MasterPageFile property inside the page directive. so by using this directive:
    Code:
    <&#37;@ Page Language="C#" MasterPageFile="~/MyMaster.Master"  
        AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits=Default" %>
    you're connecting all your website pages and by changing the master page file name will cause problems, but still this is the way of doing it. its up to you as the programmer to keep your code safe.

    and as you already mentioned the same rule applies to all other methods, at the end of the day you must get the wanted value somehow.

    as for Routing, i must admit i never had the chance to play with that yet, but if it still involves using QueryString in the background then it still consider bad practice for passing form information, you'll always pass information with POST instead of GET when you can.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Data between forms

    Even though the Page.Previouscontrol method is good, if the some other made changes in the particular table entry in database, and using the old values will cause a conflict. I would suggest to pass the ID and re populate the values once again.

    Worth reading : How to pass value between asp.net pages
    Please mark you thread resolved using the Thread Tools as shown

  6. #6
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Data between forms

    Hey,

    That is a good point about the use of Master Pages, however, you only have one "setting", the actual name of the master page. With PreviousPage, you are coupling potentially lots of different values, textboxes, labels, properties, etc, which are then all required within the next page. Also, what happens if you link to this page from another page, that doesn't have that information contained within it.

    As for the use of the query string parameters, it really comes down to what you are using it for. For instance, if you are just trying to "get" a particular record and display that, then I don't see an issue with doing an HTTP Get, however, if you are doing anything else, especially sensitive information, then absolutely, avoid using GET.

    Gary

  7. #7
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Data between forms

    Well, again, if like you say the developer is about to change many (or all) controls in the source page this only means the page is about to be re-build or even deleted and again in either way (using the findcontrol, request.querystring, sessions etc ...) you'll have to go to the target page and change it as well. that's makes no difference. you know what i mean ?

    anyway i'm not disqualify none of the methods beside using querystring, i don't like the idea of the end-user will change id's or whatever parameters in his/her browsers address bar so i avoiding using it as much as i can.
    beside having parameters in the address bar looks unprofessional and its soo 1996's
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  8. #8
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: Data between forms

    I mean to say, the values might have changed in the DB. And one more thing is that I am big fan of Page.Previouspage method. And another thing is that the data in the parent page has to populate in the
    If Not Page.IsPostBack method, as the Page.Previouspage method will cause the all the methods in the Previous Page once again
    Please mark you thread resolved using the Thread Tools as shown

  9. #9

    Thread Starter
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Data between forms

    Thanks guys. I am going to play around with a few methods and will let you know what I come up with.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Data between forms

    Quote Originally Posted by motil View Post
    beside having parameters in the address bar looks unprofessional and its soo 1996's
    The new ASP.Net Routing makes the URL look cleaner though, so not quite as bad as your typical query string:

    http://weblogs.asp.net/scottgu/archi...-0-series.aspx

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width