3

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 ...

Jeremy Clifton
  • 533
  • 5
  • 17
  • Have you posted all the code in question or did you reduce it down? If reduced, did you make sure that this code still reproduces the problem? Please try FireBug > Console window to see if a GET/POST request is being generated. – P.Brian.Mackey Oct 23 '11 at 16:58
  • This is in essence exactly the code I have. ChooseRegion() is exactly as it is in my code, and there isn't much else to the HTML/ASP.NET front-side code. I did check and I'm seeing a POST request as I would expect. – Jeremy Clifton Oct 25 '11 at 12:50

3 Answers3

2

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!

Jeremy Clifton
  • 533
  • 5
  • 17
1

why dont you add some error logging functionality that sends errors to a text file ? then you can manually check if on the server the code you are using throws any errors ?? I cant find by the eye any mistake in your code, but some some more experiences might .. I however use the method of logging errors for times like these more than other times ..

a7mad.3ezz
  • 101
  • 1
  • 6
1

All your Link Buttons should have a handler for OnClick as below:

<asp:LinkButton ID="lbAsia" OnClick="LinkButton_Click" OnCommand="ChooseRegion" CommandName="SetCookie" CommandArgument="Asia" runat="server">Asia</asp:LinkButton>

And in code behind you do something like:

  protected void LinkButton_Click(Object sender, EventArgs e) 
  {
     string commandArgument = (sender as LinkButton).CommandArgument;
     string commandName =(sender as LinkButton).CommandName;

    if(commandName == "something")
    {
        //Do something
       //and so on
    }
  }
Icarus
  • 63,293
  • 14
  • 100
  • 115
  • 1
    I tried changing the code as you outlined above. The resulting code is still not working, with the LinkButton_Click function not ever getting called. – Jeremy Clifton Oct 25 '11 at 12:51
  • 2
    Also - I'm a little confused - I've used OnCommand + CommandName + CommandArgument without an OnClick handler in several instances before without an issue, and the function specified by OnCommand gets called. I thought that's how I was supposed to be doing it, but is it only supposed to work in conjunction with OnClick? – Jeremy Clifton Oct 25 '11 at 14:15