简体   繁体   中英

How to call onclientclick after onclick

I've got this button

<asp:Button runat="server" ID="btnReviewDocs" CssClass="btnReviewDocs" data-theme="b"
                Text="Review Documents" OnClick="btnReviewDocs_Click" OnClientClick="clickHyperlink();"/>

And in 'OnClick' event I'm assembling an URL that I need to set to asp:Hyperlink and at the end of the 'OnClick' I'm setting this URL to the 'NavigateURL' propery of the 'asp:Hyperlink'. Once the 'asp:Hyperlink' has the correct URL I need to call the 'clickHyperlink()' function.

function clickHyperlink() {
    var href = $('#hlnkID').attr('href');
    if (typeof href !== "undefined") {
        $.mobile.showPageLoadingMsg();
        window.location.href = href;
    }
}

But the 'OnClientClick' event is executed always before the 'OnClick'. Any suggestions for a workaround?

I'm doing all this stuff, because I've got problems with JQuery Mobile and 'Response.Redirect(url);' is changing the page, but not the URL.

I believe that you don't really need to involve the Hyperlink control in the JS part. Modify your JS function and remove the OnClientClick attribute from the btnReviewDocs button:

<script type="text/javascript">
    function clickHyperlink(href) {
        $.mobile.showPageLoadingMsg();
        window.location.href = href;
    }
</script>

On the server, in the btnReviewDocs_Click method:

protected void btnReviewDocs_Click(object sender, EventArgs e)
{
    // TODO: set the url, maybe append some params to the 
    // hlnkID.NavigateUrl value
    var url = "http://stackoverflow.com/";
    ClientScript.RegisterStartupScript(Page.GetType(), 
        "clickHyperlink",
        "clickHyperlink('" + url + "');",
        true);
}

Use the RegisterStartupScript in the ClientScript object to run the code after postback--->

protected void btn_Click(object sender, EventArgs e)
    { 
        //some code    
        this.ClientScript.RegisterStartupScript(this.GetType(), "clintClick", "clickHyperlink", true);
    }

try this

    protected void btnReviewDocs_Click(object sender, EventArgs e)
    { 
        //something doing here

        Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "<script type='text/javascript'>clickHyperlink()</script>");//call javascript function
    }

The answer is mentioned by @Alex Filipovici.

But first you should ask yourself do you really need to go back to the client side to do a redirect ?

Why not call :

Response.Redirect("MyURL");

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