开发者

Using UrlEncode in HyperLink Button in .NET and in Anchor Tag of HTML

开发者 https://www.devze.com 2023-02-04 13:57 出处:网络
I am just trying to use UrlEncode in a Hyperlink which is in GridView and found it was not working. Then I tried to take the HyperLink as a separate control and tried with that. It is not giving Hyper

I am just trying to use UrlEncode in a Hyperlink which is in GridView and found it was not working. Then I tried to take the HyperLink as a separate control and tried with that. It is not giving Hyperlink to me, I mean it is not even clickable.

While when I tried with simple Anchor tag, it is working. This is what I开发者_如何学Python am using

    <asp:HyperLink   ID="HyperLink2" runat="server" 
            NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' > wc  
    </asp:HyperLink>

// While following is working
    <a  title="asxd" href='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>'>wc
</a>

Still looking for the answer


You don't need to use a Hyperlink control or make an anchor tag runat="server" unless you're doing something to it in your code behind.

<a href="<%=ResolveUrl("~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")) %>">wc</a>


You are missing the text property of the hyperlink control.

<asp:HyperLink   ID="HyperLink2" runat="server" 
    NavigateUrl ='<%= "~/Default.aspx?customer=" + "&CompanyName=" + Server.UrlEncode("abc#")%>' Text="wc" />


You also can set NavigateUrl property in code-behind file, in Page_Load event handler, for instance. It will work.

In the code-behind class:

        protected void Page_Load(object sender, EventArgs e)
        {                
            HyperLink2.NavigateUrl = "~/Default.aspx?customer=&CompanyName=" + Server.UrlEncode("abc#");                 
        }

And in markup:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:HyperLink ID="HyperLink2" runat="server" Target="_new">wc  
</asp:HyperLink>

    </div>
    </form>
</body>
</html>


<%= ... %>

doesn't work inside ASP.NET controls.

Alternatives:

  • <%# ... > works in some cases in data-bound controls
  • code-behind
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号