Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.NET C# Newb - Form fields not posting???

Status
Not open for further replies.

joepeacock

Programmer
Nov 5, 2001
74
US
I'm having a strange problem with form fields not being available to my .aspx.cs pages after being POSTed from another page. In order to rule out any interference from other features, I created two simple pages "test.aspx" and "test2.aspx" -- test posts a form to test2.

=== test.aspx ===========
<%@ Page Title="TEST" CodeFile="test.aspx.cs" Language="C#" AutoEventWireup="true" Inherits="test" Codebehind="test.aspx.cs" %>

<html>
<body>
<form method="post" action="/test2.aspx">
<input type="text" name="testField" /><input type="submit" value="Go" />
</form>
</body>
</html>
=========================

=== test2.aspx.cs =======
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class test2: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (string param in Request.Form)
{
Response.Write(param + "=" + Request.Form[param] + "<br />");
}

}
}
=========================

(test2.aspx is empty except the default stuff Visual Studio adds when creating a new web form)

When I build this and load it in my browser, there is nothing output on test2.aspx. HOWEVER, if I debug it, using F5 in Visual Studio, then it works as expected.

I have tried removing the "Form" and just looking for the POSTed value in Request.Params. When I do that, I see all of the request values, cookies, etc, but still not the form field value (again, it does show up, as expected, when debugging).

What am I doing wrong, here? Please help!

Thanks
 
webforms is a server framwork you cannot open the page like opening an html file (double click in windows explorer). you need a server to process the aspx requests. when you press F5 you start up cassini, VS web server. this is why the requests are processed.

something to consider.
the approach you are using, html form posted to another webform. works when you are talking about standard web development. just about every web framework (ms mvc, monorail, ror, etc) works this way, except webforms.

webforms wants you to post back to the same uri. you could then use the page life cycle and various form events to update the html. (very similar to how winforms works). If this sound awkward, it is.

Using the approach you are describing above I would opt for an MVC framework: MS MVC, Monorail, FUBU MVC. not the webforms html engine. You will still need a web server to process the requests but the handling of urls is in line with your approach.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Thank you for your replies.

jmeckley, I did not mean to imply that I was opening the file through Windows Explorer. My development environment runs IIS7 and I am browsing these pages through both local (192.168...) and internet URIs (dev...com) with the same results.

Like I said, I am new to .NET (though not to web development by a long shot), so forgive my confusion. I thought by using <form> tags without the runat="server" parameter, I was not using the webforms/postback model, and my forms would behave as normal, natural HTTP/HTML forms - with the browser sending the submitted data to the next page via HTTP.

Actually, I can change the action URL of this form and post successfully to a non-.NET page. So, I imagine the problem is in the page that I expect to be receiving the data. Is that the problem? The test2.aspx page is expecting any incoming data to be from a postback? Why, then, does this work under Cassini, but not IIS? Is there possibly an IIS configuration problem?

jbenson001 - I will look into cross posting. From an initial glance, it looks like it is still playing within the webforms realm, though. I'll investigate further. Thanks for the tip.

Any more advice, guys?
- Joe
 
if it works under cassini but not IIS then it is probably a configuration problem. I'm not 100% sure how webforms handles not "server" forms. I know it's possible, but it strays away from the webforms framework and how requests are handled.

an aspx page will use the page life cycle. if you don't need the entire webform page life cycle you could post to an generic handler ashx. this will still flow through the asp.net framework, but it will not use the webforms framework. that's probably not what you are talking about though.

the cross posting is probably the best direction for now. If you have been doing web development for a long time and webforms seems awkward then take a look at MVC. it may seem more familiar.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top