p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 1.0 and 1.1 Basics (http://p2p.wrox.com/forumdisplay.php?f=60)
-   -   HowTo Pass Form data from Page1.aspx to Page2.aspx (http://p2p.wrox.com/showthread.php?t=8601)

dati January 26th, 2004 06:34 PM

HowTo Pass Form data from Page1.aspx to Page2.aspx
 
Hi,

I am currently studying the book (Beginning ASP.NET using VB.NET: ISBN: 1861005040 (Published Year: 2001)).

I am stuck at the code pass from Page1.aspx to Page2.aspx, I think it could be todo with the ASP.NET Version. I am using Microsoft.NET Framework v1.1.4322.

I have try putting the runat="server" in the Form section, to get rid of the error, but the submit does not Work.

I know how to extract the data from the same page, but since I am start to learn ASP.NET that's why I want to know why this method of passing from page1 to page2 doesn't work.

Please see the following code.

Thanks in advance.

Regards Dat.

Code:
Page1.aspx
-------------------------------------------------
Code:

<html>
<head>
    <title>Datagrid Control example</title>
</head>
<body>
    <form action="Page2.aspx" method="post" <s>runat="server"</s>>
        <p>
            Enter Your Name:
            <asp:TextBox id="TextBox1" runat="server" />
        </p>
        <p>
            <input type="submit" value="Submit" />
            <input type="reset" value="Reset" />
        </p>
    </form>
</body>
</html>

Page2.aspx
-------------------------------------------------
Code:

<%@ Page Language="VB" %>
<script runat="server">

    Sub Page_Load()
            Message1.text = Request.Form("TextBox1")
    End Sub
</script>
<html>
<head>
    <title>datacontrol response</title>
</head>
<body>
    <p>
        <asp:Label id="Message1" runat="server" />
    </p>
</body>
</html>

Error When running Page1.aspx
Server Error in '/5040' Application.
---------------------------------------------------------------------
Control 'TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Control 'TextBox1' of type 'TextBox' must be placed inside a form tag with runat=server.



Corrected Code:
Page1.aspx

Code:

<script>
    Public Sub Button1_Click(sender As Object, e As System.EventArgs)
        Server.Transfer("Page2.aspx",True)
    End Sub
</script>

<html>
<head>
    <title>Datagrid Control example</title>
</head>
<body>
    <form runat="server">
        <p>
            Enter Your Name:
            <asp:TextBox id="TextBox1" runat="server" />
        </p>
        <p>
            <asp:Button id="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
        </p>
    </form>
</body>
</html>


The red highlighted code are the new code.

melvik January 27th, 2004 12:59 AM

its wahat I now & guess if its wrong plz correct me.
First of all when u use <asp: tags it must be in runat=server [u]form</u>.
Second, when u have runat=server [u]form</u> with <asp: it'll couse the page to postback (self-submit)!

Always:),
Hovik Melkomian.

dati January 27th, 2004 01:20 AM

Hi Hovik Melkomian,

The reason why the runat="server" attribute is not set on the form because asp.net "automatically" create the action attribute within the Page1.aspx which stop me from moving to another page (Page2.aspx).

I select View Source when Page1.aspx is loaded with <form runat="server"
Code:

<form name="_ctl0" method="post" action="Page1.aspx" id="_ctl0">
<input type="hidden" name="__VIEWSTATE" value="dDwxOTg4MTk0Mzc2O3Q8O2w8aTwxPjs++==" />

My Question is can this be done without specifying runat="server" in the form section, if not how to pass data from page1.aspx to page2.aspx?

Code:

<form action="Page2.aspx" method="Post">
     <asp:Textbox runat="server">
</form>

Thank your for your reply.

Regards Dat [8D]

melvik January 27th, 2004 01:51 AM

I know, did I say something else?!

Always:),
Hovik Melkomian.

Imar January 27th, 2004 02:57 AM

Hi there,

The book was originally written for ASP.NET 1.0. Early beta's of that product indicated that it was possible to submit data from 1 page to the next. However, this feature was dropped in the final release.

It is possible to send information from 1 page to another, it's just not as straight-forward as you'd want it to. Check out the following articles for more information:

http://authors.aspalliance.com/kenc/passval.aspx
http://msdn.microsoft.com/library/de...FormsPages.asp

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.

dati January 27th, 2004 06:54 AM

Thanks Imar,

That's the answers I was looking for, I was suspected the error could be due to the version of the ASP.NET that I am running.

[:p] Look like I have to buy a book that have code written for ASP.NET version 1.1

:D
Regards Dat.
[8D]

Imar January 27th, 2004 06:57 AM

Hmmm, not sure what you mean with that. The feature was dropped when version 1.0 came out, so it's not available in ASP.NET 1.0 but also not in ASP.NET 1.1.

It seems that later versions (i.e. Whidbey) may again support passing info from page to page.

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.


All times are GMT -4. The time now is 10:24 AM.

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