ASP.NET & Databases Part 4
page 3 of 5
by . .
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 22696/ 55

The DataList Control

DataList

The DataList is very much like the Repeater but the DataList supports editing of data. You use the templates like we did with the repeater, but there are two new ones :

  • SelectedItemTemplate. This is the template the shows up when you select a row.
  • EditItemTemplate. This is what shows up when you select a row for editing.

Here is a very simple use of it.

dl.DataSource = ds.Tables("users").DefaultView
Databind()
End Sub

Sub dl_ItemCommand(sender as object, e as DataListCommandEventArgs)
    dl.SelectedIndex = e.Item.ItemIndex
    dl.DataBind()
End Sub
</script>

<html><body><font face="arial" size="2"><form runat="server">
<asp:datalist id="dl" runat="server"
     HeaderStyle-BackColor="#6699FF"
     SelectedItemStyle-BackColor="#6666FF"
     SelectedItemStyle-ForeColor="#FFFFFF"
     RepeatLayout = "table"
     RepeatDirection = "vertical"
     DataKeyField = "ID"
     OnItemCommand="dl_ItemCommand">

<HeaderTemplate>
     Last Name, click for full name.
</HeaderTemplate>

<ItemTemplate>
     <asp:linkbutton id="b1" runat="server" Text='<%# Container.DataItem("LName") %>' CommandName = "select" />
     <br>
</ItemTemplate>

<SelecteditemTemplate>
     <%# Container.DataItem("LName") & ", " & Container.DataItem("FName") %>
     <br>
</SelectedItemTemplate>
</asp:datalist>
</form></font>
</body>
</html>

This is a rather large piece of code, but here is the explanation....

  • After the databinding we put a new procedure (this will be explained soon).
  • We enclose the DataList in a form tag because it needs to reload the page to select items.
  • We define properties of the DataList -
    • HeaderStyle-BackColor The background of the Header (instead of putting it in the table code).
    • SelectedItem-BackColor The background of the Selected Item.
    • RepeatLayout This is how the DataList should be laid out - either 'table' or 'flow' (flow is just nothing ie. You define exactly how you want it.)
    • RepeatDirection The direction in which to display the items (horizontal or vertical).
    • DataKeyField The primary key to use, this helps when selecting and editing data.
    • OnItemCommand Explained Later.
  • HeaderTemplate This is just a very simple Header column.
  • ItemTemplate The link button is a link that serves as a submit type button (but text-only). You display the Last Name and the CommandName of "Select" sends the command "Select" to the DataList (which then performs whatever it needs to do.
  • SelectedItemTemplate Shows the First and Last Name.
  • Then we close up everything

This is quite complicated and there are many unexplained things, here is an explanation for one of them :

Most of these controls have events that happen when you do something, like when you click on an item or automatic ones like when an item is created. All of these events can have event handlers. In the above example we used an event handler for the ItemCommand event. It takes in the usually parameters with one difference - its DataListCommandEventArgs and not EventArgs, this provides you with more properties that are specialized for use with the DataList.

dl.SelectedIndex tells the DataList what Item to put the selected template on.
e.Item.ItemIndex returns the index of the Item that was selected.

There are many more events for Data Viewers and I hope to provide them in a future part.

But that wraps up the DataList, don't be afraid to experiment with properties and ways to view data.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2021 ASPAlliance.com  |  Page Processed at 2021-11-29 9:38:53 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search