LinkBut​​ton OnCommand 或 OnClick 未触发

发布于 2024-12-10 17:10:59 字数 2682 浏览 3 评论 0原文

我正在尝试做一些看似相当简单的事情。我有一个带有多个 LinkBut​​tons 的用户控件。每个 LinkBut​​ton 都有 OnCommandCommandNameCommandArgument 设置,如下所示:

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

就其价值而言,用户控件具有AutoEventWireup="true"

在代码隐藏中,我有这样的内容:

    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());
    }

在我的生产环境(Windows Server 2008)中,当我单击链接按钮时,ChooseRegion() 不会被调用,因此没有设置 cookie。我不知道为什么。我尝试在 OnInit 中手动配置命令处理程序,并尝试使用 OnClick 而不是 OnCommand。我尝试在 LinkBut​​tons 上设置 EnableViewState="true"。到目前为止我所尝试的一切都没有成功。

然而,这在我的开发机器(Windows 7)上运行良好。

我现在相当确定,两个环境之间可能存在一些配置差异,导致了这种情况,但我真的不知道从哪里开始寻找。有想法吗?

编辑 - 2011-10-26:

好的 - 我发现我在其他地方也遇到了这个问题。包含上述代码的网站和我遇到问题的另一个网站都是共享同一个 Sitecore 实例的 Sitecore 解决方案。

在另一个站点上,有一个用于电子邮件地址的文本框和一个带有 OnClick 事件的关联 ImageButton。这一切都在网站每个页面上包含的控件中。在主页上,OnClick 处理程序永远不会触发。在任何其他页面上,OnClick 处理程序都会触发。

我上面帖子的原始部分中的代码也位于该网站的主页上。如果我将其移动到任何其他页面,它会按预期工作。

另外,就像我帖子的原始部分中的代码一样,它在我的开发计算机(Windows 7)上运行良好,但在生产计算机(Windows 2008 Server)上运行不佳。因此,这似乎与出现在主页上有关,也许 Sitecore 与该问题有关。不过我还是摸不着头脑……

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天暗了我发光 2024-12-17 17:10:59

啊哈!经过思考我上面添加的一些信息后,我意识到问题是什么,在弄清楚我的开发环境和生产机器之间的区别在于生产机器安装了 ISAPI_Rewrite 以及我没有的一整套重写规则之后没有。

其中之一将对 /default.aspx 的请求重写为 / ... 因此,当我单击 LinkBut​​ton 时,服务器拦截了 POST 请求并向 / 发出 301 重定向,然后浏览器使用 GET 请求获取 / - 所以POST 有效负载从未进入我的 ASP.NET 代码。

RewriteCond METHOD GET 添加到相关规则中解决了问题。

现在我可以让我的头发重新长出来了!

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!

浅唱ヾ落雨殇 2024-12-17 17:10:59

为什么不添加一些将错误发送到文本文件的错误日志记录功能?
那么您可以手动检查服务器上您使用的代码是否抛出任何错误?我无法通过肉眼发现代码中的任何错误,但更多的经验可能会..然而,我比其他时候更多地使用记录错误的方法。

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

独自唱情﹋歌 2024-12-17 17:10:59

所有链接按钮都应该有一个 OnClick 处理程序,如下所示:

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

并在后面的代码中执行类似以下操作:

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

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
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文