How to open page in new tab using the response. redirect at asp.net

后端 未结 8 2103
太阳男子
太阳男子 2021-01-03 23:42

I want to open a new tab or a new page by using Response.Redirect in a button click handler. I\'m using a query string to pass some values. How can I open he pa

相关标签:
8条回答
  • 2021-01-03 23:46

    if you are using http then use below code

    Response.Write("<script>window.open ('URL','_blank');</script>");
    

    this code cannot be used for https for https do below

    javascript in page

    function shwwindow(myurl) {
        window.open(myurl, '_blank');
    }
    

    in c# code behind

     string URL = ResolveClientUrl("~") + "**internal page path**";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "show window", 
        "shwwindow('"+URL+"');", true);
    

    this code cannot bybass the browser popup blocker. user must allow it to run. for ie it opens in new window or new tab up to ie version in firefox and chrome it opens new tab

    enjoy it!!

    0 讨论(0)
  • 2021-01-03 23:47

    You can change your design element as below : OnClientClick

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button"  OnClientClick ="document.forms[0].target = '_blank';"  />
    

    Javascript code :

     Response.Write("<script>");
     Response.Write("window.open('NewQuote.aspx' ,'_blank')");
     Response.Write("</script>");
    
    0 讨论(0)
  • 2021-01-03 23:47

    You can use ScriptManager to do the needed:

    private void Redirect_New_Tab(string url_To_Open)
    {    
        string modified_URL = "window.open('" + url_To_Open + "', '_blank');";
        ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", modified_URL , true);
    }
    
    0 讨论(0)
  • 2021-01-03 23:53

    I encountered this same problem today.

    Response.write didn't work for me, to solve it I used instead System.Web.UI.ScriptManager.RegisterClientScriptBlock.

    This is how your btnSave click event should be:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ...//some code to insert records
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "openModal", "window.open('NewQuote.aspx?val= "+ this.txtQuotationNo.Text+"' ,'_blank');", true);
    }
    

    NOTE: I'm using ScriptManager.RegisterClientScriptBlock because the button is inside an UpdatePanel, if that's not your case you should try:

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ...//some code to insert records
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "openModal", "window.open('NewQuote.aspx?val= " + this.txtQuotationNo.Text + "' ,'_blank');", true);
    }
    
    0 讨论(0)
  • 2021-01-03 23:54

    Try this. This works sure for me...

    protected void btnSave_Click(object sender, EventArgs e)
    {
        ...//some code to insert records
        Response.Write("<script>window.open ('NewQuote.aspx?val=" + txtQuotationNo.Text+"','_blank');</script>");
    }
    
    0 讨论(0)
  • 2021-01-04 00:06

    Worked for me when I left the Double Quotes off of target ='_blank';

    0 讨论(0)
提交回复
热议问题