Why is my Asp.Net Form arriving empty when I post from Page to Page?
Asked Answered
T

4

4

I have the following HTML Code

<%@ Page Language="C#" %>

<html>
    <head>
        <title></title>
    </head>
    <body>
        <form id="frmSystem" method="post" action="target.aspx">
            <input id="txtTextField" type="text" />
            <input id="btnPost" value="Submit" onclick="javascript:frmSystem.submit();" type="button" />
        </form>
    </body>
</html>

The target Page is coming up but the form that it is receiving is empty. I have a break point on my target.aspx page and while I can see a form, it's keys are empty and Request["txtTextField"] gives me nothing.

Any clue why?

Thinking answered 12/3, 2010 at 15:12 Comment(3)
MVC or webforms? Looks like you are trying to use MVC.Betook
What does your target form code look like? where is your breakpoint?Ringent
For now all I have in my target is this <%@ Page Language="C#" %> <% string text = ""; %> and I'm obviously breaking on the only line of code. I use Watches to verify what I'm getting.Thinking
B
5

If you are using ASP.NET MVC, the input names need to be set with the "name" attribute rather than "id".

Betook answered 12/3, 2010 at 15:18 Comment(3)
I'm using webforms. No code behind, just a dll to handle some database access and the rest is scripts.Thinking
Thanks a lot actually, that was the solution.Thinking
You can use the name rather than id attribute as well as in the Asp.net web application or webforms too.Demisemiquaver
G
2

You probably reset the form value in event handlers (such as page_load).

Gibe answered 4/6, 2013 at 10:41 Comment(1)
I usually blame users for stupid conducts, now the stupid was me. Thanks @GibeArmistice
O
1

If you are using ASP.NET 4.5 like me, use below hints

  • Disable auto Friendly URLs in a Web Form project
    • settings.AutoRedirectMode = RedirectMode.Off; // in RouteConfig.cs
  • if you can:
    • remove action="CILandingPage.aspx" from form element
    • put a asp:button instead of normal button
    • set PostBackUrl="~/CILandingPage.aspx" on your asp:button

more resources with more detail that was useful to me

Oncoming answered 17/11, 2016 at 8:40 Comment(1)
Importantly noted in your first link, you can set settings.AutoRedirectMode = RedirectMode.Off inside RouteConfig.cs . It is not necessary to disable auto friendly URLs entirely.Zosi
Z
0

Another option is to capture your Request.Form[] data in Application_BeginRequest on the Global.asax.cs:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    //capture form data and preserve in query string
    if (Request.Form["txtTextField"]!= null)
    {
        Response.Redirect(Request.RawUrl + "?txtTextField=" 
          + Request.Form["txtTextField"]);
    }
    //or preserve in Session variable
    if(Request.Form["txtTextField"]!=null)
    {
        Session["txtTextField"]=Request.Form["txtTextField"];
    }
}

The problem is that the form data is lost on the automatic redirect which is applied by friendlyUrls - if you store that data as something other than form data, it is unnecessary to turn off friendlyUrls or set AutoRedirectMode to RedirectMode.Off.

Zosi answered 18/1, 2017 at 21:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.