Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2004, 04:49 PM
Authorized User
 
Join Date: Feb 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default Maintaining Variables from Page to Page

Hi, all. This is my first post, so please excuse any awkwardness on my part.

I am working on building a Web application to maintain county information in a database. Right now, I have a drop down list connected to the database to populate the drop down with the available counties for the user to select a county to edit. No problem there. However, I haven't seen any code in the books yet that explains ways to pass a user-selected value to another page for porcessing. Here is the control:

<form runat="server">
<asp:DropDownList id="ddlCounty" name="SelectedCounty" DataTextField="County" DataValueField="CountyOID" onSelectedIndexChanged="Load_Muni" AutoPostBack="True" runat="server" />
<asp:Button id="btnCountyEdit" onClick="CntyEdit_Click" text="Edit County" runat="server" />
</form>

And here is the subroutine that redirects the user to the second page:

Sub CntyEdit_Click(Sender As Object, E As EventArgs)
        Response.Redirect("cntyedit.aspx")
End Sub

When the user presses 'Edit County', a redirect takes the user to another page--the county edit page, which loads a data set for that county. What I need to do is have the selected county from the first page be exposed for use as a variable in the second page so I can load all the relevant values for that county for display through a SQL statement, as well as show that selected value to the user in the <h2> element.

Any thoughts on the best way to approach this? Any place where I could read about this?

Thanks in advance!

Dave

 
Old February 3rd, 2004, 05:36 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

ASP.Net has a feature called viewstate. You want to post the information back to itself to process kind of like a windows form. If you need to pass variables to another page for any reason you should use something like this:

page 1
<a href=nextpage.aspx?test=1>test</a>

page 2
page_load
dim myVar
myVar = request.QueryString("test")
label1.text = myVar

 
Old February 4th, 2004, 10:59 AM
Authorized User
 
Join Date: Feb 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply. I still have a question about this process.

The page I have created uses an ASP drop down list control, which requires that I place it in a form that runs on the server. So how am I able to pass the selected value to another page from that drop down list? I cannot use an asp control in a form that does not run on the server; therefore I cannot pass the value through the URL, correct?. So I cannot the QueryString method with a control, can I? I understand the anchor example above, just not its application to what I am trying to achieve with the drop down list.

Thanks again.

 
Old February 4th, 2004, 11:14 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,110
Thanks: 0
Thanked 3 Times in 3 Posts
Default

That's why you will want to take advantage of viewstate.

You can add an event handler to your dropdownlist control and set autopostback=true

Sub myDrpDwnList_OnChange(ByVal s As System.Object, ByVal e As System.EventArgs) Handles myDrpDwnList.SelectedIndexChanged

        If myDrpDwnList.SelectedIndex > -1 Then
            Label2.Text = "You selected<b> " & myDrpDwnList.SelectedItem.Text & "</b>"

        End If
    End Sub

HTML section:
<asp:DropDownList id="myDrpDwnList" AutoPostBack=True OnSelectedIndexChanged="myDrpDwnList_OnChange" runat="server"></asp:DropDownList>
 
Old February 4th, 2004, 02:41 PM
Authorized User
 
Join Date: Feb 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I believe I understand your example. It's like the ones in the book that show the user what they have selected from a drop down list. But they all involve AutoPostBack because the selected value is being processed on the same page. I'm still not sure how that selected variable is accessible from another page to which it was passed. It seems you are displaying the selected value back on the same page in Label2.

 
Old February 4th, 2004, 03:12 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Don't confuse the use of viewstate with passing variables to another page. Viewstate is for maintaining values during the "viewable" lifespan of a .net web form. As long as you are posting back to that same web form, the viewstate will be maintained as it's part of the posted form data.

When you want to pass variables to another page, you need to implement something that the other page can see. Viewstate has nothing to do with this concept and is not visible to the other page. You will need to pass a variable on using the querystring as was suggested, or by using the session state collection. (You could also use cookies but that's overkill for this type of scenario.)

Using the query string:
You can either create a link on the page that you can click that points to the other page, and on that link you can place the value you want to page.

<a href="anotherpage.aspx?variablename=value">Click Me</a>

OR

You can have the aspx webform post back to itself, and then redirect. Here's how you would modify the dropdownlist handler you already have:

Sub CntyEdit_Click(Sender As Object, E As EventArgs)
    Response.Redirect("cntyedit.aspx?county=" & CntyEdit.SelectedValue)
End Sub

Then on the other page you would need to retrieve that value. Usually this would happen in Page_Load:

Dim sCounty As String
sCounty = Request.QueryString("county")


Using session state:
The session is a data collection maintained by the web server. It is automatically loaded up for you when the page request is received. The server identifies the user with a session cookie and uses that to load up just that users session data. You can use the collection to store practically anything. One of the nice things about using the session is that it's maintained throughout the life of a client browser session on that site (assuming they haven't been inactive longer than the session timeout limit). Because of this, session values are typically those that you want to use on more than just one page. If you were return randomly back to your "edit county" page you'd still be able to load up the county that's in the session value. This may not be desirable.

In your example, we'll save the selected county to the session and then retrieve it in the other page.

Sub CntyEdit_Click(Sender As Object, E As EventArgs)
    Session("county") = CntyEdit.SelectedValue
    Response.Redirect("cntyedit.aspx")
End Sub

Then on the other page, we can get the value:

Dim sCounty As String
sCounty = Session("county")


Peter
------------------------------------------------------
Work smarter, not harder.
 
Old February 4th, 2004, 03:42 PM
Authorized User
 
Join Date: Feb 2004
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter, you absolutely rock. It works perfectly. Thank you.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Maintaining state in an Ajax page isme BOOK: Professional Ajax ISBN: 978-0-471-77778-6 0 April 27th, 2006 07:24 PM
Page Variables or Properties ~Bean~ ASP.NET 1.x and 2.0 Application Design 1 October 25th, 2005 02:08 PM
How to pass variables from Aspx page to Asp Page jayaraj Classic ASP Basics 2 May 23rd, 2004 06:56 AM
How to pass the variables in Aspx page to Asp Page jayaraj ASP.NET 1.0 and 1.1 Basics 3 May 23rd, 2004 06:55 AM
Maintaining Global variables tenikiwon Beginning PHP 1 June 19th, 2003 02:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.