6

I am currently having an issue with a C# User Control when I post to a form - IsPostback is always set to false. The back end of my User Control checks IsPostback and redirects to another url if it is true, but because IsPostback is continuously false the page just refreshes.

A few details about the issue:

  • This is a webforms project using .NET version 3.5
  • The form is created on the Master page which contains the User Control. IsPostback is false on both the Master page and the User Control.
  • I am currently letting .NET populate the action of the form. If the form action is overridden and set to a subdirectory action the redirect fails, but if the form action is overridden and set to a specifiec page (ex - index.aspx) it redirects correctly
  • I have verified that when the form is submitted, in the Network tab of developer tools (Chrome) the request is a post
  • If the name of the page is included at the end of the url, IsPostback is set to true and the page redirects correctly (ex - Submitting the form on mysite.com will cause a refresh, but submitting on mysite.com/index.aspx will successfully redirect)
  • In the web.config file, commenting out the AmbientData HttpModule fixes the issue and the page redirects correctly (on both mysite.com and mysite.com/index.aspx)
  • The redirect works fine locally, but the AmbientData.HttpModule is commented out

Here's the form:

<form id="form1" runat="server" method="post">
      <MyForm:SubmitForm ID="ctlSubmitForm" runat="server" />
</form>

My User Control code:

<%@ Control Language="C#" AutoEventWireup="True" CodeBehind="SubmitForm.ascx.cs" Inherits="UserControls.SubmitForm" %>

<div id="SubmitForm">
    <fieldset>
        <input type="text" name="SubmitFormInput" id="SubmitFormInput" runat="server" />
        <button type="submit" title="Submit">submit</button>
    </fieldset>
</div>

And the User Control page load:

protected void Page_Load(object sender, EventArgs e)
  {
      if (IsPostBack)
      {
          // Hits here when Ambient Data HttpModule is commented out in Web.config
          Log.Debug("Submit Form Control IsPostBack");
          Response.Redirect("http://www.google.com");
      }
      else
      {
          Log.Debug("Submit Form Control NOT IsPostBack");
      }
  }

Web.config httpModules/modules sections:

<httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add name="Tridion.ContentDelivery.AmbientData.HttpModule" type="Tridion.ContentDelivery.AmbientData.HttpModule" />
      <add name="Tridion.ContentDelivery.Preview.Web.PreviewContentModule" type="Tridion.ContentDelivery.Preview.Web.PreviewContentModule" />
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>

<modules>
      <remove name="ScriptModule" />
      <remove name="WebDAVModule" />
      <remove name="FileAuthorization" />
      <remove name="UrlRoutingModule" />
      <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
      <add name="Tridion.ContentDelivery.AmbientData.HttpModule" type="Tridion.ContentDelivery.AmbientData.HttpModule" />
      <add name="PreviewContentModule" type="Tridion.ContentDelivery.Preview.Web.PreviewContentModule" />
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0,  Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</modules>

My question is: Why does removing the AmbientData HttpModule from the Web.config file fix this issue?

3
  • Can't answer the AmbientData Module question, but I would use <button id="btnSubmit" runat="server" onserverclick="btnSubmit_Click">submit</button>, and then do the redirect in the btnSubmit_Click event handler. Haven't tested it but suspecting your "client side" button tag would have something to do with the odd behaviour.
    – Hao Peng
    Apr 23, 2014 at 19:58
  • Strange. I tried your code in a simple ASPX page on a site that does have the ADF running - and I didn't have any issues. IsPostBack behaved as expected and my redirect worked fine. I didn't try to have the code in a user control - maybe that's an issue. I'm using 2013 SP1, by the way. Apr 24, 2014 at 12:38
  • Sorry didn't specify, I'm using Hotfix Rollup 2011 SP1 - 2
    – jgambino
    Apr 24, 2014 at 19:16

1 Answer 1

1

My limited understanding of how this works is as follows.

When an ASP.NET HTTP module first accesses the POST data for a particular HTTP request, the stream containing the request body is consumed, parsed into the ASP.NET request context and then disposed. From this point on, the POST data is not available to any native IIS modules, which run outside of ASP.NET.

The DefaultDocumentModule module, which is responsible for forwarding requests for extensionless URLs to the default page, is a native IIS module. It works by sending a child request through the IIS request pipeline, forwarding all of the request data.

However, if the request's POST data has already been read by a managed (ASP.NET) module (and thus the stream containing the request body has been disposed), there will be no POST data to forward. Because of this, when the child request finally reaches the page, there's no POST data.

The IsPostBack property is not based purely on HTTP verb but some arcane combination of viewstate and other things that ASP.NET deems relevant. No POST data means no IsPostBack.

So, the Ambient Data Module presumably accesses the request's POST data or performs some other action that causes the request body to be read and the stream to be disposed.

In theory, then, you should be able to correct this by adding preCondition="managedHandler" to the module's add element; because requests for extensionless URLs aren't treated as being requests for ASP.NET resources, this should mean that the Ambient Data Module doesn't run for the first request, so the DefaultDocumentModule can successfully forward the POST data to the child request where your page's code actually runs.

As such, this issue isn't specific to the Ambient Data Module but any .NET module that accesses the body of the request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.