Showing posts with label couple. Show all posts
Showing posts with label couple. Show all posts

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

Monday, March 26, 2012

Passing values in QueryStrings and form actions

Hi, new to .Net.

Despite labouring through a 600 page book I am still baffled by some of the fundamentals. I have a couple of questions.

1) Form Submission

Every example in the book had this tag on the page.

<form id="FormName" runat="server">

By default the form submits to the same page that it the form is on.

How do I make a form submit to another page?

In html/asp this would be ..

<form name="FormName" action="SomeOtherPage.asp" method="get">

2) How do you pass variables in the QueryString?

In ASP this would be:

<form name="FormName" action="SomePage.asp?SomeID=275" method="Get">

and, on SomePage.asp you would access the value of SomeID by writing

SomeID = Request.QueryString("SomeID")

How do you do this in .Net?

Thanks for any help.In asp.net 2.0 you can specify a different page to post the form to if nessecary but you generally never have queryString values in a form action. QueryString values work in the same way > Request.QueryString("SomeID")

One of the main advances in .net is that is "event" driven i.e. on one page you can handle only the specific events you want to like page load, button1 click, button2 click etc when they occur. classic asp could not do this so you posted form info to another page to process it. .net you present the info and send it back to the same page to process.

Once you get the hang of it more complex tasks become easier to code etc.
Just a quick note.

If you are using C#, then it is:

Request.QueryString["SomeID"]

and not

Request.QueryString("SomeID")

Hope that helps!!

Gary
In asp.net 2.0 you can specify a different page to post the form to if nessecary but you generally never have queryString values in a form action. QueryString values work in the same way > Request.QueryString("SomeID")

One of the main advances in .net is that is "event" driven i.e. on one page you can handle only the specific events you want to like page load, button1 click, button2 click etc when they occur. classic asp could not do this so you posted form info to another page to process it. .net you present the info and send it back to the same page to process.

Once you get the hang of it more complex tasks become easier to code etc.

But how do you concatenate the querystring to pass the values. In ASP.net the form tag looks like

<form id="FormID" runat="server">

by default the form submits to the same page it is on. Let's say the page it is on is called Booking.aspx ... how would I make the form submit to Booking.aspx?BookingID=237

Thanks for your reply.
just add the action attribute

<form id="FormID" action="Booking.aspx?BookingID=237" runat="server">

This only works is asp.net 2.0, not 1.1 and I suggest not using querystring values in form action, it defeats the purpose of posting form data. put a textbox or hidden control <asp:hiddenfield id="bookingID" value="123"....etc/>

Posting to another page you can get form data like request.form("bookingID")

post_back to same page would be bookingID.value
just add the action attribute

<form id="FormID" action="Booking.aspx?BookingID=237" runat="server">

This only works is asp.net 2.0, not 1.1 and I suggest not using querystring values in form action, it defeats the purpose of posting form data. put a textbox or hidden control <asp:hiddenfield id="bookingID" value="123"....etc/>

Posting to another page you can get form data like request.form("bookingID")

post_back to same page would be bookingID.value

Thanks for your reply ... but I can't get it to work. (I am using ASP.Net 2.0)

<%@. Page Language="C#" AutoEventWireup="true" CodeFile="Test.aspx.cs" Inherits="Test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" action="Login.aspx" runat="server">
<div>
<asp:Label ID="myLabel" runat="server" />
<asp:TextBox ID="TextBox1" runat="server" Text="hello"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>

When I click the submit button, the form still submits to the page it is on (test.aspx). Why isn't it submitting to Login.aspx?

Thanks again.
Sorry .net sets the form action attribute unless you override it in an inherited class.

You set postBackURL="myPage.aspx" on a button etc.
Ah, that's it. Thanks very much.
Read about cross page postbacks:
http://www.codeproject.com/aspnet/PostToAnotherPage.asp