Subject: HowTo Pass Form data from Page1.aspx to Page2.aspx
Posted By: dati Post Date: 1/26/2004 5:34:14 PM
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
-------------------------------------------------

<html>
<head>
    <title>Datagrid Control example</title>
</head>
<body>
    <form action="Page2.aspx" method="post" runat="server">
        <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
-------------------------------------------------

<%@ 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



<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.
Reply By: melvik Reply Date: 1/26/2004 11:59:21 PM
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 form.
Second, when u have runat=server form with <asp: it'll couse the page to postback (self-submit)!

Always,
Hovik Melkomian.
Reply By: dati Reply Date: 1/27/2004 12:20:39 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"

<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?


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

Thank your for your reply.

Regards Dat
Reply By: melvik Reply Date: 1/27/2004 12:51:45 AM
I know, did I say something else?!

Always,
Hovik Melkomian.
Reply By: Imar Reply Date: 1/27/2004 1:57:11 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/default.asp?url=/library/en-us/vbcon/html/vbtskPassingValuesBetweenWebFormsPages.asp

Cheers,

Imar


---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply By: dati Reply Date: 1/27/2004 5:54:25 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.

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


Regards Dat.

Reply By: Imar Reply Date: 1/27/2004 5:57:37 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.

Go to topic 9023

Return to index page 962
Return to index page 961
Return to index page 960
Return to index page 959
Return to index page 958
Return to index page 957
Return to index page 956
Return to index page 955
Return to index page 954
Return to index page 953