Asp.net CompositeDataBoundControl for a single object

I created a CompositeDataBoundControl so that I can perfectly bind data. Now I want to do the same, but not for a list of objects, but for a single object. The reason is that I want my colleagues to be able to simply use <% # Eval ("X")%> in their front-end code.

The problem is that CompositeDataBoundControl has a method that I have to override, which only accepts a collection as a data source

CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) 

Is there a way to do the same for a single object?

+6
source share
3 answers

You need to override the DataSource property.

 public class SingleObjectView : CompositeDataBoundControl { private object dataSource; public override object DataSource { get { return new List<object> { dataSource }; } set { dataSource = value; } } protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding) { SingleItem singleItem = null; if (dataBinding) { var it = dataSource.GetEnumerator(); it.MoveNext(); singleItem = new SingleItem(it.Current); } else { singleItem = new SingleItem(null); } ItemTemplate.InstantiateIn(singleItem); Controls.Add(singleItem); if (dataBinding) singleItem.DataBind(); return 1; } [PersistenceMode(PersistenceMode.InnerProperty), TemplateContainer(typeof(SingleItem))] public ITemplate ItemTemplate { get; set; } } public class SingleItem : Control, IDataItemContainer { public SingleItem(object dataItem) { DataItem = dataItem; } public object DataItem { get; set; } public int DataItemIndex { get { return 0; } } public int DisplayIndex { get { return 0; } } } 

edit to show usage

 <ctrl:SingleObjectView ID="productView" runat="server"> <ItemTemplate> <%# Eval("ProductName") %> <br /> <%# Eval("Price","{0:c}") %> </ItemTemplate> </ctrl:SingleObjectView> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { productView.DataSource = new { ProductName="My Product", Price="10"}; productView.DataBind(); } } 

So, the DataSource receives an object that does not implement IEnumerable , but overriding the DataSource property of the SingleObjectView control, ensures that CreateChildControls is called.

+4
source

How to create a list of controls, add one object and call a method?

+2
source

I use FormView to create a template for a single object:

  protected void fvProduct_Init(object sender, EventArgs e) { // Load product template fvProduct.ItemTemplate = LoadTemplate("Templates/ProductTemplate.ascx"); // Bind product to data source fvProduct.DataSource = new[] { Product }; fvProduct.DataBind(); } 

Then in ProductTemplate.ascx you can do the following:

 <h1><%# Eval("ProductName") %></h1> <p><%# Eval("Description") %></p> <h4><%# Eval("Price", "{0:C}")%></h4> 

In your scenario, you mentioned that you are using a user control, but the idea is basically the same as the answers above. You simply create an enumeration with one element and bind it. The reason for using FormView is that it is designed to display only one item at a time.

0
source

Source: https://habr.com/ru/post/909828/


All Articles