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

本秂侑毒 提交于 2019-11-29 15:21:15

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

Iman Abidi

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!