Understanding ASP.NET Eval() and Bind()

前端 未结 2 1848
栀梦
栀梦 2020-11-30 19:42

Can anyone show me some absolutely minimal ASP.NET code to understand Eval() and Bind()?

It is best if you provide me with two separate cod

2条回答
  •  一整个雨季
    2020-11-30 20:23

    For read-only controls they are the same. For 2 way databinding, using a datasource in which you want to update, insert, etc with declarative databinding, you'll need to use Bind.

    Imagine for example a GridView with a ItemTemplate and EditItemTemplate. If you use Bind or Eval in the ItemTemplate, there will be no difference. If you use Eval in the EditItemTemplate, the value will not be able to be passed to the Update method of the DataSource that the grid is bound to.


    UPDATE: I've come up with this example:

    <%@ Page Language="C#" %>
    
    
    
    
        Data binding demo
    
    
        
    <%# Eval("Name") %>

    And here's the definition of a custom class that serves as object data source:

    public class CustomDataSource
    {
        public class Model
        {
            public string Name { get; set; }
        }
    
        public IEnumerable 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
        }
    }
    

提交回复
热议问题