Brian
Brian

Reputation: 25834

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

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

This code strikes me as ugly. More specifically, any time I see a call to Response.Write inside an ascx or aspx file, I get suspicious that I'm doing something wrong. Perhaps this is due to previously working with XSLT and noticing that when done right, there is rarely a need for <xsl:text> element in order to generate html. I feel like it interferes with my ability to read the code when my html code is hidden inside a <% %> block.

Is this just something I need to get used to with Asp.Net or is there a better way to do it?

Upvotes: 3

Views: 13225

Answers (4)

Daniel A. White
Daniel A. White

Reputation: 190905

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

<%= is equivalent to Response.Write.

And as nicely formatted code:

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

Also, this is the ASP.NET MVC method.

Upvotes: 7

Aleksandar
Aleksandar

Reputation: 1339

I think that is better to use Literal control and set his Text property in Page_Load or OnLoad methods in the code behind class.

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

Additionally System.Web.UI.WebControls.Literal control has an property named Mode that gives control of the rendering:

  1. PassThrough - The contents of the control are not modified;
  2. Encode - The contents of the control are converted to an HTML-encoded string (can give you some defense against cross-site scripting).
  3. Transform - Unsupported markup-language elements are removed from the contents of the control. If the Literal control is rendered on a browser that supports HTML or XHTML, the control's contents are not modified.

Check MSDN online documentation for literal class link

Upvotes: 1

Josh
Josh

Reputation: 44906

It looks like you are just doing some conditional formatting here. You could do some of this in the codebehind if you wanted to. Try using a Label and doing some logic against it.

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

And in your codebehind you could do something like this:

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

It is not a terrible thing to do stuff using server side includes, but you can utilize the codebehind to make for a cleaner seperation of view logic if you choose.

Upvotes: 3

Mehrdad Afshari
Mehrdad Afshari

Reputation: 421978

An alternative approach is to use a <asp:Literal> control and bind (or manually set) its Visible property to CanRemove and its contents to the specific text.

Whether this approach is better or worse depends on the style the application is developed in. In ASP.NET MVC style apps (or those that heavily modify HTML directly), I think the current method makes more sense. In ASP.NET Web form and server control based apps, the <asp:Literal> solution is more consistent.

Upvotes: 4

Related Questions