Newbie Question

T

Terry Holland

I passed my 70-305 exam about 4 months ago and have not used any asp.net
since

I am therefore embarrassed to ask the following question.

Im in the process of designing an ASP.Net application. Users will be
required to login and once authenticated be able to use the services
available to them on the site. They will build up what is effectively a
'shopping basket' and this will need to be available on a number of pages
that they might visit during their session.
I think I'm right in saying that this information is best stored in a
session variable.
(Embarrassing bit) What im not sure about is the way to move between forms.
Im trying to use an HTML Submit button with no success

<body>
<form id="Form1" method="post" action="WebForm2.aspx" runat="server">
<INPUT type="submit" value="Submit">
</form>
</body>

This is the html (except action) that is generated when I add HTML Submit
button on page. This works if I remove runat="server", so Im wondering why
the runat is put there.
If I do move to another page using Form Post, how do I read the values
posted by this form in the next page and how can I use these values in a
code behind page?

tia

Terry Holland
 
G

Guest

To answer your second question first:
The "runat=server" means form events will fire on the server and you can
write "code-behind" for them
If you remove it, yes the form actions IN THE CLIENT but no server-side
events occur

Regarding sharing your data:
If you want to use the Session you must attach your data to the Session
object.
Something like:
Session("myData") = theDataToPersist

Then in your next page you should be able to talk about it:
newPageLocalVariable = Session("mnData")

Be aware Session objects are not necessarily the best solution to this and
consider using a DB table for your shopping cart
Use the SessionID of the Session opbject to trackt he current user's
entries in this table or maybe get them to login and use their login instead.

At least that's what I'd do - there are probably other options too...

(e-mail address removed)
 
G

Guest

Sorry, missed the bit about you targetting a different page with the from's
action:

In your "receiving" page you can talk about the Request object's .Forms()
collection to access the values sent from the form:
myLocalVar = Request.Forms("nameofTextBox")

HTH

(e-mail address removed)
 
T

Terry Holland

Thanks for speedy response.

So I have a class clsUser and 2 pages WebForm1 & WebForm2 with the following
code

WebForm1.aspx

HTML
===============================================
<body>
<form id="Form1" method="post" action="WebForm2.aspx" runat="server">
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<INPUT type="submit" value="Submit">
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</form>
</body>

Code Behind
===============================================
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim objUser As New clsUser

objUser.UserName = TextBox1.Text
Session("User") = objUser
Response.Redirect("WebForm2.aspx")
End Sub


WebForm2.aspx

Code Behind
===============================================
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
Dim objUser As clsUser
objUser = Session("User")

Me.Label1.Text = objUser.UserName
End Sub



On WebPage1 I have 2 buttons, one HTML Submit Button & another Web control
button. The example I have shown is the only way I can think of to move
from WebForm1 to WebForm2 but Im sure this is not the "right way". Could you
give me a code snippet indicating how to carry out the above task in a more
"usual" way.

tia

Terry
 
G

Guest

Well, I don't see much wrong with it -
except you don't need the submit button in your HTML if your going to hit
the Button1 :)

Mind you - I mainly code FORM's to submit back to the same page so there
could be a better way.

Could you not just create the objUser object straight away in WebPage2 in
the Page_Load?

Using:
objUser.UserName = Request.Forms("TextBox1.Text")

(e-mail address removed)
 
T

Terry Holland

Mind you - I mainly code FORM's to submit back to the same page so there
could be a better way.

So is it better to have HTML controls on the Form and pickup the values
from these controls on the next page using Request.Forms("TextBox1.Text")?
If I do this I have to remove the runat=server from form tag and therefore
lose any Server Controls from my form.

If I decide to use Server controls and use the Code Behind page to move me
from one page to another, is Response.Redirect the correct function to use?
 
G

Guest

The drawback with Response.Redirect is that it sends a message back to the
client browser to redirect its request...

Look up Server.Transfer. This reroutes server activity to another page
WITHOUT the roundtrip to the browser...

I don't understand why removing the "runat=server" would prevent server-side
controls. Surely each one has a "runat" attribute?

Sorry I'm getting hazy now but my current role is non-coding - which I am
trying to get out of :) and i'm shoprt of development tools to test out what
I'm telling you...

(e-mail address removed)
 
T

Terry Holland

If my Form tag has runat="Server" then then submit button seems to have no
effect.
If I remove the runat attribute then I get the following error if i have any
web contols on that form.
Control 'Button1' of type 'Button' 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 'Button1' of type
'Button' must be placed inside a form tag with runat=server.

Source Error:


An unhandled exception was generated during the execution of the
current web request. Information regarding the origin and location of the
exception can be identified using the exception stack trace below.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top