I am using ASP.NET to open a new window on clicking a button. Here in MainPage.aspx
i will enter a value in TextBox
and press the button. This value should be obtained and displayed in the new window that opens. Im opening the new window using ScriptManager.RegisterStartupScript
. But im not sure how to pass the value and display it in new page. Im using the following code for this,
MainPage.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Main Page</title>
<link rel='stylesheet' type='text/css' href='Styles/Main.css'/>
</head>
<body>
<form id="form_main" runat="server">
<asp:TextBox ID="txt_value" runat="server" />
<asp:Button ID="btn_open" Text="Open" runat="server" OnClick="btn_open_Click" />
</form>
</body>
</html>
MainPage.aspx.cs
protected void btn_open_Click(object sender, EventArgs e)
{
string value = txt_value.Text;
// Pass value to new page when opening it
ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'NewPage.aspx', null, 'height=700,width=760,status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);
}
NewPage.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>New Page</title>
<link rel='stylesheet' type='text/css' href='Styles/New.css'/>
</head>
<body>
<form id="form_new" runat="server">
<asp:Label ID="lbl_received" runat="server" Text="" />
</form>
</body>
</html>
NewPage.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
lbl_received.Text = "Value got from MainPage.aspx";
}