问题
The one-way databind Eval
function belongs to DataBinder
class and can be accessed in a page through TemplateControl.Eval
method that calls the DataBinder.Eval
method using the GetDataItem
method to resolve the object reference that the expression is evaluated against, phew!
OK... so, how the two-way databind Bind
function works? It belongs to which class?
回答1:
As far as I could tell the "Bind" is not a method but rather a language construct.
Source and further reading: http://weblogs.asp.net/leftslipper/archive/2007/06/29/how-asp-net-databinding-deals-with-eval-and-bind-statements.aspx
回答2:
I would like to say ,Bind() method was nowhere to be found, even using Reflector.
---for read-write values such as TextBoxes (also known as "two-way databinding") you can use the Bind() statement. Where does that Bind() statement come from?
To be specifically, there isn’t a bind method in ASP.NET! When ASP.NET parses your file and sees you're using a databinding expression
(in the angle-bracket-percent-pound format, "<%# %>")
it has special-case code to parse for the Bind syntax and generates some special code for it. When you use <%# Bind("Name") %>
it's not a real function call.
If ASP.NET parses the code and detects a Bind() statement, it splits the statement into two parts. The first part is the one-way databinding portion, which ends up being just a regular Eval() call. The second part is the reverse portion, which is typically some code along the lines of "string name = TextBox1.Text"
that grabs the value back out from where it was bound.
Non-Bind() databinding statements are literal code (we use CodeSnippetExpressions in CodeDom), so arbitrary code in the language of your choice is allowed. However, because ASP.NET has to parse Bind() statements, two-way databinding doesn’t support anything other than Bind(). For example, the following syntax is invalid because it tries to invoke arbitrary code and use Bind() at the same time: <%# FormatNameHelper(Bind("Name")) %>
The only formats supported in two-way databinding are Bind("field") and Bind("field", "format string {0}"). There are some very minor variations of these syntax examples, such as allowing use of single quotes and not just double quotes. Since some languages supported by ASP.NET favor one format over the other, we have to support both formats, even though the language you're using may support only one.
来源:https://stackoverflow.com/questions/7866066/the-databind-bind-function-belongs-to-which-class