HyperLink

Is it always better to use asp control?

I am confused which one is better.

Aspx:

<asp:HyperLink ID="HyperLink1" runat="server">HyperLink</asp:HyperLink>

the code:

String url = "http://stackoverflow.com";
if(IsShow)
{
    HyperLink1.Visible = true;
    HyperLink1.NavigateUrl = url;
}

and the second option:

<%if(IsShow){%>
<a href="<%=url%>">HyperLink</a>
<%}%>

These are two ways to do the same.

Which one is better, and why?

+3
source share
1 answer

Basically, for readability, preference is given to the first (although the code you entered is not valid - you need to wrap it in a script tag and specify a function (i.e. Page_Load) to execute your logic.

Secondly, the second method is executed on the page_PreRender, so you are limited to running logic at the end of the page life cycle. You will notice this method when programming in ASP.NET MVC (since there is no code model).

Use the first method in Web Forms, the second in ASP.NET MVC.

+4
source

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