简体   繁体   English

使用Asp.Net的OnClick / OnClientClick事件

[英]OnClick/OnClientClick event with Asp.Net

I am a little confused to use this script and how it actually works? 我有点困惑使用这个脚本,它实际上是如何工作的?

Here is my script: 这是我的脚本:

 <script type="text/javascript">
        $(document).ready(function () {
            $('#<%=TextBox1.ClientID%>').change(function () {
                $('#various3').attr('href', $(this).val());
            });


                $("#<%=Button1.ClientID%>").click(function (event) {
                    setTimeout(showStickySuccessToast, 1000);
                    function showStickySuccessToast() {
                        $().toastmessage('showToast', {
                            text: 'Finished taking Screenshot!',
                            sticky: false,
                            position: 'middle-center',
                            type: 'success',
                            closeText: '',
                            close: function () {

                            }
                        });
                    }

                })                
        });
      </script>

This is my button: 这是我的按钮:

<a id="various3" href="#"><asp:Button ID="Button1" runat="server" 
    Text="Button" OnClientClick="Button1_Click"/></a>

This is my code behind for button1_Click event: 这是我的button1_Click事件背后的代码:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim NewTh As New Threading.Thread(AddressOf DoIT)
    NewTh.SetApartmentState(Threading.ApartmentState.STA)
    NewTh.Start()
    ImgPreview.ImageUrl = "~/uploads/Sample.jpg"
End Sub

Now if I click the button it should open the iframe and display the message which I am able to do. 现在,如果我点击按钮,它应该打开iframe并显示我能够做的消息。 Now the problem is how do I call the button1_Click event which is in the codebehind at the same time. 现在问题是我如何同时调用代码隐藏中的button1_Click事件。 I have used OnClick and OnClientClick event as shown below but it's not working and I'm not getting any errors. 我已经使用了OnClickOnClientClick事件,如下所示,但它没有工作,我没有收到任何错误。

Onclick = raises a postback, executing the server eventhandler. Onclick =引发回发,执行服务器事件处理程序。

OnClientClick = raises a client click event and you can call a javascript function but you cant handlé an asp event directly although after calling a javascript function a postback occurs. OnClientClick =引发一个客户端点击事件,你可以调用一个javascript函数,但你不能直接处理一个asp事件,虽然在调用javascript函数后发生回发。

It looks like you've got things backwards: 看起来你有倒退的事情:

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" />

The above example assumes that Button1_Click is the code-behind click event handler. 上面的示例假定Button1_Click是代码隐藏的单击事件处理程序。 Since you're assigning the client click event in jQuery, you shouldn't need to use OnClientClick at all. 由于您在jQuery中分配客户端单击事件,因此您根本不需要使用OnClientClick

EDIT 编辑

I would check to make sure that validation isn't intefering with the click. 我会检查以确保验证不会与点击相关。 You can test this by setting CausesValidation="false" on the Button. 您可以通过在Button上设置CausesValidation="false"来测试它。

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

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