ASP.NET Web Forms 4.5 supports model binding, which allows you to automatically populate an object with data from a request. When the model contains a collection, you can use the Bind
attribute to specify the prefix for the collection items in the request.
Here's an example of how to use the Bind
attribute to bind a collection property on a model:
public class MyModel { public int Id { get; set; } public string Name { get; set; } public List<ItemModel> Items { get; set; } } public class ItemModel { public int Id { get; set; } public string Name { get; set; } } public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { var model = new MyModel(); TryUpdateModel(model, new FormValueProvider(ModelBindingExecutionContext)); // ... } } }
In this example, we define a MyModel
class that contains a list of ItemModel
objects. In the Page_Load
method, we use the TryUpdateModel
method to populate a new instance of MyModel
with data from the request. We pass in a FormValueProvider
that specifies the source of the data for the model binding.
To bind the collection of ItemModel
objects, we use the Bind
attribute on the Items
property. The prefix item
specifies that the collection items should be prefixed with item
in the request, for example:
<input type="text" name="item[0].Id" value="1" /> <input type="text" name="item[0].Name" value="Item 1" /> <input type="text" name="item[1].Id" value="2" /> <input type="text" name="item[1].Name" value="Item 2" />
When the TryUpdateModel
method is called, it will automatically populate the Items
collection with two ItemModel
objects based on the data in the request.
Note that the Bind
attribute can also be used to specify other options for model binding, such as ignoring certain properties or including only certain properties.
Model Binding with Simple Collection in ASP.NET Web Forms 4.5:
public class MyModel { public List<string> Items { get; set; } }
Binding a GridView to a Collection Property in ASP.NET Web Forms:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" ItemType="MyModel" SelectMethod="GetData" />
Handling Collection Model Binding in ASP.NET Web Forms 4.5:
public class MyModel { public List<MyItem> Items { get; set; } } public class MyItem { public string Name { get; set; } // Other properties }
Model Binding with Nested Collection in ASP.NET Web Forms:
public class MyModel { public List<Category> Categories { get; set; } } public class Category { public string Name { get; set; } public List<Product> Products { get; set; } } public class Product { public string Name { get; set; } // Other properties }
Using Data Controls with Collection Model Binding:
<asp:Repeater ID="Repeater1" runat="server" ItemType="MyItem" SelectMethod="GetItems"> <ItemTemplate> <%# Item.Name %> </ItemTemplate> </asp:Repeater>
Handling Postback with Collection Model Binding:
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { // Access and update collection data // ... } }
Editing a Collection in ASP.NET Web Forms Model Binding:
// Handle editing and updating collection data protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { // Update collection data // ... }
Model Binding with Repeater Control and Collection Property:
<asp:Repeater ID="Repeater1" runat="server" ItemType="MyItem" SelectMethod="GetItems"> <ItemTemplate> <%# Item.Name %> </ItemTemplate> </asp:Repeater>
Model Binding with DropDownList and Collection Property:
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="ObjectDataSource1" DataTextField="Name" DataValueField="Id" />
Model Binding with Custom Collection Converter:
public class MyModel { [TypeConverter(typeof(MyItemConverter))] public List<MyItem> Items { get; set; } } public class MyItemConverter : TypeConverter { // Implement custom type conversion logic // ... }
in-app-billing url facebook-like uisearchbar filter embedded-documents timezone-offset maven-2 ef-database-first hibernate-criteria