Sungguk Lim
Sungguk Lim

Reputation: 6228

Is it always better to used asp Control?

I'm confused which one is better.

ASPX:

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

Code:

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

and the second option is:

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

This two ways to do exactly same.

Which one is better, and why?

Upvotes: 3

Views: 78

Answers (1)

RPM1984
RPM1984

Reputation: 73112

It's mainly for readability that the first one is preferred (although the code you pasted is invalid - you need to wrap it in a script tag and specify the function (ie Page_Load) to do your logic.

Secondly, the second method gets executed on Page_PreRender, so you are limited by performing logic late in the page life cycle. You will notice this method when programming in ASP.NET MVC (as there is no code-behind model).

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

Upvotes: 4

Related Questions