Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I want to know that how to open web page in different window and passed a value from javascript to c# on page load


ASP.NET
<JavaScript>
function open_win() 
{
var s=document.getElementById("txtQty");
window.open("../Stock/IPurchaseDetail.aspx","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
}

</javascript>


<asp:CheckBox runat="server" ID="chkGenerateSerialNumber" AutoPostBack="true" CausesValidation="false"
        OnCheckedChanged="chkGenerateSerialNumber_CheckedChanged" />
C#
protected void chkGenerateSerialNumber_CheckedChanged(object sender, EventArgs e)
        {

            if (chkGenerateSerialNumber.Checked == true)
            {
                Session["NoOfRows"] = txtQty.Value;


                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(@"<script language='javascript'>");
                sb.Append(@"open_win();");
                sb.Append(@"</script>");
                ScriptManager.RegisterStartupScript(this, this.GetType(), "javascript",   sb.ToString(), true);// it does  work but in open_win() method window.open("../Stock/IPurchaseDetail.aspx","_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400"); does not get call
}
}

Now I have got Session["NoOfRows"] = txtQty.Value; but another page does not call.
C#
protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              NoOfRows Convert.ToInt32((string)HttpContext.Current.Session["NoOfRows"]);

          }

      }
Posted
Updated 30-Oct-13 1:49am
v4

Hello,

There are at-least two ways to do that.

  1. Similar to your code except that the desired parameters are passed as a Query String. S your code becomes something like one shown below.
    JavaScript
    function open_win() {
        var s = document.getElementById("txtQty");
        var url = "../Stock/IPurchaseDetail.aspx?txtQty=" + encodeURIComponent(s.value);
        window.open(url, "_blank","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
    }
  2. This method is very much similar to above, the only difference is that you open a named window with blank url and POST the existing form to that window. Following code snippet shows how this can be done.
    JavaScript
    function open_win() {
        var hWin = window.open("", "hWinPurchase","toolbar=yes, location=yes, directories=no, status=no, menubar=yes, scrollbars=yes, resizable=no, copyhistory=yes, width=400, height=400");
        var frm = document.getElementById('YOUR_FORM_NAME');
        frm.target = "hWinPurchase";
        frm.submit();
    }
  3. Regards,
 
Share this answer
 
Add target attribute to form tag thats it, you are done.
Now whenever button is clicked, aspx will open in new window.
Paste the following code in your .ASPX page
ASP.NET
<form id="form1" runat="server" target="_blank">

<!-- Add your buttons herere -->

</form>
 
Share this answer
 
Comments
Member 7909353 30-Oct-13 4:49am    
But page is child page.
Go to Tools->Option->Content and uncheck block popup windows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900