开发者

ASP.NET anchor tag with runat="server" acting weird

I have a strange problem with the page I'm designing. I have a link like this:

<a id="asel1" runat="server" href="#"><img id="isel1" runat="server" src="/Images/selbar/1.jpg" /></a>

And when I view source on the resulting page I get this:

<a href="../Cards/#" id="ctl00_ContentMainSection_CardsControl1_asel1"><img src="/Images/selbar/1.jpg" id="ctl00_ContentMainSection_CardsControl1_isel1" /></a>

My goal was to programmatically insert a link if it was applicable to the page in question, and leave a href="#" if it wasn't (basically a blank anchor tag). However now it will take them to an 开发者_运维问答actual link, which of course doesn't exist.

How can I make it stop doing this?


How are you inserting the link? I've just tried the following:

<a id="asel1" runat="server" href="#">
    <img id="isel1" runat="server" src="/Images/selbar/1.jpg" />
</a>

If you do nothing in the code behind the link is rendered as:

<a href="#" id="ctl00_MainContent_asel1">
    <img src="/Images/selbar/1.jpg" id="ctl00_MainContent_isel1" />
</a>

and if in the Page_Load you define the href:

protected void Page_Load(object sender, EventArgs e)
{
    asel1.HRef = "../Cards";
}

it will render as:

<a href="../Cards" id="ctl00_MainContent_asel1">
    <img src="/Images/selbar/1.jpg" id="ctl00_MainContent_isel1" />
</a> 

which other the id mangling is the expected behavior.


I know this is an old question, but I too ran into the exact same problem. In my case the anchor tag was also inside an ASCX file (user control). I found that if I removed:

runat="server"

then the problem was fixed.


I would have replaced the href as following:

<a id="asel1" runat="server" onclick="return false;">
   <img id="isel1" runat="server" src="/Images/selbar/1.jpg" />
</a>


You are inserting the link dynamically in asp c#, right? you have to look there for the href and id.

in asp, Control.ClientID value is generated by concatenating the ID value of the control and the UniqueID value of its parent control. That explains how your id is changing. According to this Each part of the generated ID property is separated by the ClientIDSeparator property value. The value always returns an underscore (_).

That is why you have id="ctl00_ContentMainSection_CardsControl1_asel1"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜