在 ASP.NET Web Forms 中,Eval() 和 Bind() 是数据绑定表达式中的关键方法,主要用于在数据绑定控件(如 GridView、DetailsView 等)中处理数据,以下是它们的区别和应用示例:

核心区别
| 特性 | Eval() |
Bind() |
|---|---|---|
| 绑定方向 | 单向(只读) | 双向(读写) |
| 使用场景 | 仅显示数据(ItemTemplate) | 编辑/插入数据(EditItemTemplate) |
| 数据更新 | ❌ 不支持回发更新 | ✅ 支持回发更新 |
| 性能 | 更轻量 | 略重(需维护视图状态) |
| 语法 | <%# Eval("字段名") %> |
<%# Bind("字段名") %> |
应用示例
示例 1:GridView 中使用 Eval 和 Bind
<asp:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false"
DataKeyNames="EmployeeID" OnRowEditing="gvEmployees_RowEditing"
OnRowUpdating="gvEmployees_RowUpdating">
<Columns>
<!-- 只读列:使用 Eval -->
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<%# Eval("EmployeeID") %>
</ItemTemplate>
</asp:TemplateField>
<!-- 可编辑列:EditTemplate 中使用 Bind -->
<asp:TemplateField HeaderText="姓名">
<ItemTemplate>
<%# Eval("Name") %> <!-- 显示模式 -->
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server"
Text='<%# Bind("Name") %>' /> <!-- 编辑模式 -->
</EditItemTemplate>
</asp:TemplateField>
<!-- 下拉列表示例 -->
<asp:TemplateField HeaderText="部门">
<EditItemTemplate>
<asp:DropDownList ID="ddlDept" runat="server"
SelectedValue='<%# Bind("DepartmentID") %>'> <!-- 绑定选中值 -->
<asp:ListItem Value="1">技术部</asp:ListItem>
<asp:ListItem Value="2">市场部</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<%# Eval("DepartmentName") %> <!-- 显示部门名称 -->
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
后台代码(C#)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
private void BindGridView()
{
// 从数据库获取数据(此处简化为模拟数据)
var employees = new List<Employee> {
new Employee { EmployeeID = 1, Name = "张三", DepartmentID = 1, DepartmentName = "技术部" },
new Employee { EmployeeID = 2, Name = "李四", DepartmentID = 2, DepartmentName = "市场部" }
};
gvEmployees.DataSource = employees;
gvEmployees.DataBind();
}
protected void gvEmployees_RowEditing(object sender, GridViewEditEventArgs e)
{
gvEmployees.EditIndex = e.NewEditIndex;
BindGridView();
}
protected void gvEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
// 通过 Bind 自动获取修改后的值
int employeeId = (int)gvEmployees.DataKeys[e.RowIndex].Value;
GridViewRow row = gvEmployees.Rows[e.RowIndex];
string newName = ((TextBox)row.FindControl("txtName")).Text;
int newDeptId = int.Parse(((DropDownList)row.FindControl("ddlDept")).SelectedValue);
// 更新数据库(伪代码)
// UpdateEmployee(employeeId, newName, newDeptId);
gvEmployees.EditIndex = -1;
BindGridView();
}
示例 2:FormView 中使用 Bind
<asp:FormView ID="fvEmployee" runat="server" DataKeyNames="EmployeeID"
DefaultMode="Insert" OnItemInserting="fvEmployee_ItemInserting">
<InsertItemTemplate>
<div>
<label>姓名:</label>
<asp:TextBox ID="txtName" runat="server" Text='<%# Bind("Name") %>' />
</div>
<div>
<label>邮箱:</label>
<asp:TextBox ID="txtEmail" runat="server" Text='<%# Bind("Email") %>' />
</div>
<asp:Button runat="server" CommandName="Insert" Text="保存" />
</InsertItemTemplate>
</asp:FormView>
后台代码(C#)
protected void fvEmployee_ItemInserting(object sender, FormViewInsertEventArgs e)
{
// 通过 Bind 自动收集的值会存在于 e.Values 中
string name = e.Values["Name"].ToString();
string email = e.Values["Email"].ToString();
// 插入数据库(伪代码)
// InsertEmployee(name, email);
fvEmployee.ChangeMode(FormViewMode.ReadOnly);
// 重新绑定数据...
}
关键注意事项
-
Bind()必须配合数据操作事件
如 GridView 的RowUpdating、FormView 的ItemInserting,在这些事件中通过e.Values或控件查找获取值。 -
数据源要求
若使用SqlDataSource等声明式数据源,Bind()可自动处理更新,无需手动编码。
-
视图状态开销
Bind()依赖视图状态存储原始值,在大型数据集场景中需评估性能影响。 -
格式处理
可在表达式中直接格式化数据:
<%# Eval("HireDate", "{0:yyyy-MM-dd}") %> <!-- 日期格式化 --> <%# Bind("Salary", "{0:C}") %> <!-- 货币格式化 -->
通过合理使用 Eval() 和 Bind(),可以高效实现数据的显示和编辑功能。Eval() 适用于只读场景,而 Bind() 则是编辑/插入数据的首选方案。
图片来源于AI模型,如侵权请联系管理员。作者:酷小编,如若转载,请注明出处:https://www.kufanyun.com/ask/288375.html

