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

0 comments:

Post a Comment