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

I have a strange problem with the page I'm developing. 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 look at the source code on the resulting page, I get the following:

<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 applicable to the page in question, and leave href = "#" if it wasn’t (basically an empty anchor tag). However, now this will lead to the actual link, which, of course, does not exist.

How can I make it do this?

+3
source share
4 answers

How do you embed a link? I 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, it appears as:

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

Page_Load href:

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

:

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

id .

+2

, , . ASCX ( ). , :

runat="server"

.

0

href :

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

asp #, ? href id.

in asp, the value is Control.ClientIDgenerated by combining the ID value of the control with the UniqueID value of its parent control. This explains how your identifier changes. According to this, each part of the generated identifier property is separated by the value of the ClientIDSeparator property. The value always returns an underscore (_).

That is why you have id="ctl00_ContentMainSection_CardsControl1_asel1"

0
source

Source: https://habr.com/ru/post/1742883/


All Articles