Results 1 to 10 of 10

Thread: Simple Question About Transferring Data.

  1. #1

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160

    Simple Question About Transferring Data.

    I am new to ASPX, I'm guessing this is just a simple question.

    Ok so I have two ASPX WebForm pages.

    The first page has a list box which a user may select items on.

    The second page has a list box with only the items that were selected on the first page.

    Very simple thing, in ASP I would create a form with a submit button and get the data that way. It doesn’t seem a <FORM> tag is proper in ASPX (it tells me "Per the active schema, the element 'FORM' cannot be nested within html")

    So how are you supposed to transfer data from one page to another?

    Maybe an array of a session variable?
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  2. #2
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Transferring data from one page to another is possible but you should get into the mind-set of doing it all in one. Very simple example.

    Code:
    <%@ Page Language="C#" %>
    <script runat="server">
    
        void Button1_Click(Object sender, EventArgs e) {
            ListBox1.Visible = false;
            ListBox2.Visible = true;
            ListBox2.Items.Clear();
            if (ListBox1.SelectedIndex > -1) {
                for (Int32 i=0;i<ListBox1.Items.Count;i++) {
                     if (ListBox1.Items[i].Selected)
                        ListBox2.Items.Add(ListBox1.Items[i].Text);
                }
        
            }
        }
        
        void Page_Load(Object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                ListBox2.Visible = false;
            }
        }
    
    </script>
    <html>
    <head>
    </head>
    <body>
        <form runat="server">
            <p>
                <asp:ListBox id="ListBox1" runat="server" Width="123px" Height="103px" SelectionMode="Multiple">
                    <asp:ListItem Value="One">One</asp:ListItem>
                    <asp:ListItem Value="Two">Two</asp:ListItem>
                    <asp:ListItem Value="Three">Three</asp:ListItem>
                    <asp:ListItem Value="Four">Four</asp:ListItem>
                </asp:ListBox>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
                <asp:ListBox id="ListBox2" runat="server" Width="136px" Height="104px"></asp:ListBox>
            </p>
            <p>
                &nbsp;
            </p>
            <p>
                <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Button"></asp:Button>
            </p>
            <!-- Insert content here -->
        </form>
    </body>
    </html>

  3. #3

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Thank you, but I still need to know how it's done.

    The real issue is I have a report that I need to generate based on the information that is inputted. It involves multiple List boxes, combo boxes and text boxes, which a user fills out and then a printable report is generated. I do not believe that this is possible, at lest not practical to do on one page.

    If someone provides me with an example of transferring list boxes from one page to another, I can figure out the rest.
    Last edited by Dmyze; Aug 13th, 2002 at 01:55 PM.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  4. #4
    Fanatic Member Patoooey's Avatar
    Join Date
    Aug 2001
    Location
    New Jersey, USA
    Posts
    774
    Originally posted by Dmyze
    Thank you, but I do not believe that is what I asked for. Unless I am reading it wrong that looks like only one page.
    A 5 second search on Google groups turned up numerous links to samples for passing data.

    http://www.dotnetbips.com/displayarticle.aspx?id=79

    The reason I didn't include it before is because, after 5 months of working with ASP.NET, that question has become a bore. Every ASP guy wants to transfer data from one page to another and I just don't see the point. It's easier to control on one page. You just plop a Panel or PlaceHolder in the HTML and fill it with whatever you want on every postback. Going from one page to another defeats the whole point of trying to get away from the spaghetti code of ASP apps.

    I've seen multi-page wizards that were all one page. The only time I've used a seperate aspx page is to stream images. That works the same as b4.

    It's not ASP. Free your mind.

    http://www.aspnextgen.com/
    http://www.asp.net/
    http://www.4guysfromrolla.com/
    http://aspalliance.com/

  5. #5
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    This is the world of programming we are talking about isn't it? There is never one hard and fast way of doing things. I can straight away think of a good example of posting to another page. You have a search textbox on your main page. You make it a standard form who's action is GET so that the aspx page that displays search results gets the query from the querystring and the user can add the page to their favorites.
    I'm very much into the "posting to self" mindset you talk about but there are numerous situations where you will want a user to be able to add a page to their favorites or send a link to a friend to a page that includes dynamic content. If it's dynamic then you need querystrings.

  6. #6

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    I have over 80 reports that are going to use the same input screen. You want me to put all 80 reports onto one page?? I avoid Spaghetti Code by doing that?

    I guess I'll figure out the server.transfer object, what a hassle for such a simple operation.
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  7. #7
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Well dont despair. To answer your original question you can put a standard old form into your first page and get it to send to a new page. In the following example I use the get method so that the selected values will be in the querystring, a handy option for users if they want to save the page to their favorites.

    <form id="Form1" method="get" action="newpage.aspx">
    <SELECT name="options" multiple>
    <OPTION value="opt1">Option1</OPTION>
    <OPTION value="opt2">Option2</OPTION>
    <OPTION value="opt3">Option3</OPTION>
    <OPTION value="opt4">Option4</OPTION>
    <OPTION value="opt5">Option5</OPTION>
    </SELECT>
    <input type="submit" value="Submit">
    </form>

    In newpage.aspx the code to get all the selected values would be as follows:

    Dim coll As Collections.Specialized.NameValueCollection
    Dim arrSelectedValues() as String
    Dim arrKeys() as String
    Dim x, y as integer

    coll = Request.QueryString
    arrKeys = coll.AllKeys
    for x = 0 to arrKeys.GetUpperBound(0)
    If arrKeys(x) = "options" then 'options is the listbox name here
    arrSelectedValues = coll.GetValues(x)
    End If
    Next x
    For y = 0 To arrSelectedValues.GetUpperBound(0)
    Response.Write("Selected Value " & (y + 1) & ":" & arrSelectedValues(y) & "<br>")
    Next

    Now you have an array of the selected values (not the text values) of the listbox.

  8. #8

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    (my hassle comment was towards the server.transfer object, not the forum..)

    I don't want to do it the 'old way' if I wanted it done that way I would have never posted my question here. (not to mention I aleady devloped a cool WebForm for the start page that fills all kinds of fields with data for the user.)

    I think the answer lies in the server.transfer object.

    In my opinion if I was MS I would have made a extremely simple way to pass objects from one page to another, the objects are already running off the server I wouldn't think it would be hard to build a simple way to share objects.. But I'm not MS. Maybe server.transfer is simple, just all the articles I read on it say "somewhat complex but sophisticated method of passing values"
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  9. #9

    Thread Starter
    Addicted Member Dmyze's Avatar
    Join Date
    Mar 2002
    Location
    Seattle
    Posts
    160
    Server.transfer was easy.


    Code:
    'In sending page:
      Public ReadOnly Property listStores() As ListBox
        Get
          Return lstStores
        End Get
    
      Private Sub cmdCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCreate.Click
    
        Server.Transfer("stores.aspx")
    
      End Sub
    
    'In recieving page:
      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        Dim wf1 As dart._Default
        Dim lstItem As ListItem
        Dim lstTemp As ListBox
        'get reference to current handler instance
        wf1 = CType(Context.Handler, dart._Default)
    
        lstTemp = wf1.listStores
    
    
        For Each lstItem In lstTemp.Items
          lblList.Items.Add(lstItem)
        Next
    
      End Sub
    -Daryl
    "Two More Rolls of Duct tape, and the world is mine!"
    VB.NET Guru

  10. #10
    Hyperactive Member
    Join Date
    Dec 2001
    Location
    Dublin, Ireland
    Posts
    262
    Funny how people refer to it as the old way.
    <form runat="server">
    renders as
    <form action="self" method="post">
    and the only difference in my "old" way is putting a get in for a post.
    asp.net server controls are a simple way of rendering the same stuff as always except it includes events and stores state if you wish. Old way or new way your dealing with html and posted headers at the end of the day.

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