Click here to Skip to main content
13,045,180 members (40,350 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

5.9K views
8 bookmarked
Posted 9 Jun 2014

That Annoying ASP.NET Issue with Postback

, 9 Jun 2014
Rate this:
Please Sign up or sign in to vote.
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:

https://www.codeproject.com/KB/asp/784156/b6dd41e3-edc6-4701-a6bd-91516a0de716.Jpeg

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:

https://www.codeproject.com/KB/asp/784156/6038696b-44e9-4b89-bc7c-fbeba36a2288.Png

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:

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.

License

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

Share

About the Author

Jeffrey T. Fritz
Program Manager
United States United States
Jeffrey is a software developer coach, architect, and speaker in the Microsoft.Net community. He currently works as a program manager for the Microsoft .NET Developer Outreach group. He has delivered training videos on Pluralsight, WintellectNow, and on YouTube. Jeffrey makes regular appearances delivering keynotes, workshops, and breakout sessions at conferences such as TechEd, Ignite, DevIntersection, CodeStock, FalafelCon, VSLive as well as user group meetings in an effort to grow the next generation of software developers.

You may also be interested in...

Comments and Discussions

 
-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 10 Jun 2014
Article Copyright 2014 by Jeffrey T. Fritz
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid