Several methods of data binding in asp.net

Source: Internet
Author: User

This article introduces several data binding methods in. net. I use the <% # And %> delimiters most of the time. If you need the Eval and Bind functions, check them out.

The data binding expression is included in the <% # And %> delimiters and uses the Eval and Bind functions. The Eval function is used to define one-way (read-only) binding. The Bind function is used to define bidirectional (updatable) binding. In addition to calling the Eval and Bind methods in the data binding expression to perform data binding, you can also call any common code within the <% # And %> delimiters, to execute the code During page processing and return a value.

When the DataBind method of the control or Page class is called, the data binding expression is parsed. Some controls, such as the GridView, DetailsView, and FormView controls, automatically parse the data binding expression during the PreRender event of the control, and do not need to explicitly call the DataBind Method
 

There are two types of data binding in ASP. NET: Single-value binding and multi-value binding.

 

What they have in common: they do not need to work with ADO. NET at the same time.

Differences: Single-value binding can dynamically insert a variable, attribute, or expression into a page to help create a data control template.

Multi-value binding can display the content used by a table. It must support its special control (including the DataSource attribute)

 

Single value binding:

There are four common expressions:

The Code is as follows: Copy code
<% = XX %>, inline mode, can reference C # code
<Asp: FormView ID = "FormView1"
Performanceid = "sqlperformance1"
DataKeyNames = "ProductID"
RunAt = "server">

<ItemTemplate>
<Table>
<Tr> <td align = right> <B> Product ID: </B> </td> <% # Eval ("ProductID ") %> </td> </tr>
<Tr> <td align = right> <B> Product Name: </B> </td> <% # Eval ("ProductName ") %> </td> </tr>
<Tr> <td align = right> <B> Category ID: </B> </td> <% # Eval ("CategoryID ") %> </td> </tr>
<Tr> <td align = right> <B> Quantity Per Unit: </B> </td> <% # Eval ("QuantityPerUnit ") %> </td> </tr>
<Tr> <td align = right> <B> Unit Price: </B> </td> <% # Eval ("UnitPrice ") %> </td> </tr>
</Table>
</ItemTemplate>
</Asp: FormView>

<% # XXX %>, referencing the code field in the. cs File

I have added the extension method version. I like the concept of expansion.

Now, you only need to add a static help class and name it as you like.

The Code is as follows: Copy code

Public static class Helper
{
Static object ExpHelper <TEntity, TResult> (Page, Func <TEntity, TResult> func)
{
Var itm = page. GetDataItem ();
Return func (TEntity) itm );
}

Public static object Eval <T> (this Page, Func <T, object> func)
{
Return ExpHelper <T, object> (page, func );
}
}


On the page, you can

The Code is as follows: Copy code
<% # This. Eval <Student> (_ => _. Name + "(" + _. Age + ")") %>


<% # Eval (xxx) %>. You need to bind the data source.
The Eval method can calculate the post-bound data expression in the template of a data binding control (such as the GridView, DetailsView, and FormView controls. At runtime, the Eval method calls the Eval method of the DataBinder object and references the current data item of the named container. A naming container is usually the smallest component of a data binding control that contains a complete record, such as a row in the GridView control. Therefore, the Eval method can only be used for binding to the template of the data binding control.


The following controls are list controls that support data binding:

Asp: RadioButtonList
Asp: CheckBoxList
Asp: DropDownList
Asp: Listbox
The optional items in each of the preceding controls are usually defined in one or more asp: ListItem controls, similar to the following:

The Code is as follows: Copy code

<Html>
<Body>

<Form runat = "server">
<Asp: RadioButtonList id = "countrylist" runat = "server">
<Asp: ListItem value = "C" text = "China"/>
<Asp: ListItem value = "S" text = "Sweden"/>
<Asp: ListItem value = "F" text = "France"/>
<Asp: ListItem value = "I" text = "Italy"/>
</Asp: RadioButtonList>
</Form>

</Body>
</Html>

Related Article

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.