简体   繁体   English

OnClick 和 OnClientClick

[英]OnClick and OnClientClick

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 method So my question is that is there any way I can run the method on OnClick and finish it and then run the OnClientClick method?我要做的是做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);
    }

No, you can't run server side code (OnClick event handler) before client side.不,您不能在客户端之前运行服务器端代码(OnClick 事件处理程序)。 OnCLientClick event was added to perform some validation before post back.添加了 OnCLientClick 事件以在回发之前执行一些验证。 There is only one way to do it - update the f2 method and post data on server via ajax只有一种方法可以做到 - 更新 f2 方法并通过 ajax 在服务器上发布数据

You can put your javascript inside a PlaceHolder tag which you make visible in your server-side OnClick handler.您可以将 javascript 放在 PlaceHolder 标记中,您可以在服务器端 OnClick 处理程序中显示该标记。

aspx code: asp代码:

<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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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