LinkButton OnCommand or OnClick Not Firing

Jeremy Clifton picture Jeremy Clifton · Oct 20, 2011 · Viewed 7.9k times · Source

I'm trying to do something that seems to be fairly simple. I have a user control with several LinkButtons. Each LinkButton has OnCommand, CommandName and CommandArgument set, like so:

<ul id="continents">
    <li id="northamerica"><asp:LinkButton ID="lbNorthAmerica" OnCommand="ChooseRegion" CommandName="SetCookie" CommandArgument="North America" runat="server">North America</asp:LinkButton></li>
    <li id="southamerica"><asp:LinkButton ID="lbSouthAmerica" OnCommand="ChooseRegion" CommandName="SetCookie" CommandArgument="South America" runat="server">South America</asp:LinkButton></li>
    <li id="asia"><asp:LinkButton ID="lbAsia" OnCommand="ChooseRegion" CommandName="SetCookie" CommandArgument="Asia" runat="server">Asia</asp:LinkButton></li>
    <li id="australia"><asp:LinkButton ID="Australia" OnCommand="ChooseRegion" CommandName="SetCookie" CommandArgument="Australia" runat="server">Australia</asp:LinkButton></li>
    <li id="africa"><asp:LinkButton ID="lbAfrica" OnCommand="ChooseRegion" CommandName="SetCookie" CommandArgument="Africa" runat="server">Africa</asp:LinkButton></li>
    <li id="europe"><asp:LinkButton ID="lbEurope" OnCommand="ChooseRegion" CommandName="SetCookie" CommandArgument="Europe" runat="server">Europe</asp:LinkButton></li>
</ul>

For what it's worth, the user control has AutoEventWireup="true".

In the codebehind, I have this:

    protected void ChooseRegion(object sender, CommandEventArgs e)
    {
        Response.Cookies["region"].Value = e.CommandArgument.ToString();
        Response.Cookies["region"].Expires = DateTime.Now.AddDays(90);
        SendToRegion(e.CommandArgument.ToString());
    }

In my production environment (Windows Server 2008), ChooseRegion() is not getting called when I click on the link buttons, and thus no cookie is being set. I'm at a loss to figure out why. I've tried manually configuring the Command handlers in OnInit and tried using OnClick instead of OnCommand. I tried setting EnableViewState="true" on the LinkButtons. Nothing I've tried thus far has worked.

However, this works fine on my development machine (Windows 7).

I feel fairly certain at this point that there is probably some configuration difference between the two environments that is causing this, but I'm really not sure where to start looking. Ideas?

Edit - 2011-10-26:

OK - I've discovered I'm having this problem elsewhere as well. The site with the above code and the other site where I'm having the issue are both Sitecore solutions sharing the same Sitecore instance.

On the other site, there is an textbox for an email address and an associated ImageButton with an OnClick event. This is all in a control that is being included on every page on the site. On the home page, the OnClick handler never fires. On any other page, the OnClick handler DOES fire.

The code in the original part of my post above is also on the home page of the site. If I move it to any other page, it works as expected.

Also, like the code in the original part of my post, it works fine on my development machine (Windows 7) but not on the production machines (Windows 2008 Server). So, it would seem that this seems to have something to do with being on the home page, and perhaps Sitecore is involved in the problem somehow. I'm still scratching my head, though ...

Answer

Jeremy Clifton picture Jeremy Clifton · Oct 26, 2011

Aha! After thinking through the bit of information I added above, I realized what the issue was, after figuring out that the difference between my development environment and the production machines is that the production machines have ISAPI_Rewrite installed and a whole soup of rewrite rules that I don't have.

One of them rewrites requests to /default.aspx to / ... so when I was clicking on the LinkButton, the server intercepted the POST request and issued a 301 redirect to /, and then the browser fetched / with a GET request - so the POST payload never made it to my ASP.NET code.

Adding RewriteCond METHOD GET to the rule in question solved the problem.

Now I can let my hair grow back out again!