.NET     Console.WriteLine( "All Things .NET" );
.NET Nerd Blog Home
3.12.2002

 

Lists, Grids, Repeaters



A good place to start is MSDN library comparison of functionalities.

  • Table gives programatic control of the HTML table element
  • Repeater is for simple, read-only output, layout list by creating templates
  • DataList provides data-binding output with editing, non-tabular lists, easily customizable output, WYSIWYG editing, horizontal or vertical layout
  • DataGrid provides full-featured data-binding output with editing, paging, auto-format, customizable with templates, edit/update/delete contents, sorting

Repeater, DataList, DataGrid are all considered "list" controls, and therefore have automatic data-binding capabilities. For the DataSource property, you can provide any object that supports the IEnumerable interface.

Table Server Control

Use this anytime you want to add rows/columns at runtime. Not inherently data-bound, have to do this yourself.
NOTE: Controls you add dynamically to a web forms page do not automatically become part of the page's view state. The controls and their values are not saved during a round-trip to the server. You are responsible for saving this state yourself.

TableRow tr = null;
TableCell tc = null;
for ( int j=0; j<2; j++ )
{
tr = new TableRow();
Table1.Rows.Add( tr );
for (int i = 0; i<3; i++ )
{
tc = new TableCell();
tc.Text = "Row " + j + ", Cell " + i;
tr.Cells.Add( tc );
}
// add a "control" to a cell instead of just plain text
tc = new TableCell();
tc.Controls.Add( new LiteralControl( "Search " ));
System.Web.UI.WebControls.HyperLink h = new HyperLink();
h.Text = "MSDN";
h.NavigateUrl = "http://msdn.microsoft.com";
tc.Controls.Add( h );
tr.Cells.Add( tc );
}


Repeater Server Control

Data bound container that produces a list of individual items. You define the layout of individual items on a web page using templates. When the page is run, the control repeats the layout for each item in the datasource.
This control does not inerently produce a table for output - it can produce any kind of list, such as:

  • table layout
  • comma separated list ( "a, b, c, d, ... " )
  • bulleted or numbered list


Templates can contain any combination of HTML text and controls.

  • ItemTemplate - rendered once for each row in data source.
  • AlternatingItemTemplate - rendered for every other row in the data source.
  • HeaderTemplate & FooterTemplate - rendered before and after all rows in the data source
  • SeparatorTemplate - render between each row, such as line breaks, <BR>, etc.


Add the following elements to the tag.

<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.T1NAME") %>
<%# DataBinder.Eval(Container, "DataItem.T1NID") %>
,
</ItemTemplate>


ItemCreated event is where you can customize each item before rendering.
RepeaterItemEventArgs - use e.Item.DataItem to get DataRowView object, then Row property.
e.Item.DataItem.Row[3] will give the 3rd column of the current row for this item in the repeater. (Couldn't get this to work)


DataList Server Control



Displays data in format you can define using templates, most useful for displaying rows of data. Optionally, you can configure to have edit or delete functionality, as well as selecting rows. DataList has all the templates the Repeater control has plus the following:


  • SelectedItemTemplate - elements to render when user selects this row. Typically to visually mark the row with a background color, or to expand the item by displaying additional fields from the datasource.
  • EditItemTemplate - layout of an item when it's in edit mode. Typically contains edit controls such as EditBox.


Item Layout


  • Flow vs. Table - flow renders items inline. Table renders into an HTML table, allowing you to set table cell properties.
  • Vertical vs. Horizontal ordering - by default, items are rendered in single vertical column. Can specify more than one vertical column, as well as ordered vertically (newspaper) or horizontally (calendar).
  • Number of columns


Not a lot of code to get this list to display, mostly design time stuff. Probably can adjust these properties at runtime too.
Right click on DataList, Edit Template, Item Templates. Now add any text, controls, etc. You can data-bind any control you put in there (such as Label, EditBox, etc). Properties on the control, DataBindings -- for the Text property, put in a custom binding expression like DataBinder.Eval(Container, "DataItem.T1NAME"). (Couldn't get the simple bindings to work -- it would just show DataRowView type name for all datasource rows)
When finished editing, right click on Templates, End Template Editing.

(... gave up on edit and select modes)

Create Templates Dynamically

Save a template as an .ascx file, then in Page_Init, call

DataList1.AlternatingItemTemplate = Page.LoadTemplate( "NewTemplate.ascx" );


DataGrid Server Control

ItemDataBound event - last chance to change anything about a given row in the table, as it's being bound.
Use e.Item.Visible = false to hide that row.
Use e.Item.Cells[3].Text for example to access the data in each cell of the current row.
Things like e.Item.Cells[2].Visible = false; e.Item.Cells[1].ColumnSpan = 2; also work!
Or change the color of a row with e.Item.BackColor = Color.Red




Comments: Post a Comment

Powered by Blogger