c# - OnClick and OnClientClick - Stack Overflow

I have an image button on a pop up page which is opened by another page<asp:ImageButton ID="But

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

<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.

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?

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

<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.

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?

Share Improve this question edited Jun 1, 2011 at 8:13 Bastardo asked May 30, 2011 at 11:28 BastardoBastardo 4,1529 gold badges43 silver badges60 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3
<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. 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

You can put your javascript inside a PlaceHolder tag which you make visible in your server-side OnClick handler.

aspx code:

<asp:PlaceHolder id="refreshScript" visible="false" runat="server">
  window.opener.location.href = window.opener.location.href;
  window.close();
</asp:PlaceHolder

cs code:

protected void button_Click(Object sender, EventArgs e) {
  // do whatever
  refreshScript.Visible = true;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744262190a4565689.html

相关推荐

  • c# - OnClick and OnClientClick - Stack Overflow

    I have an image button on a pop up page which is opened by another page<asp:ImageButton ID="But

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信