Binding a Scalar Array to a Data Web Control

本文探讨了如何在ASP.NET的数据网格(DataGrid)、数据列表(DataList)和重复器(Repeater)中显示标量数组,如整数数组。通过设置数据源和使用特定的绑定语法,可以轻松地在这些控件中展示数组数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ref: http://aspnet.4guysfromrolla.com/demos/printPage.aspx?path=/articles/082504-1.aspx


Introduction
The ASP.NET data Web controls - the DataGrid, DataList, and Repeater - are highly versatile controls that can display arbitrary collections of data. Specifically, these controls can display any data that implements either the IEnumerable interface or the IListSource interface, which include things like arrays, collections (like ArrayLists, Hashtables, Queues, custom strongly-typed collection classes, and so forth), DataReaders, DataTables, and DataSets. These objects are bound to a data Web control through two lines of code:

  1. The object is assigned to the data Web control's DataSource property,
  2. The data Web control's DataBind() method is called.
At this point, the data Web control enumerates the DataSource one record at a time. Typically each record has a set of fields. For a database query, each record is a row returned from the database, and each field is a column returned by the SQL query. For a custom, strongly-typed collection of business objects, each record is a business object instance and the fields are the public properties of the business object. (See Displaying Custom Classes in a DataGrid for more information on displaying custom objects in a data Web control.)

The syntax for displaying a particular field in a data Web control depends upon the data Web control being used. For a DataGrid, you can use a BoundColumn, setting its DataField property to the name of the field. For a DataList or Repeater (or a DataGrid's TemplateColumn), you use the databinding syntax like so: <%# DataBinder.Eval(Container.DataItem, "fieldName") %>. But how do you display data bound that the data Web control that has no fields? For example, a scalar array - such as an array of integers - does not contain any fields, per se, but can definitely be bound to a data Web control, since arrays can be bound to data Web controls. But what's the syntax to display the array values? In this article we'll examine how to accomplish this!

Displaying a Scalar Array with a DataGrid
As with rich objects like DataSets or DataReaders, the steps for displaying an array of scalar values differs whether you're using a DataGrid or if you're using a DataList or Repeater. Let's first look at displaying a scalar array in a DataGrid. With the DataGrid there are two options:

  1. Let the DataGrid automatically generate the columns.
  2. Generate the columns explicitly, using a single TemplateColumn.
For option 2 the syntax is precisely the same as the syntax used for the DataList or Repeater controls, so we'll save that discussion for later. For the first option, the simple solution is to leave the AutoGenerateColumns property to its default value - True. (If you use the Property Builder through Visual Studio .NET, simply leave the "Create columns automatically at runtime" checkbox checked.) This will create a DataGrid with a single row whose Header has the text "Item". (You might want to set ShowHeader to False.)

The following code snippet shows an example ASP.NET Web page that binds an integer array to a DataGrid with its AutoGenerateColumns property set to True, followed by the output.

<script runat="server">
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim i as Integer
    Dim fib(10) As Integer

    fib(0) = 1
    fib(1) = 1

    For i = 2 To 10
        fib(i) = fib(i - 1) + fib(i - 2)
    Next

    Me.dgArrayAutoGenerate.DataSource = fib
    Me.dgArrayAutoGenerate.DataBind()
End Sub
</script>

<asp:DataGrid id="dgArrayAutoGenerate" runat="server"
       AutoGenerateColumns="True">
</asp:DataGrid>

[View a live demo!]

As you can see in the live demo, the DataGrid displays a single column with the values of the integer array.

Displaying a Scalar Array with Template
While using the DataGrid's AutoGenerateColumns functionality is nice, there are times when you might want to display a scalar array in a Repeater or DataList, as to have more control over the HTML markup that surrounds the scalar array's values. Displaying the value of each scalar array item in a DataList or Repeater - or in a TemplateColumn in the DataGrid - is fairly simple, just use the following databinding syntax: <# Container.DataItem %>. That's it! The following example illustrates displaying an integer array in a DataList.

<script runat="server" language="C#">
private void Page_Load(object sender,  System.EventArgs e)
{
    int [] fib = new int[11];
    
    fib[0] = 1;
    fib[1] = 1;

    for (int i = 2; i < 11; i++)
        fib[i] = fib[i - 1] + fib[i - 2];

    dlFibs.DataSource = fib;
    dlFibs.DataBind();
}
</script>

<asp:DataList id="dlFibs" runat="server" RepeatColumns="3"
     GridLines="Both" CellPadding="10" RepeatDirection="Horizontal">
  <ItemTemplate>
    <b><%# Container.DataItem %></b>
  </ItemTemplate>
</asp:DataList>

[View a live demo!]

As you can see in the live demo, the integer array is displayed in a three-columned DataList control, each value displayed in bold.

Conclusion
While typically the data you'll display in a data Web control will have fields, there may be times where you want to display a scalar array. As we saw in this article, it's quite possible to accomplish this with not only the DataGrid, but also with the DataList and Repeater.

Happy Programming!

转载于:https://www.cnblogs.com/javafun/archive/2008/03/20/1114101.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值