This is a migrated thread and some comments may be shown as answers.

Close window with code behind

4 Answers 932 Views
Window
This is a migrated thread and some comments may be shown as answers.
William
Top achievements
Rank 1
William asked on 30 Sep 2011, 06:38 AM
I'm using your demo code from
http://demos.telerik.com/aspnet-ajax/window/examples/dialogreturnvalue/defaultvb.aspxand would like to put the button call that closes the window into code.
change this:

<div style="margin-top: 4px; text-align: right;">

 <button title="Submit" id="close" onclick="returnToParent(); return false;">

 Submit</button>

 </div>

into this:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

      Button1.Attributes("onclick") = "return returnToParent(); return false;"

  End Sub

But it takes two clicks to call the function.  All help is apprecieted

4 Answers, 1 is accepted

Sort by
0
Kevin
Top achievements
Rank 2
answered on 30 Sep 2011, 12:30 PM
Hello William,

The reason it doesn't work is because adding it to the buttons onclick client-side event during the server-side click event, so the bahviour you described is expected. You should actually be calling the code like so:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  
      Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "CloseWindow", "returnToParent();", true);
  
  End Sub

I hope that helps.
0
William
Top achievements
Rank 1
answered on 30 Sep 2011, 01:51 PM
Thanks for the input but I may still be missing something because I could not get that to work.

But I did get
  Button1.Attributes("onclick") = "return returnToParent(); return false;"
 to work if I put it in the page load event, then I put other code in the button click event such as update my database which did get fired and then the window closed.

I'm not sure what direction I need to go.  I did get it to work but I also want to end up with the cleanest code.  Is this the right way to do this???  Is there a best practice method

I got the page load idea from:
http://msdn.microsoft.com/en-us/library/aa479390.aspx

Thanks
0
Hugo Augusto
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 17 Mar 2012, 10:10 PM

The problem that William reports also happens to me. I tried using

ScriptManager.RegisterStartupScript(this, this.GetType(), "mykey", "CloseWindow();", true);
  
Page.ClientScript.RegisterStartupScript(this.GetType(), "mykey", "CloseWindow();return false;", true);
  
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "mykey", "CloseWindow();", true);

inside my button click event but nothing happens. The script is not called. In my case I'm using a RadAjaxManagerProxy that is inside a user control placed in a page that has a masterpage. RadAjaxManager is on the Master Page of course. Everythings works just fine except closing my RadWindow (WindowAddFolder) from code behind using the code that I posted here. William solution worked great for me after removing the ";return false;" bit.
protected void Page_Load(object sender, EventArgs e)
{
     WindowAddFolder_ButtonSave.Attributes["onclick"] = "CloseWindow();";
}

Why does this happen and is this the right approach? I wish you guys made the control a little bit more server side/ajax friendly. It's a nightmare to work with it when using ajax instead of regular postbacks.
0
rdmptn
Top achievements
Rank 1
answered on 20 Mar 2012, 12:15 PM

Well, it is one way to do it, execute the script before the ajax request. Another way to do it is to properly inject the script, try

ScriptManager.RegisterStartupScript(this.Page, this..Page.GetType(), "mykey", "CloseWindow();", true);


Tags
Window
Asked by
William
Top achievements
Rank 1
Answers by
Kevin
Top achievements
Rank 2
William
Top achievements
Rank 1
Hugo Augusto
Top achievements
Rank 2
Iron
Veteran
Iron
rdmptn
Top achievements
Rank 1
Share this question
or