简体   繁体   中英

Need to execute the OnClick event before OnClientClick

I am developing a scanning application and I need to do some validation and db updates and then scan the document.

I have tried various approaches based on searching for answers and not able to locate anything.

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

Have even tried removing the OnClientClick and executing the javascript through the onClick event. It enters the Onclick but won't execute the 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);

Any help would be greatly appreciated.

OnClientClick event runs on client-side and OnClick event runs on server-side. OnClientClick triggers first, so it's impossible to execute the OnClick event before OnClientClick .

According to your requirement, you need to run server-side code first, and then run a client-side callback. Here is my solution.

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

Note: the ScriptManager and UpdatePanel ( AJAX Extension ) are required in order to make Dynamic Web TWAIN code work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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