javascript OnClick 和 OnClientClick
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6175695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
OnClick and OnClientClick
提问by Bastardo
I have an image button on a pop up page which is opened by another page
我在由另一个页面打开的弹出页面上有一个图像按钮
<asp:ImageButton
ID="Button_kalem_islemikaydet"
runat="server"
CausesValidation="False"
ImageUrl="~/images/butonlar/buyuk/Kaydet.jpg"
meta:resourcekey="Button_kalem_islemikaydetResource1"
OnClick="Button_ust_islemikaydet_Click"
OnClientClick="f2()"
Width="100" />
f2()
is
f2()
是
<script type="text/javascript">
function f2() {
opener.document.getElementById("TextBox1").value = "hello world";
opener.document.getElementById("HiddenField1").value = "hello world";
window.opener.location.href = window.opener.location.href;
}
</script>
And Button_ust_islemikaydet_Click
is another method implemented in aspx.cs file and it updates the database tables which are shown in the parent page in a GridView.
并且Button_ust_islemikaydet_Click
是在 aspx.cs 文件中实现的另一种方法,它更新显示在 GridView 父页面中的数据库表。
What I am trying to do is to doPostBack I mean refresh the opener(parent) page.And with these above codes refresh is working.However, parent page still shows the same data before the refresh.And the reason is that OnClientClick
works before OnClick
methodSo my question is that is there any way I can run the method on OnClick
and finish it and then run the OnClientClick
method?
我想要做的是 doPostBack 我的意思是刷新开启者(父)页面。上面的这些代码刷新是有效的。但是,父页面在刷新之前仍然显示相同的数据。原因是OnClientClick
在OnClick
方法之前有效所以我的问题是有什么方法可以运行该方法OnClick
并完成它然后运行该OnClientClick
方法?
回答by Yuriy Rozhovetskiy
<form id="aspnetForm" runat="server">
<asp:Button Text="Click Me" ID="ClickMeButton" OnClick="ClickMeButton_OnClick" runat="server" />
<asp:HiddenField runat="server" ID="UpdateOpenerHiddenField" Value="false" />
<script type="text/javascript">
//1st approach
var updateOpenerField = window.document.getElementById("<%= UpdateOpenerHiddenField.ClientID %>");
if (updateOpenerField.value === "true") {
f2();
updateOpenerField.value = "false";
}
// for the 2nd approach just do nothing
function f2() {
alert("Hello, opener!");
}
</script>
</form>
protected void ClickMeButton_OnClick(object sender, EventArgs e)
{
//1st approach
UpdateOpenerHiddenField.Value = "true";
// 2nd approach
ClientScript.RegisterStartupScript(this.GetType(), "RefreshOpener", "f2();", true);
}
回答by vityanya
No, you can't run server side code (OnClick event handler) before client side. OnCLientClick event was added to perform some validation before post back. There is only one way to do it - update the f2 method and post data on server via ajax
不,您不能在客户端之前运行服务器端代码(OnClick 事件处理程序)。添加了 OnCLientClick 事件以在回发之前执行一些验证。只有一种方法 - 更新 f2 方法并通过 ajax 在服务器上发布数据
回答by Ray
You can put your javascript inside a PlaceHolder tag which you make visible in your server-side OnClick handler.
您可以将您的 javascript 放在 PlaceHolder 标记中,该标记在您的服务器端 OnClick 处理程序中可见。
aspx code:
aspx代码:
<asp:PlaceHolder id="refreshScript" visible="false" runat="server">
window.opener.location.href = window.opener.location.href;
window.close();
</asp:PlaceHolder
cs code:
cs代码:
protected void button_Click(Object sender, EventArgs e) {
// do whatever
refreshScript.Visible = true;
}