What other objects are available inside the <% #%> tags in aspx?

I come across similar codes such as this all the time on aspx pages:

 <asp:CheckBox Runat="server" ID="myid" Checked='<%# DataBinder.Eval(Container.DataItem, "column").Equals(1) %>'> 

I was wondering what other objects I have inside this <% #%> tag. Why are DataBinder.Eval () and Container.DataItem not visible inside .CS code?

+4
source share
6 answers

In the <% #%> tags you have access to

  • Everything that is visible in your code class (including protected methods and properties).
  • Everything declared on an aspx page using <@import @>.
  • Everything that was passed as arguments to the event when the ItemDataBound event was fired (for example, RepeaterItemEventArgs, DataListItemEventArgs, etc.).

The container is actually a wrapper for RepeaterItemEventArgs.Item, DataListItemEventArgs.Item, etc. That way, you can access it in code outside of your ItemDataBound events, like e.Item (usually this is the name of the argument argument parameter).

DataBinder is also available in code using System.Web.UI.DataBinder.

The excellent use of Container.DataItem, on the other hand, is preferable to using Eval. Eval uses reflection, so there is overhead. In VB.NET, it will be something like

 <%#DirectCast(Container.DataItem, DataRow)("some_column")%> 

Or with#

 <%#((DataRow)Container.DataItem)["some_column"].ToString()%> 
+8
source

I believe that you have access to something within the page class, although the results of the expression are converted to a string, so you cannot insert conditional expressions the way you can with "<%".

Here is a good blog post that plunges under the covers of the created ASPX class.

Hope this helps.

+1
source

<% # is specific to built-in ASPX bindings, such as the proposed ckramer link.

Why are DataBinder.Eval () and Container.DataItem not showing up inside .CS code?

To access the anchor in the code, you need to set up the ItemDataBound event.

+1
source

ASP.NET subclasses the TemplateControl for each occurrence of the template. Binding operators are expressions used in a method inside this class. Thus, you can call any public / secure instance of the method in TemplateControl. See any example that uses XPath as they will use the XPath and XPathSelect methods; Eval, XPath, and XPathSelect are all instance methods in TemplateControl.

The DataBinder is actually a separate class, and Eval is a public static method; this is in System.Web.UI. DataBinder.Eval and plain Eval are not directly related, although they are noticeably similar to things.

I believe that "Container" is actually a local variable or parameter in which data binding operators are compiled. I can’t remember his type at the moment.

+1
source

Great example

 <%#((System.Data.DataRow)Container.DataItem)["ColumnName"].ToString()%> 
+1
source

using <%# %> actually means that the code inside this block will execute when the page.DataBind() method is page.DataBind() . That way, you can access anything at that point, accessible as secure / public for that particular page / control.

+1
source

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


All Articles