Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to open new window from code behind?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.94k
    Comment on it

    In  c#, there are different ways to implement the functionality,Here i am going to explain this functionality in the most easiest way. Let's try to understand and then follow the same in visual studio installed in your machine.

    So,To implement this functionality we can use Javascript "Window.Open()" method:

    SYNTAX:

    Window.Open( [URL], [Name], [Features], [Replace])
    1. URL: It takes the URL of the page you want to open in new window and this is optional. you can left it blank.
    2. NAME: It is used to give any name to your window and this is also optional.
    3. FEATURES: There are so many features which can be used separating them by commas,feature will consist an option and a value. Example: (fullscreen=yes|no|1|0,location=yes|no|1|0,menubar=yes|no|1|0,resizable=yes|no|1|0,toolbar=yes|no|1|0,
      height=500,left=300,top=300,width=500)
    4. REPLACE: Given URL will replace the current window. 

     

    To include above javascript functionality on server side we have to use:  "ClientScript.RegisterStartupScript"

    ClientScript: Dynamically it will insert the client script into your webpage at the runtime.

    RegisterStartupScript:

    1. It will insert the script before the form closing tag (</form>) at the end of page.
    2. By Using this method we can write a JavaScript function in code behind (aspx.cs page) and call it from code-behind or from HTML.
    3. It will take 4 Paramaters:
    • Type=>We define the type of the startup script to register.
    • Key=>Just to distinguish from other scripts,the key of the startup script is given.
    • Script=>it will hold the given URL in your script.
    • AddScriptTags=>It will take boolean value (true/false)which will indicate whether to add script tags or not.

            

     You can include the following code in your webpage to open a new window.

                   In design page:(Webform.aspx)

    <asp:Button ID="BtnOpeNewWindow" Text="Open New Window" runat="server" OnClick="OpenNewWindow" Height="59px" Width="218px" />

                  

                  In code behind page(Webform.aspx.cs)

    protected void OpenNewWindow(object sender, EventArgs e)
            {
                string url = "Default.aspx";
                string win = "window.open('" + url + "','mywindow', 'toolbar=1');";
               ClientScript.RegisterStartupScript(this.GetType(), "newwin", win, true);
               
            }
    

     Note: It will open a new window in new browser but if you want to open the new window in new tab; then there  is little bit change in your code behind file as given below:

     :) "Include the following code in aspx.cs page and you will get the new tabbed window" :)

      

    protected void OpenNewWindow(object sender, EventArgs e)
            {
                string url = "Default.aspx";
                string s = "window.open('" + url + "');";
               ClientScript.RegisterStartupScript(this.GetType(), "newwin", win, true);
               
            }
    

     Please refer screenshots of the above mentioned functionality.

     

    1. When we click on this "Open new window" button, the new window will be opened in new browser :)

         

     

    2. ScreenShot For the New tabbed Window :)

           

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: