Data Binding and Analysis of container. dataitem)

Source: Internet
Author: User

Eval ("") and bind ("") one-way binding and one-way binding

BIND is a two-way binding, but can be used only when the data source can be changed

ASP. NET 2.0 improves the Data Binding operation in the template. It simplifies the Data Binding syntax databinder. eval (container. dataitem, fieldname) in v1.x to eval (fieldname ). The eval method, like databinder. Eval, can accept an optional formatted string parameter. Shortened eval syntax and databinder. the difference between Eval is that Eval will automatically parse fields based on the dataitem attribute of the most recent container object (such as datalistitem), while databinder. eval needs to use parameters to specify the container. For this reason, Eval can only be used in the template of the data-bound control, but cannot be used in the page layer. Of course, the ASP. NET 2.0 page still supports databinder. Eval. You can use it in environments that do not support simplified eval syntax.

The following example demonstrates how to bind a new simplified eval Data Binding syntax to the image, label, and hyperlink controls in the datalist data item template (itemtemplate.



<Asp: datalist id = "datalist1" repeatcolumns = "5" width = "600" runat = "server" performanceid = "objectperformance1">

<Itemtemplate>

<Asp: hyperlink id = "hyperlink1" runat = "server" navigateurl = & apos; <% # eval ("photoid", "photoformviewplain. aspx? Id = {0} ") %> & apos;>

<Asp: Image id = "image1" runat = "server" imageurl = & apos; <% # eval ("FILENAME", "images/thumbs/{0 }") %> & apos;/> </ASP: hyperlink>

<Asp: Label id = "captionlabel" runat = "server" text = & apos; <% # eval ("caption") %> & apos;/>

</Itemtemplate>

</ASP: datalist> <br/>

<Asp: objectdatasource id = "objectperformance1" runat = "server" typename = "datacomponenttableadapte Rs. photostableadapter "selectmethod =" getphotosforalbum ">

 

 

Data Binding and Analysis of container. dataitem
Flexible Use of Data Binding
Bind to simple attributes: <% # username %>
Bind to collection: <asp: ListBox id = "listbox1" datasource = '<% # myarray %> 'runat = "server">
Binding to expression: <% #(class1.property1. tostring () + "," + class1.property2. tostring () %>
Return Value bound to the method: <% # getsafestring (STR) %>
Bind to hashtable: <% # (dictionaryentry) container. dataitem). Key %>
Bind to arraylist: <% # container. dataitem %>

If an object is put in the array, you may need to convert the object before binding it, for example:
<% # (Object Type) container. dataitem). Attribute %>

Bind to dataview, able, Dataset:
<% # (Datarowview) container. dataitem) ["field name"] %> or
<% # (Datarowview) container. dataitem). Rows [0] ["field name"] %>
To Format:
<% # String. Format ("format", (datarowview) container. dataitem) ["field name"]) %>
<% # Databinder. eval (container. dataitem, "field name", "format") %>

Bind to datareader:
<% # (Idatareader) container. dataitem). Field name %>

Of course, the eval method of the databinder class is generally used for convenience. However, it is less efficient to bind a large amount of data at the same time.

This sentence is often used when binding data.Program: <% # Databinder. eval (container. dataitem, "XXXX") %> or <% # databinder. eval (container, "dataitem. XXXX") %>
Today I learned another method, and Microsoft also said that this method is more efficient than the above two methods.

<% # (Datarowview) container. dataitem) ["XXXX"] %>

Very useful, so that you can do a lot of things on the front-end page.

Remember to import the namespace system. Data on the front-end page. Otherwise, an error message is generated.

<% @ Import namespace = "system. Data" %>

This usage is actually the same as <% # (dictionaryentry) container. dataitem). Key %>.

When binding to dataset or able:

<% # (System. Data. datarowview) container. dataitem) ["field name"] %>
<% # (System. Data. datarowview) container. dataitem) [Index] %>

When bound to datareader:
<% # (System. Data. Common. dbdatarecord) container. dataitem) [Index] %>
<% # (System. Data. Common. dbdatarecord) container. dataitem) ["field name"] %>

The key is the container, which is mysterious. Its namespace is system. componentmodel. I need to further understand it.

Beginner. net, now reading the DataGrid Control, when itemtemplate displays data,

What is the difference between databinder. eval (container. dataitem, "name") and container. dataitem ("name?

Databinder is system. A static class in the Web, which provides the eval method to simplify writing data binding expressions. However, it uses reflection and other overhead methods to achieve ease of use, therefore, its performance is not the best. The container is not a static object or method at all. It is ASP. NET page compiler local variables declared in the Data Binding event handler, its type is the data container type of controls that can be bound to data (for example, the data binding container inside the repeater is called repeateritem). In these container classes, dataitem attributes are basically available, therefore, you can write container. dataitem, which returns the data item from the data source you are bound. If your data source is datatable, the Data Type of this data item is actually datarowview

 

Q: What is the eval () method?Source code? What are the performance differences between databinder. eval () and eval?

A: Actually, the eval method is templatecontrol, while the system. web. UI. page and system. web. UI. usercontrol inherits from templatecontrol, so we can directly call a method on page and usercontrol.

The page. Eval method can help us better write data binding expressions. In the ASP. NET 1. x era, the general form of data binding expressions is:

<% # Databinder. eval (container, "dataitem. Name") %>

In ASP. NET 2.0Code, We can write as follows:

<% # Eval ("name") %>

How is ASP. NET 2.0 implemented? First, we will study the eval method. By reflecting the source code of the. NET Framework 2.0 class library, we can see that this method is implemented as follows:

Protected internal object eval (string expression)

{

This. checkpageexists ();

Return databinder. eval (this. Page. getdataitem (), expression );

}

We don't have to worry about the first line. This is to check whether there is a page object during the call. If not, an exception will be thrown.

The key is the second line:

Return databinder. eval (this. Page. getdataitem (), expression );

Page. getdataitem () is also a newly added method in 2.0. It is used to replace container. dataitem in ASP. NET 1. X.

It seems that the getdataitem () method is hard to understand the eval principle. The implementation of getdataitem is also very simple:

Public object getdataitem ()

{

If (this. _ databindingcontext = NULL) | (this. _ databindingcontext. Count = 0 ))

{

Throw new invalidoperationexception (Sr. getstring ("page_missingdatabindingcontext "));

}

Return this. _ databindingcontext. Peek ();

}

We noticed that there is an internal object _ databindingcontext, which is a stack type by checking the source code. So he has the peek method. This piece of code is easy to understand. First, judge whether the stack is instantiated, and then determine whether there are any elements in the stack. If the stack is not instantiated or has no elements, an exception is thrown. Finally, return the elements at the top of the stack.

ASP. NET 2.0 uses a stack to save the so-called dataitem. We soon found the method for compressing and popping the element for this stack: Control. databind:

Protected virtual void databind (bool raiseondatabinding)

{

Bool flag1 = false; // The use of this flag is easily introduced in the context. If dataitem is used to press the stack, the stack is output later.

If (this. isbindingcontainer) // determines whether the control is a data-bound container. In fact, it is used to determine whether the control class implements inamingcontainer.

{

Bool flag2;

Object obj1 = databinder. getdataitem (this, out flag2); // This method is used to determine whether the control has the dataitem attribute and obtain it.

If (flag2 & (this. Page! = NULL) // if the control has dataitem

{

This. Page. pushdatabindingcontext (obj1); // press dataitem to stack. pushdatabindingcontext is to call the push method of _ databindingcontext.

Flag1 = true;

}

}

Try

{

If (raiseondatabinding) // you can check whether the databinding event is triggered.

{

This. ondatabinding (eventargs. Empty );

}

This. databindchildren (); // bind data to the Child control. If the control has a dataitem, The dataitem is pushed to the top of the stack. In this way, the eval or getdataitem method is called in the Child control, the dataitem you just pressed will be taken out.

}

Finally

{

If (flag1) // if there is a stack, it will pop up now.

{

This. Page. popdatabindingcontext (); // popdatabindingcontext is the pop method that calls _ databindingcontext

}

}

}

Now, we have a full understanding of the working principles of getdataiten and eval methods in ASP. NET 2.0.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.