需要在OnClientClick之前执行OnClick事件

时间:2018-07-23 20:07:06

标签: javascript

我正在开发一个扫描应用程序,我需要进行一些验证和数据库更新,然后扫描文档。

我已经尝试过各种基于搜索答案且无法找到任何东西的方法。

                <asp:ScriptManager ID="script1" runat="server" EnablePageMethods="true"></asp:ScriptManager>
                    <asp:UpdatePanel ID="panel1" runat="server">
                        <ContentTemplate>
                             <tr><td><asp:Button ID="btnScan" runat="server" Text="Scan" OnClick="Scan" OnClientClick="return AcquireImage()" /><p></td></tr>
                         </ContentTemplate>
                    </asp:UpdatePanel>
              </table>            
          </td>        
          <td>                        
            <table width=100% >
                <tr><td>
                    <div id="dwtcontrolContainer" > </div>
                </td></tr>
            </table>  
            <script type="text/javascript">
                function AcquireImage() {
                        var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
                        DWObject.IfDisableSourceAfterAcquire = 'true';
                        DWObject.SelectSource();
                        DWObject.OpenSource();
                        DWObject.AcquireImage();
//                        var isPostBack = <%=Convert.ToString(Page.IsPostBack).ToLower()%>;
//                        alert(isPostBack);
                       return true;
                    }

甚至尝试过删除OnClientClick并通过onClick事件执行javascript。它进入Onclick,但不会执行javascript

        if (enddate.Text == "")
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "key", "alert('Please enter a Transaction date before scanning');", true);
            return;
        }

        //ScriptManager.RegisterStartupScript(this, this.GetType(), "myScript", "AcquireImage()", false);
        Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMAnotherFunction", "javascript:AcquireImage();", true);

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

OnClientClick 事件在客户端运行, OnClick 事件在服务器端运行。 OnClientClick 首先触发,因此不可能在 OnClientClick 之前执行 OnClick 事件。

根据您的要求,您需要先运行服务器端代码,然后再运行客户端回调。这是我的解决方法。

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

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="head1" runat="server">
    <title>Button.OnClientClick Example</title>
    <script type="text/javascript" src="http://tst.dynamsoft.com/libs/dwt/14.0/dynamsoft.webtwain.min.js"> </script>
</head>
<body>

    <script runat="server">

    void Button1_Click(object sender, EventArgs e)
    {
        string script = @"
        var DWObject = Dynamsoft.WebTwainEnv.GetWebTwain('dwtcontrolContainer');
        DWObject.IfDisableSourceAfterAcquire = true;
        var bSelected = DWObject.SelectSource(); 

        if(bSelected) {
            var OnAcquireImageSuccess, OnAcquireImageFailure;
            OnAcquireImageSuccess = OnAcquireImageFailure = function () {
            DWObject.CloseSource();
            };
        };

        DWObject.OpenSource();
        DWObject.AcquireImage(OnAcquireImageSuccess, OnAcquireImageFailure);  

        ";
        bool isTrue = true;
        if (isTrue)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "key", script, true);
        }


    }

</script>
    <form id="form1" runat="server">

        <h3>Dynamsoft Dynamic Web TWAIN Demo</h3>

        <p></p>

    <div id="dwtcontrolContainer"></div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <asp:Button ID="Button1" runat="server" CausesValidation="False" OnClick="Button1_Click" Text="Scan Documents" UseSubmitBehavior="False" />
            </ContentTemplate>
        </asp:UpdatePanel>


    </form>

    </body>
</html>

注意:要使Dynamic Web TWAIN代码正常工作,需要 ScriptManager UpdatePanel AJAX扩展)。