Thursday, March 29, 2012

Passing values between pages

I'm attempting to pass values between a couple of webpages in Visual Studio Express 2008. I feel I'm almost there, but I can't get the info to display on the second page. BTW I'm using master and code behind pages.

Here's the code:

page1.aspx
=========

<%@dotnet.itags.org. Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="page1.aspx.vb" Inherits="page1" title="Untitled Page" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

<asp:Label ID="Label1" runat="server" Text="First Name:"></asp:Label> <asp:TextBox id="txtFirst" runat="server" /><br>
<asp:Label ID="Label2" runat="server" Text="Last Name:"></asp:Label> <asp:TextBox id="txtLast" runat="server" />
<asp:Button id="button1" Text="Submit" onclick="doSubmit" runat="server" />

</asp:Content>page1.aspx.vb=============Partial Class page1 Inherits System.Web.UI.Page Sub doSubmit(ByVal Source As Object, ByVal E As EventArgs) Context.Items.Add("first", txtFirst.text) Context.Items.Add("last", txtLast.text) Server.Transfer("Page2.aspx") End SubEnd Classpage2.aspx========<%@dotnet.itags.org. Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="page2.aspx.vb" Inherits="page2" title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"><asp:Label ID="Label1" runat="server"></asp:Label></asp:Content>page2.aspx.vb==========Partial Class page2 Inherits System.Web.UI.Page Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs) Label1.Text = "The Name you entered was : " & Context.Items("first") & " " & Context.Items("last") End SubEnd Class
 Hopefully someone with more experience can help me solve what seems a relitively simple process. 
 
TIA
Steve

Check below link

Ways to pass data between webforms

HC


Try this:

/////////////////////////
// Page1
/////////////////////////

Public Property FirstValue() As String
Get
Dim viewState As Object = Me.ViewState("FirstValue")

If viewState Is Nothing Then
Return String.Empty
Else
Return CStr(viewState)
End If
End Get
Set
Me.ViewState("FirstValue") = value
End Set
End Property

Sub doSubmit(ByVal Source As Object, ByVal E As EventArgs)
FirstValue = txtFirst.Text

' Add additional public properties and assignments as needed

Server.Transfer("Page2.aspx")
End Sub

/////////////////////////
// Page2
/////////////////////////

Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
Dim contextHandler As Object = Context.Handler
If TypeOf contextHandler Is page1 Then
Dim page1Class As page1 = CType(contextHandler, page1)
Dim firstValue As String = page1Class.FirstValue
Label1.Text = "The Name you entered was : " + firstValue
Else
Label1.Text = "Not from Page 1!"
End If '
End Sub

NC...


Try using your transfer method like this Server.Transfer("your detail page",true). i think that should help you out.


NC,

Thanks for your response to my question.

I am attempting to utilze your code, but I'm running into a problem on the second vb page

NC01:

Sub Page_Load(ByVal Source As Object, ByVal E As EventArgs)
Dim contextHandler As Object = Context.Handler
If TypeOf contextHandler Is page1 Then
Dim page1Class As page1 = CType(contextHandler, page1)
Dim firstValue As String = page1Class.FirstValue
Label1.Text = "The Name you entered was : " + firstValue
Else
Label1.Text = "Not from Page 1!"
End If '
End Sub

I'm getting the following error: "Type 'page1' is not defined."

Can you tell me how to resolve this.

TIA

Steve


Why cont you use the PreviousPage Property to get the previous page all contsol in current page..just have a lokk at the folowing articvles..it would help you out..

http://forums.asp.net/t/1005660.aspx


I thought that you posted that the class name of the first page is page1? You are not by chance going across virtuals are you?

NC...


NC,

No they are physically seperate pages.

Steve

0 comments:

Post a Comment