[英]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'. 在'OnClick'事件中,我正在组装一个我需要设置为asp的URL:超链接,在'OnClick'结束时,我将此URL设置为'asp:Hyperlink'的'NavigateURL'属性。 Once the 'asp:Hyperlink' has the correct URL I need to call the 'clickHyperlink()' function. 一旦'asp:Hyperlink'具有正确的URL,我需要调用'clickHyperlink()'函数。
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'. 但是'OnClientClick'事件总是在'OnClick'之前执行。 Any suggestions for a workaround? 有关解决方法的任何建议吗?
I'm doing all this stuff, because I've got problems with JQuery Mobile and 'Response.Redirect(url);' 我正在做所有这些事情,因为我遇到了JQuery Mobile和'Response.Redirect(url);'的问题。 is changing the page, but not the URL. 正在更改页面,但不是URL。
I believe that you don't really need to involve the Hyperlink
control in the JS part. 我相信你真的不需要在JS部分中涉及Hyperlink
控件。 Modify your JS function and remove the OnClientClick
attribute from the btnReviewDocs
button: 修改您的JS函数并从btnReviewDocs
按钮中删除OnClientClick
属性:
<script type="text/javascript">
function clickHyperlink(href) {
$.mobile.showPageLoadingMsg();
window.location.href = href;
}
</script>
On the server, in the btnReviewDocs_Click
method: 在服务器上,在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);
}
Use the RegisterStartupScript
in the ClientScript
object to run the code after postback---> 使用ClientScript
对象中的RegisterStartupScript
在回发后运行代码--->
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. 答案由@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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.