简体   繁体   中英

opening a link in a new window

I need to open the link in the link button in a new window. I have the below code but it throws javascript error. Please help

aspx code:

<asp:LinkButton ID="Link" runat="server" CssClass="ReadOnlyLabel" 
                        CausesValidationn="false" OnClick="Link_Click"/>

code behind

protected void Link_Click(object sender, EventArgs e)
{
   ScriptManager.RegisterStartupScript(this, typeof(string), "OPEN_WINDOW", 
                      "window.open(" + Link.Text + " ,'new_tab');", true);
}

Better to use Hypelink in that scenario, it has target option to do that:-

<asp:HyperLink ID="Link" runat="server" CssClass="ReadOnlyLabel" 
                  CausesValidationn="false" Target="_blank">Click Here</asp:HyperLink>

Just set NavigateUrl property to set window url, and set Target property as _blank .

try this

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),  
"window.open('" + Link.Text + "' ,'new_tab');", true);

EDIT

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),  
@"window.open('https://www.google.co.in/' ,'new_tab');", true);

So it Becomes

ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(),  
"window.open('https://" + Link.Text + "' ,'new_tab');", true);

I'm quite the novice with aspx, but surely this would be of interest?

onclick="window.open (this.href, 'popupwindow', 'width=500,height=500,scrollbars,resizable')

http://forums.asp.net/t/1162787.aspx?How+to+open+hyperlink+in+new+window+without+any+tools+displayed+

This worked for me

Uri uri = new Uri(LinkButton.Text);
ScriptManager.RegisterStartupScript(this, Page.GetType(), "", "window.open('" + uri.OriginalString + "');", true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM