Hi ,

I have text box and beside to it I have a linkbutton showing the link that is present in the text box. I am getting the URL from the website into the text from database.I need to open the link button in a new browser window.


aspx page

<asp:Label ID="lblBusinessURL" runat="server" Text="Label"></asp:Label>
                                                               
 <asp:LinkButton ID="lnkBusinessURL"  runat="server" ></asp:LinkButton>

page

string businessurl;
            businessurl = DSabc.Tables[0].Rows[0]["BusinessURL"].ToString();
            if (businessurl.Contains ("http://"))
            {
                 lblBusinessURL.Text = "<a href=" + businessurl  + ">" + businessurl + "</a>";
            }
            else {
             lblBusinessURL.Text = "<a href=http://" + businessurl  + ">" + businessurl  + "</a>";
            
            }

Can anybody help??

Thanks in Advance.

Dani AI

Generated

As suggested, prefer an <asp:HyperLink> for navigation — LinkButton is for server-side postbacks and is the wrong control when you just want to navigate. , writing raw anchor HTML into a Label can work but your current string lacks quoted attributes, doesn’t set a target, and leaves you open to XSS or javascript: schemes coming from the database. Below are safe, practical options: one using HyperLink (recommended) and one that keeps a LinkButton but uses client-side opening.

Use a HyperLink and validate the URL on the server before assigning it. Validate the scheme (allow only http/https), ensure a scheme is present (prefix if missing), HTML-encode the link text, and add Target and rel to avoid window.opener attacks:

<asp:HyperLink ID="hlBusinessURL" runat="server"></asp:HyperLink>
string raw = DSabc.Tables[0].Rows[0]["BusinessURL"].ToString().Trim();
if (!raw.StartsWith("http://", StringComparison.OrdinalIgnoreCase) &&
    !raw.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
    raw = "http://" + raw;

if (Uri.TryCreate(raw, UriKind.Absolute, out Uri uri) &&
    (uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
{
    hlBusinessURL.NavigateUrl = uri.ToString();
    hlBusinessURL.Text = HttpUtility.HtmlEncode(uri.ToString());
    hlBusinessURL.Target = "_blank";
    hlBusinessURL.Attributes["rel"] = "noopener noreferrer";
}
else
{
    hlBusinessURL.Text = "Invalid URL";
}

If you must keep a LinkButton, open a new window client-side and prevent postback:

// after validating 'url' as above
lnkBusinessURL.OnClientClick = "window.open('" + url.Replace("'", "\\'") + "','_blank'); return false;";

Notes: server-side Redirect cannot force a new browser window. Always validate/sanitize DB URLs (reject javascript:), encode visible text, and add rel="noopener noreferrer" when using target="_blank". For accessibility, indicate to users when a link opens in a new window.

Use HyperLink instead of LinkButton.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.