"+ProductName+""); %> This code amazes me as ugly....">

Asp.net Response.Write inside an ascx / aspx file

<%if (CanRemove) Response.Write("<b>"+ProductName+"</b>"); %>

This code amazes me as ugly. More specifically, every time I see a Response.Write call inside an ascx or aspx file, I get a suspicion that I'm doing something wrong. Perhaps this is due to the fact that I previously worked with XSLT and noticed that when everything is done correctly, an element is rarely needed <xsl:text>to generate html. I feel this interferes with my ability to read code when my html code is hidden inside a block <% %>.

Is this what I need to get used to Asp.Net or is there a better way to do this?

+3
source share
4 answers
<% if (CanRemove) { %><b><%= ProductName %></b><% } %>

<%= equivalent to Response.Write.

And how beautifully formatted the code is:

<% if (CanRemove) { %>
    <b><%= ProductName %></b>
<% } %>

It is also an ASP.NET MVC method.

+6

<asp:Literal> ( ) Visible CanRemove .

, . ASP.NET MVC ( , HTML ), , . ASP.NET - <asp:Literal> .

+4

, . , . .

<asp:Label id="lblProductName" runat="server" />

- :

lblProductName.Text = ProductName;
lblProductName.Visible = CanRemove;
lblProductName.CssClass = "productLabel";

, , codebehind, , .

+3

, Literal control Text Page_Load OnLoad .

protected void Page_Load(object sender, EventArgs e)
{
  if (CanRemove)
  {
    myLiteral.Text = "<b>ProductName</b>"
  }
}

System.Web.UI.WebControls.Literal control Mode, :

  • PassThrough - ;
  • Encode - , HTML ( ).
  • Transform - Unsupported markup language elements are removed from the contents of the control. If the Literal control is displayed in a browser that supports HTML or XHTML, the content of the control does not change.

Check the MSDN online documentation for class literal reference

+1
source

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


All Articles