onclick后如何调用onclientclick

时间:2013-07-03 12:25:34

标签: c# jquery asp.net onclick onclientclick

我有这个按钮

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

在'OnClick'事件中,我正在组装一个我需要设置为asp的URL:超链接,在'OnClick'结束时,我将此URL设置为'asp的'NavigateURL'属性:超链接”。一旦'asp:Hyperlink'具有正确的URL,我需要调用'clickHyperlink()'函数。

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

但是'OnClientClick'事件总是在'OnClick'之前执行。有关解决方法的任何建议吗?

我正在做所有这些事情,因为我遇到了JQuery Mobile和'Response.Redirect(url);'的问题。正在更改页面,但不是URL。

4 个答案:

答案 0 :(得分:2)

我相信你并不需要在JS部分中涉及Hyperlink控件。 修改您的JS函数并从OnClientClick按钮中删除btnReviewDocs属性:

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

在服务器上,使用btnReviewDocs_Click方法:

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

答案 1 :(得分:1)

使用RegisterStartupScript对象中的ClientScript在回发后运行代码---&gt;

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

答案 2 :(得分:0)

试试这个

    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
    }

答案 3 :(得分:0)

答案由@Alex Filipovici提及。

但首先你应该问问自己,你真的需要回到客户端进行重定向吗?

为什么不打电话:

Response.Redirect("MyURL");