简体   繁体   English

ASP-OnServerClick无法正常工作

[英]ASP - OnServerClick Doesn't work properly

I'm using ASP.NET with C# 4.0. 我在C#4.0中使用ASP.NET。
My problem is that I want only one button to activate a certain method on server. 我的问题是我只需要一个按钮即可激活服务器上的某种方法。
What do i mean? 我是什么意思 I'm glad you asked. 我很高兴你问。
I have 2 buttons. 我有2个按钮。 “过滤器”和“ FetchEvents”
Any my aspx code is: 我的任何aspx代码为:

 <form id="form2" runat="server">
     <div class="box-generic">
         <div class="form-group well" style="">
            <input type="submit" name="button_filter" value="filter" class="btn btn-primary" />
            <input type="button" id="all_events_button" name="all_events_button" value="All Events CSV" class="btn btn-primary" OnServerClick="downloadAllEvents" runat="server"/>  
         </div>
     </div>
 </form>

If I press the "filter" button it works awesome. 如果我按下“过滤器”按钮,它就很棒。
Whenever I press the "All Events CSV" button, it works great. 每当我按下“所有事件CSV”按钮时,它都很好用。

Scenario 情境

1. Clicking on All Events. 1.单击所有事件。 (Activates the all events button normally) (正常激活所有事件按钮)
2. Clicking on Filter. 2.单击过滤器。

downloadAllEvents() Method is being activated. downloadAllEvents()方法正在激活。 BAD BAD BAD 坏坏坏

And the weirdest part about it is that this method will be activated through filter only after it has been activated through AllEvents. 最奇怪的是,此方法仅通过AllEvents激活才能通过过滤器激活。

EDIT: 编辑:

I'm currently checking inside the method that the call didn't came from the filter button. 我目前正在检查该方法不是来自筛选器按钮的方法内部。
not a pretty sight. 不太漂亮。

Haven't found someone with the same problem. 尚未找到遇到相同问题的人。
Thanks in advance :) 提前致谢 :)

I did a below sample page, and its working perfectly... can you POST the full page ... ? 我做了一个下面的示例页面,它工作正常...您可以发布整页...吗?

Try this code and tell me what exactly is happening: 试试下面的代码,告诉我到底发生了什么:

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Am Load<br/>");
    }

    protected void downloadAllEvents(object sender, EventArgs e)
    {
        Response.Write("Am Download");
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form2" runat="server">
        <div class="box-generic">
            <div class="form-group well" style="">
                <input type="text" name="x" />
                <input type="submit" name="button_filter" value="filter" class="btn btn-primary" />
                <input type="button" id="all_events_button" name="all_events_button" value="All Events CSV" class="btn btn-primary" onserverclick="downloadAllEvents" runat="server" />
            </div>
        </div>
    </form>
</body>
</html>

Not the answer I was hoping for, BUT it works and it works good. 不是我希望的答案, 但是它有效并且很好。 (Bugless) (无聊)
In my downloadAllEvents() I check who sent the post request and I deny the post request from anyone who is not all_events_button 在我的downloadAllEvents()我检查谁发送了发帖请求,并且拒绝了不是all_events_button任何人的发帖请求

By doing so i transfer the responsibility from the sender to the listener. 通过这样做,我将责任从发送者转移到了听者。 If anyone has better answer please provide it. 如果有人有更好的答案,请提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM