1

我有这个按钮

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

在“OnClick”事件中,我正在组装一个需要设置为 asp:Hyperlink 的 URL,并在“OnClick”结束时将此 URL 设置为“asp:Hyperlink”的“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

4 回答 4

2

我相信你真的不需要Hyperlink在JS部分中涉及控件。修改您的 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);
}
于 2013-07-03T12:38:49.677 回答
1

回发后使用RegisterStartupScriptinClientScript对象运行代码--->

protected void btn_Click(object sender, EventArgs e)
    { 
        //some code    
        this.ClientScript.RegisterStartupScript(this.GetType(), "clintClick", "clickHyperlink", true);
    }
于 2013-07-03T12:33:48.120 回答
0

@Alex Filipovici 提到了答案。

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

为什么不打电话:

Response.Redirect("MyURL");
于 2013-07-03T12:49:17.810 回答
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
    }
于 2013-07-03T12:35:17.543 回答