Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using <%= %> or <%# %> with runat=server in ASP.NET

I have a web control that looks like this

public class Foo : WebControl
{
  [Bindable(true)]
  [Category("Default")]
  [DefaultValue("")]
  [Localizable(true)]
  public string Bar { get; set; }

  protected override void Render(HtmlTextWriter output)
  {
    output.WriteLine(Bar);
  }
}

I want to put this webcontrol in my aspx page like so:

<cc1:Foo Bar="<%= Fa.La.La %>/otherstuff" runat="server" />

(obviously this code is simplified to show the problem)

In my Render method the variable Fa.La.La is not evaluated. It's coming in as the raw text "<%= Fa.La.La %>" How do I evaluate it?

I'm not particular how the variables are passed in. If the variables can be evaluated if they are passed in as <%# ... %>, that works fine. The point is I have some server-side variables I want evaluated before/while my Render() method is called.

The only thing I can think of is to use a regex to grab the contents of <%= ... %> and use reflection or something, but there has to be a more elegant way to do this.

This question is pretty similar to using server variables in a href <%= xx %> with runat=server, but it's not exactly the same since none of the answers there were useful.

like image 307
mhenry1384 Avatar asked Aug 13 '12 04:08

mhenry1384


People also ask

Is code that is embedded directly within the ASP NET page?

Embedded code blocks are supported in ASP.NET Web pages primarily to preserve backward compatibility with older ASP technology. In general, using embedded code blocks for complex programming logic is not a best practice, because when the code is mixed on the page with markup, it can be difficult to debug and maintain.

Which inline ASP NET tag will HTML encode its contents?

<%#: %> is an HTML Encoded Data-Binding Expression (new in ASP.NET 4.5).

What is inline code in asp net?

Inline Code refers to the code that is written inside an ASP.NET Web Page that has an extension of . aspx. It allows the code to be written along with the HTML source code using a <Script> tag.

Is ASP NET core a framework?

ASP.NET is a popular web-development framework for building web apps on the . NET platform. ASP.NET Core is the open-source version of ASP.NET, that runs on macOS, Linux, and Windows.


1 Answers

Well, first you should be clear to diff between both tags. here are some points i have read and used practically..

  • The <%= expressions are evaluated at render time
  • The <%# expressions are evaluated at DataBind() time and are not evaluated at all if DataBind() is not called.
  • <%# expressions can be used as properties in server-side controls.<%= expressions cannot.

read more it on MSDN Blog

like image 118
Mayank Pathak Avatar answered Oct 03 '22 16:10

Mayank Pathak