Ev Conrad
Ev Conrad

Reputation: 333

ASP.NET WebForms form not being posted - why?

Although I don't consider myself a web programmer, I've done a fair amount of web programming, so I'm almost embarrassed to ask what is wrong with the below code. There is something fundamental about ASP.NET that I must be missing.

I have two pages, source.aspx and destination.aspx:

source.aspx - html:

<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
    <input id="Text1" type="text" />
    <input id="Text2" type="text" />
    <input id="Checkbox1" type="checkbox" />
    <input id="Submit1" type="submit" value="submit" />
</form>
</body>

destination.aspx - code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        // Below variable gets assigned null.
        string text1 = Request.Form["Text1"];
    }

When I submit the source.aspx form, once it gets to the destination.aspx form, there is no information in the FORM variables. I thought that the forms 'runat="server" ' would ensure that I ended up in the ASP.NET page pipeline, and in fact I can step through this code. There are no POSTed form variables other than viewstate, and the PARAMs collection doesn't have anything corresponding to control data either, not even ones that would correspond to decorated control names. The question is, what is happening that is making my POSTed variables 'disappear', at least to the destination page?

I'm not looking for alternatives how to make this work (i.e. make the controls server controls with runat="server", etc). I can fix the problem. What I am trying to determine is 'what is it about ASP.NET that makes my controls not appear to be receivable by the destination page. Thanks - I thought that I understood HTTP pretty well, but there seems to be a little sleight of hand courtesy of ASP.NET that I'm not seeing.

Upvotes: 1

Views: 5370

Answers (3)

N. Wigi
N. Wigi

Reputation: 11

In my case, it is because FriendlyUrlSettings.AutoRedirectMode at RouteConfig I removed this line then it's work.

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        //settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
}

Upvotes: 1

hakan
hakan

Reputation: 3534

Request.Form uses name attribute from elements. So you should write name attributes to each of the html elements.

<body>
<form id="form1" action="destination.aspx" method="post" runat="server">
    <input id="Text1" name="Text1" type="text" />
    <input id="Text2" name="Text2" type="text" />
    <input id="Checkbox1" name="Checkbox1" type="checkbox" />
    <input id="Submit1" type="submit" value="submit" />
</form>
</body>

Upvotes: 3

Jeff
Jeff

Reputation: 126

You can remove the runat="server" off of your form tag since you want to opt out of what ASP.NET gives you with server controls. You are basically thinking correctly that this should work without needing all the ASP.NET page processing bits.

It's a small change you need to make - you need to use 'name' instead of 'id' on your input controls in order for them to appear in the Form collection. It's a subtle thing but I believe it's not exclusive to ASP.NET - the name attribute specifies what to associate the value with in the POST variable collection.

Consult HTML input - name vs. id for more information on id vs. name

Good luck

Upvotes: 5

Related Questions