Click here to Skip to main content
15,867,686 members
Articles / Web Development / HTML

That Annoying ASP.NET Issue with Postback

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
9 Jun 2014Ms-PL2 min read 9.5K   8  
Arrgh!  I've got an annoying issue in ASP.NET postback: it's not entering the event handler for my button click at all. Read on to learn how I solved this annoying issue.

Stop me if you've heard this before:

annoying.jpg

I'm working through an ASP.NET project and suddenly Postback stops working. I know! This is the simplest of interactions with an ASP.NET web forms application, and it's not WORKING!! What the heck is wrong with this stupid website? I mean, I didn't change anything. I just want my default.aspx form to be able to have a button click button handler. Is that so DIFFICULT? Get your act together FRITZ!1!!

I did some digging and digging on this one. Did I bring in an add-in or library to my project that was preventing the postback from capturing the click event? I broke out Fiddler and analyzed the traffic, to ensure that the form content was indeed being submitted properly back to the server. Everything looked good there.

My next analysis step was to take a look at the Request.Form collection. I should see the __EVENTARGUMENT being populated. When I opened the Immediate Window and inspected Request.Form, this is what I found:

The Visual Studio Immediate Window when I inspected Request.Form

The Visual Studio Immediate Window when I inspected Request.Form.

How is the Request.Form collection empty? What's the deal with THAT?

I started thinking about the ASP.NET pipeline, and it hit me: FriendlyUrls.

There's this interesting thing that happens when web forms attempt to post back to pages managed by FriendlyUrls and lack a filename. This could be any of the following pages in your site:

  • /
  • /Products/
  • /Orders/

You get the idea. These pages for some reason don't handle postback properly. With that in mind, I set forth to drop a quick and dirty change to enable this throughout the website. Fortunately, I can make that change through a URL Rewrite operation. I added the following event handler to my global.asax.cs file:

C#
void Application_BeginRequest(object sender, EventArgs e)
{
  var app = (HttpApplication)sender;
  if (app.Context.Request.Url.LocalPath.EndsWith("/"))
  {
    app.Context.RewritePath(
             string.Concat(app.Context.Request.Url.LocalPath, "default"));
  }
}

With that, my pages started posting back properly... all was right in the world.

Please note: You will have this problem with the default ASP.NET project with web forms when FriendlyUrls are enabled.

This article was originally posted at http://feeds.feedburner.com/CsharpOnTheFritz

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Program Manager
United States United States
Jeff Fritz is a senior program manager in Microsoft’s Developer Division working on the .NET Community Team. As a long time web developer and application architect with experience in large and small applications across a variety of verticals, he knows how to build for performance and practicality. Four days a week, you can catch Jeff hosting a live video stream called 'Fritz and Friends' at twitch.tv/csharpfritz. You can also learn from Jeff on WintellectNow and Pluralsight, follow him on twitter @csharpfritz, and read his blog at jeffreyfritz.com

Comments and Discussions

 
-- There are no messages in this forum --