繁体   English   中英

OnClick 和 OnClientClick

[英]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,我的意思是刷新开启者(父)页面。上面的代码刷新正在工作。但是,父页面在刷新之前仍然显示相同的数据。原因是OnClientClickOnClick之前工作方法所以我的问题是有什么方法可以在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM