Choose Eval or Bind in a Web Forms template

Eval reads a value from the current data item. Bind also allows a data-bound control to extract an edited value during its update workflow.

Neither expression is a database update by itself. The surrounding control and data source, or your event handler, must perform persistence.

Display encoded values

In a .NET Framework 4.8.1 Web Forms page, place this GridView inside the server form:

HTML
<asp:GridView ID="Items" runat="server" AutoGenerateColumns="false">
  <Columns>
    <asp:TemplateField HeaderText="Name">
      <ItemTemplate><%#: Eval("Name") %></ItemTemplate>
      <EditItemTemplate>
        <asp:TextBox ID="NameEditor" runat="server" Text='<%# Bind("Name") %>' />
      </EditItemTemplate>
    </asp:TemplateField>
  </Columns>
</asp:GridView>

In the code-behind, bind a small source during initial loading:

C#
protected void Page_Load(object sender, EventArgs e) {
	if (!IsPostBack) {
		Items.DataSource = new[] { new { Name = "A < B" } };
		Items.DataBind();
	}
}

The page displays A < B as text. The encoded <%#: form prevents the value from becoming raw markup in the item template.

Complete editing through the control's workflow

The edit template illustrates the correct two-way expression but does not enable editing by itself. Add the intended edit command and update handling or a data source with an update command. Validate edited values and authorize the record before persisting them.

Bind is special template syntax rather than a general method to nest inside arbitrary expressions. Use Eval for read-only formatting. Rebinding unconditionally in Page_Load can overwrite posted input before an update handler reads it. Bind according to the control's lifecycle and data-source mode.

Official references

Data-Binding Expressions Overview
https://learn.microsoft.com/en-us/previous-versions/aspnet/ms178366(v=vs.100)
Data-binding expressions

Author: Herman Schoenfeld