Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a URL in a new tab

I have an url like

Response.Redirect("~/webpages/frmCrystalReportViewer.aspx?VoucherNo=" + txtVoucherNo.Text + "&VoucherDate=" + txtVoucherDate.Text + " &strUserCode=" + strUserCode.ToString() + "&strCompanyCode=" + strCompanyCode.ToString() + "&formName=frmPaymentVoucher");

I want to open this url in new tab of browser. I tried below code...

string pageurl = "~/webpages/frmCrystalReportViewer.aspx?VoucherNo=" + txtVoucherNo.Text + "&VoucherDate=" + txtVoucherDate.Text + " &strUserCode=" + strUserCode.ToString() + "&strCompanyCode=" + strCompanyCode.ToString() + "&formName=frmPaymentVoucher";
Response.Write("<script>");
Response.Write("window.open('" + pageurl + "','_blank')");
Response.Write("</script>");

also i tried below

string pageurl = "~/webpages/frmCrystalReportViewer.aspx?VoucherNo=" + txtVoucherNo.Text + "&VoucherDate=" + txtVoucherDate.Text + " &strUserCode=" + strUserCode.ToString() + "&strCompanyCode=" + strCompanyCode.ToString() + "&formName=frmPaymentVoucher";
 ScriptManager.RegisterStartupScript(Page, Page.GetType(), "popup", "window.open('" + pageurl + "','_blank')", true);

also i tried

<asp:Button ID="btnPrint" Text="Print" runat="server" OnClick="btnPrint_Click" OnClientClick="aspnetForm.target ='_blank';"/>

but all are not working. Please tell me any another solution. Thanks in advance.

like image 563
Pallavi Sagar Avatar asked Jun 03 '13 12:06

Pallavi Sagar


3 Answers

The answer given by anand360 worked for me. Thanks!!

I made a slight change in JavaScript as follows to access only the desired element.

document.getElementById["element_id"].target = "_blank";
like image 142
Watz Avatar answered Oct 19 '22 17:10

Watz


With the help of JavaScript we can set the target property of form to _blank whenever we want to open the page in a new window. Try the following

I have an ASP.Net Button

 <asp:Button ID="btnPrint" runat="server" Text="PRINT BILL" Onclick="btnPrint_Click"  OnClientClick="SetTarget();" />

I am calling the SetTarget() JavaScript function OnClientClick event of the ASP.Net Button Control as described below

<script type = "text/javascript">
 function SetTarget() {
     document.forms[0].target = "_blank";
 }
</script>

calling the btnPrint_Click method OnClick event Control as described below

 protected void btnPrint_Click(object sender, EventArgs e)
    {
         Response.Redirect("ReportViewer1.aspx");
    }
like image 35
anandd360 Avatar answered Oct 19 '22 19:10

anandd360


You are using URL with ~ and it won't recognize by javascript. You should process url with ~ by using ResolveUrl method which

converts a URL into one that is usable on the requesting client(c)msdn

In your case:

Response.Write(String.Format("window.open('{0}','_blank')", ResolveUrl(pageurl)));
like image 14
Eugene Trofimenko Avatar answered Oct 19 '22 18:10

Eugene Trofimenko