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:
开发者_JAVA百科<%if(IsShow){%>
<a href="<%=url%>">HyperLink</a>
<%}%>
This two ways to do exactly same.
Which one is better, and why?
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.
精彩评论