简体   繁体   English

asp:Button Click事件不会触发

[英]asp:Button Click Event doesn't fire

I am having a problem with this website I am currently developing. 我目前正在开发的网站有问题。 I am trying to fire a button's Click Event after a series of codes in an external js file. 我试图在外部js文件中的一系列代码后触发按钮的Click事件。 please see my codes below: 请在下面查看我的代码:

.ASPX: .ASPX:

<script type="text/javascript">
    var btn = $("#<%=btnRefresh.ClientID%>");
</script>

...

<asp:Button ID="btnRefresh" name="btnRefresh" runat="server" Visible="false" Text="btnRefresh" onclick="btnRefresh_Click" />

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
    <asp:GridView ID="myGrd" runat="server">...</GridView>
    <Triggers>
       <asp:AsyncPostBackTrigger ControlID="btnRefresh" EventName="Click" />
    </Triggers>
<asp:UpdatePanel>

.ASPX.CS: .ASPX.CS:

protected void btnRefresh_Click(object sender, EventArgs e)
{
    myGrd.DataSource = null;
    myGrd.DataSource = GetData();
    myGrd.DataBind();
}

.JS: .JS:

function Refresh() {
    if (btn) {
        btn.click();
    }
    else {
        alert('false');
    }
}

Basically, what I want to achieve is this: 基本上,我想要实现的是:

  1. Be able to implement my Add, Edit, Delete on the Page. 能够在页面上实现我的添加,编辑,删除。 (Functioning now) (正在运行)
  2. Call the methods in an external JS file to show the popup boxes for Add, Edit, Delete. 调用外部JS文件中的方法以显示“添加”,“编辑”,“删除”的弹出框。
  3. Once successful, call the Refresh() method and refresh the gridview only. 成功后,调用Refresh()方法并仅刷新gridview。
  4. Refresh() method consumes the btnRefresh_Click event from the .aspx.cs page. Refresh()方法使用.aspx.cs页中的btnRefresh_Click事件。

I just used the button for that purpose since I don't know what other options I have. 我只是将按钮用于此目的,因为我不知道我还有其他选择。

Now my problem is this. 现在我的问题是这个。 My code was able to declare my btn variable from the .aspx page. 我的代码能够从.aspx页声明我的btn变量。 When it does inside the external .js file, here is how my btn variable looks like: 当它在外部.js文件中运行时,这是我的btn变量的样子:

在此处输入图片说明

The compiler goes through the btn.click() line which is supposed to fire the btnRefresh_Click event from the code-behind. 编译器通过btn.click()行,该行btnRefresh_Click后面的代码中触发btnRefresh_Click事件。 However, it doesn't fire up. 但是,它不会启动。

Am I missing anything here? 我在这里想念什么吗? Please help me. 请帮我。 I've been stuck here for hours. 我已经在这里呆了几个小时。

Have you tried finding your btn object when you need it and after the document is fully loaded, instead of getting it before your document is fully loaded? 您是否尝试过在需要时以及在文档完全加载之后找到btn对象,而不是在文档完全加载之前找到它?

changing: 改变:

<script type="text/javascript">
    var btn = $("#<%=btnRefresh.ClientID%>");
</script>

to

<script type="text/javascript">
    var btnid = "#<%=btnRefresh.ClientID%>";
</script>

and your js: 和你的js:

   function Refresh() {
     var btn  = $(btnid);
        if (btn) {
            btn.click();
        }
        else {
            alert('false');
        }
    }

You are setting the button Visible="false" 您正在设置按钮Visible="false"

<asp:Button ID="btnRefresh" name="btnRefresh" runat="server" Visible="false"

Setting this will not render the button on the screen.. It will completely remove the element from the markup .So no events can be attached to it. 设置此选项将不会在屏幕上呈现按钮 。它将完全从标记中删除该元素。因此无法将任何事件附加到该元素。 Instead of this try setting s**tyle="display:none" .. Doing so will render the button on the screen but will not display it.. Also the events will be attached to the element.. 代替此尝试,请设置s**tyle="display:none" ..这样做将在屏幕上呈现按钮,但不会显示该按钮。事件也将附加到元素。

UPDATED ::: Try accessing your btn in the Document.Ready function.. This makes sure you are adding the button once it is available in the DOM** 更新:::尝试在Document.Ready函数中访问您的btn。这确保一旦按钮在DOM中可用,就添加该按钮**

<script type="text/javascript">
   $(function() {
       var btn = $("#<%=btnRefresh.ClientID%>");
    });
</script>

If possible can you provide the whole markup and the required code.. Where are you calling the refresh function 如果可能的话,您可以提供整个标记和所需的代码。在哪里调用刷新功能

I'd go for the simplest thing - and the issue is not making the button invisible, but having an invisible button, that one can click on from javascript, and having the button call it's event handler. 我会选择最简单的方法-问题不是使按钮不可见,而是使按钮具有不可见的功能,使人可以从javascript单击,并使该按钮称为事件处理程序。 I have the same hack as the OP: make the button permanently invisible by setting it's style=display:none;. 我和OP具有相同的技巧:通过将按钮设置为style = display:none;使其永久不可见。 In that way the button exists on the client page, my JS clicks it, and then the buttons eventhandler on the server gets called on postback. 这样,按钮存在于客户端页面上,我的JS单击它,然后在回发时调用服务器上的按钮事件处理程序。 This didnt work properly in Firefox until I set UseSubmitBehavior=False . 在我设置UseSubmitBehavior=False之前,这在Firefox中UseSubmitBehavior=False正常工作。 Now it works in IE and FF. 现在,它可以在IE和FF中使用。

I suppose that what you are seeing at client side onclick is not the one you are declaring at server side OnClick. 我想您在客户端onclick看到的不是您在服务器端OnClick声明的。

onclick on client side is equal to OnClientClick at server side. 客户端的onclick等于服务器端的OnClientClick。

Update: 更新:

You can still call the server side click function using btn.click(); 您仍然可以使用btn.click()调用服务器端单击函数。 (same as you are using), but to achieve this you must intially set your control's Visible property to true. (与您使用的相同),但是要实现此目的,必须首先将控件的Visible属性设置为true。 when you don't set it to true the control will even not render to the client and does no state will be maintained for it. 如果您未将其设置为true,则该控件甚至不会呈现给客户端,并且不会为其维护任何状态。

If you don't want to show button intially, instead of settting Visible = "False" you can write Style="visibility:hidden"; 如果您不想最初显示按钮,则可以将Style =“ visibility:hidden”;设置为Visible =“ False”;

So, your aspx markup for button should look like this 因此,按钮的aspx标记应如下所示

<asp:Button ID="btnRefresh" name="btnRefresh" runat="server" Style="visibility:hidden" Text="btnRefresh" onclick="btnRefresh_Click" />

Change the AsynPostBackTrigger for PostBackTrigger this should work 更改AsynPostBackTriggerPostBackTrigger,这应该工作

 <asp:Button ID="btnRefresh" name="btnRefresh" runat="server" Visible="false"    Text="btnRefresh" onclick="btnRefresh_Click" />

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<asp:GridView ID="myGrd" runat="server">...</GridView>
<Triggers>
   <asp:PostBackTrigger ControlID="btnRefresh" EventName="Click" />
</Triggers>

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

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