开发者

Asp.net single value binding from a collection

开发者 https://www.devze.com 2023-04-12 03:47 出处:网络
Hello all I have a project where I need to perform single value bindings from a collection. I am trying to create an asp.net web form website with a certain degree of separation without going to mvc.

Hello all I have a project where I need to perform single value bindings from a collection. I am trying to create an asp.net web form website with a certain degree of separation without going to mvc. Within the site I have a page that invokes a method from a web service. This method returns a result collection. The page is designed to show the data in a structured layout vs grid or form view. Can someone point me in the right direction (links or sample ) upon how to perform single value bindings against a collection result in asp.net.

update

Just for clarification, the method will return a collection withi开发者_如何学编程n that collection I have the following properties (FirstName LastName Height Weight). Is it possible to bind the collection to a section within the page and then within that section display a certain property?

<div id="section1" DataSource="peopleCollection">
  <%# LastName %><br/>
  <span>Height: <%# Height %></span><br/>
  <span>Weight: <%# Weight %></span>
</div>

I'd like to do this using web forms and a pseudo implementation of MVC without using the MVC framework

Thanks in advance


Is 'peopleCollection' have a single element or multiple elements where each element has properties that need to be data-bound? If yes then you should able to use data bound control such as ListView/Repeater. What exactly is the issue here?

Or do you mean that 'peopleCollection' is really a dictionary (collection of name-value pairs) - so you have threes key into the dictionary i.e. height, wight and last-name? In such case, you can create dummy array with single element and bind it with repeater/list-view. For example,

in code-behind

// Assuming propertyCollection supports IDictionary<string, string>
var dummyArray = new IDictionary<string, string>[] { propertyCollection };
myControl.DataSource = dummyArray;
...
protected string GetValue(IDataItemContainer container, string propertyName)
{
   var properties = container.DataItem as IDictionary<string, string>;
   return properties[propertyName];
}

in mark-up

<asp:Repeater ID="myControl" runat="server">
 <asp:ItemTemplate>
   <div id="section1" DataSource="peopleCollection">
     <%# GetValue(Container, "LastName") %><br/>
     <span>Height: <%# GetValue(Container, "Height") %></span><br/>
     <span>Weight: <%# GetValue(Container, "Weight") %></span>
   </div>
 </asp:ItemTemplate>
</asp:Repeater>

EDIT: For single element (with respective properties), you can still use repeater based approach. All you have to do is to put a single element array (or any enumerable collection) and bind with the repeater (or similar control).

But really speaking, you don't really need data-binding with single element. You can also use markup such as

<div id="section1">
  <%= People.LastName %><br/>
  <span>Height: <%= People.Height %></span><br/>
  <span>Weight: <%= People.Weight %></span>
</div>

where People is a protected/public property exposing the single element.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号