ASP.NET Eval () 및 Bind () 이해
사람이 나에게 어떤 절대적으로 최소한의 ASP.NET 코드를 이해 보여줄 수 Eval()와 Bind()?
두 개의 별도 코드 스 니펫을 제공하거나 웹 링크 일 수있는 것이 가장 좋습니다.
읽기 전용 컨트롤의 경우 동일합니다. 양방향 데이터 바인딩의 경우 선언적 데이터 바인딩으로 업데이트, 삽입 등을 수행하려는 데이터 소스를 사용하려면 Bind.
예를 들어 ItemTemplate및 EditItemTemplate. Bind또는 Eval에서 사용하면 ItemTemplate차이가 없습니다. 당신이 사용하는 경우 Eval에 EditItemTemplate, 값이 전달 될 수 없습니다 Update의 방법 DataSource그리드가 결합된다.
업데이트 : 나는이 예를 생각 해냈다 :
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Data binding demo</title>
</head>
<body>
<form id="form1" runat="server">
<asp:GridView
ID="grdTest"
runat="server"
AutoGenerateEditButton="true"
AutoGenerateColumns="false"
DataSourceID="mySource">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Eval("Name") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox
ID="edtName"
runat="server"
Text='<%# Bind("Name") %>'
/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
<asp:ObjectDataSource
ID="mySource"
runat="server"
SelectMethod="Select"
UpdateMethod="Update"
TypeName="MyCompany.CustomDataSource" />
</body>
</html>
다음은 개체 데이터 원본으로 사용되는 사용자 지정 클래스의 정의입니다.
public class CustomDataSource
{
public class Model
{
public string Name { get; set; }
}
public IEnumerable<Model> Select()
{
return new[]
{
new Model { Name = "some value" }
};
}
public void Update(string Name)
{
// This method will be called if you used Bind for the TextBox
// and you will be able to get the new name and update the
// data source accordingly
}
public void Update()
{
// This method will be called if you used Eval for the TextBox
// and you will not be able to get the new name that the user
// entered
}
}
Darin Dimitrov 가이 질문에 완벽하게 답변했지만 ASP.NET 4.5 이후로 강력한 형식의 바인딩 을 활용 하여 이러한 바인딩을 대체 * Eval()및 으로 설정하는 더 좋은 방법이 있습니다.Bind()
* 참고 : 이 기능은를 사용 하지 않는 경우에만 작동 SqlDataSource합니다 anonymous object. 강력한 형식의 개체 ( EF 모델 또는 기타 클래스)가 필요합니다.
이 코드 스 니펫은 컨트롤에 사용되는 방법 Eval과 Bind사용 방법을 보여줍니다 ListView( 위의 Darin Dimitrov가 설명한대로 InsertItem필요 Bind하며 ItemTemplate읽기 전용입니다 (따라서 레이블 임 Eval).).
<asp:ListView ID="ListView1" runat="server" DataKeyNames="Id" InsertItemPosition="LastItem" SelectMethod="ListView1_GetData" InsertMethod="ListView1_InsertItem" DeleteMethod="ListView1_DeleteItem">
<InsertItemTemplate>
<li>
Title: <asp:TextBox ID="Title" runat="server" Text='<%# Bind("Title") %>'/><br />
Description: <asp:TextBox ID="Description" runat="server" TextMode="MultiLine" Text='<%# Bind("Description") %>' /><br />
<asp:Button ID="InsertButton" runat="server" Text="Insert" CommandName="Insert" />
</li>
</InsertItemTemplate>
<ItemTemplate>
<li>
Title: <asp:Label ID="Title" runat="server" Text='<%# Eval("Title") %>' /><br />
Description: <asp:Label ID="Description" runat="server" Text='<%# Eval("Description") %>' /><br />
<asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" CausesValidation="false"/>
</li>
</ItemTemplate>
에서 ASP.NET 4.5+ , 데이터 바인딩 된 컨트롤은 새 속성으로 확장되었습니다 ItemType당신이 데이터 소스에 할당하고 개체의 유형에 포인트.
<asp:ListView ItemType="Picture" ID="ListView1" runat="server" ...>
PictureEF 모델의 강력한 유형 개체입니다. 그런 다음 다음을 교체합니다.
Bind(property) -> BindItem.property
Eval(property) -> Item.property
그래서 이건:
<%# Bind("Title") %>
<%# Bind("Description") %>
<%# Eval("Title") %>
<%# Eval("Description") %>
이것이 될 것입니다 :
<%# BindItem.Title %>
<%# BindItem.Description %>
<%# Item.Title %>
<%# Item.Description %>
Eval & Bind의 장점 :
- IntelliSense는 작업중인 개체의 올바른 속성을 찾을 수 있습니다.

- 속성 이름이 변경 / 삭제되면 브라우저에서 페이지를보기 전에 오류가 발생합니다.
- 개체의 속성 이름을 바꿀 때 외부 도구 (VS의 전체 버전 필요)가 마크 업의 항목 이름을 올바르게 바꿉니다.
출처 : 이 훌륭한 책에서
참조 URL : https://stackoverflow.com/questions/1778221/understanding-asp-net-eval-and-bind
'IT이야기' 카테고리의 다른 글
| Eclipse 상대 파일 경로 작동 (0) | 2021.04.03 |
|---|---|
| SQL Server에 연결하는 동안 네트워크 관련 또는 인스턴스 관련 오류가 발생한 이유 (0) | 2021.04.03 |
| JavaEE에서 'Facet'이란 (0) | 2021.04.03 |
| PHP에서 사용자 로그인 시도를 제한하는 방법 (0) | 2021.04.03 |
| StyleCop 경고를 억제하는 방법 (0) | 2021.04.02 |