Binding a Scalar Array to a Data Web Control
2008-03-20 08:55
369 查看
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
At this point, the data Web control enumerates the
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: Let the DataGrid automatically generate the columns. 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
[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
[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!
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
IEnumerableinterface or the
IListSourceinterface, 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: The object is assigned to the data Web control's
DataSourceproperty, The data Web control's
DataBind()method is called.
At this point, the data Web control enumerates the
DataSourceone 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
DataFieldproperty 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: Let the DataGrid automatically generate the columns. 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
AutoGenerateColumnsproperty 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
ShowHeaderto False.) The following code snippet shows an example ASP.NET Web page that binds an integer array to a DataGrid with its
AutoGenerateColumnsproperty 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
AutoGenerateColumnsfunctionality 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!
相关文章推荐
- Using .NET Databinding to Bind an Editable InkPicture Control to a Database
- Coursera Using python to access Web data
- LINQ to SQL系列Part 5 - Binding UI using the ASP:LinqDataSource Control
- 如何用.Net 取得指定网面的内容? How to get html web page data?
- Control.DataBinding数据绑定细解
- How to pass ViewState value from web form page to web user control page?
- Coursera Using python to access Web data quiz 5
- Adding Client Capabilities to a Web Server Control
- 关于Flex警告:-Data binding will not be able to detect assignments to...
- How to use CreateChildContorls method inherited from System.Web.UI.Control
- 试着翻译一下ScottGu's Blog,LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
- Python Web-第二周-正则表达式(Using Python to Access Web Data)
- DataGird导出EXCEL的几个方法(WebControl)
- How to use HttpWebRequest to post data to another page which is on another server
- WebDriverAgent · How to remote control iOS devices?
- BIRT data.engine.ColumnBindingReferToInexistColumn 和 Group.NewTabularHierarchy。错误异常
- 【转】Control.DataBinding数据绑定细解
- LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
- 错误解决:SharePoint Designer 2010编辑后,出现数据源控件未能执行插入命令,data source control failed to execute the insert command
- using iscroll.js and iscroll jquery plugin in android webview to scroll div and ajax load data.