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])
- URL: It takes the URL of the page you want to open in new window and this is optional. you can left it blank.
- NAME: It is used to give any name to your window and this is also optional.
- 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)
- 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:
- It will insert the script before the form closing tag (</form>) at the end of page.
- 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.
- 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)