[英]OnClick and OnClientClick
我在另一個頁面打開的彈出頁面上有一個圖像按鈕
<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()
是
<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>
Button_ust_islemikaydet_Click
是在 aspx.cs 文件中實現的另一種方法,它更新 GridView 中父頁面中顯示的數據庫表。
我要做的是做PostBack,我的意思是刷新開啟者(父)頁面。上面的代碼刷新正在工作。但是,父頁面在刷新之前仍然顯示相同的數據。原因是OnClientClick
在OnClick
之前工作方法所以我的問題是有什么方法可以在OnClick
上運行該方法並完成它然后運行OnClientClick
方法?
<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);
}
不,您不能在客戶端之前運行服務器端代碼(OnClick 事件處理程序)。 添加了 OnCLientClick 事件以在回發之前執行一些驗證。 只有一種方法可以做到 - 更新 f2 方法並通過 ajax 在服務器上發布數據
您可以將 javascript 放在 PlaceHolder 標記中,您可以在服務器端 OnClick 處理程序中顯示該標記。
asp代碼:
<asp:PlaceHolder id="refreshScript" visible="false" runat="server">
window.opener.location.href = window.opener.location.href;
window.close();
</asp:PlaceHolder
cs代碼:
protected void button_Click(Object sender, EventArgs e) {
// do whatever
refreshScript.Visible = true;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.