Close window with code behind

5 posts, 0 answers
  1. William
    William avatar
    55 posts
    Member since:
    Sep 2008

    Posted 30 Sep 2011 Link to this post

    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

  2. Kevin
    Kevin avatar
    360 posts
    Member since:
    Jul 2012

    Posted 30 Sep 2011 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. William
    William avatar
    55 posts
    Member since:
    Sep 2008

    Posted 30 Sep 2011 Link to this post

    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
  5. Hugo Augusto
    Hugo Augusto avatar
    55 posts
    Member since:
    Jul 2004

    Posted 17 Mar 2012 Link to this post

    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.
  6. rdmptn
    rdmptn avatar
    72 posts
    Member since:
    Aug 2011

    Posted 20 Mar 2012 Link to this post

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


Back to Top
UI for ASP.NET Ajax is Ready for VS 2017