DataList is a data bound list control that displays items using certain templates defined at the design time. The content of the DataList control is manipulated by using templates sections such as AlternatingItemTemplate, EditItemTemplate, FooterTemplate, HeaderTemplate, ItemTemplate, SelectedItemTemplate and SeparatorTemplate. Each of these sections has its own characteristics to be defined but at a very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Other sections can be used to provide additional look and feel to the DataList control.
The contents of the DataList control can be manipulated by using templates. The following table lists the supported templates.
The DataList control displays data items in a repeating list, and optionally supports selecting and editing the items. The content and layout of list items in DataList is defined using templates. At a minimum, every DataList must define an ItemTemplate; however, several optional templates can be used to customize the appearance of the list.
AlternatingItemTemplate: If defined, provides the content and layout for alternating items in the DataList. If not defined, ItemTemplate is used.
EditItemTemplate: If defined, provides the content and layout for the item currently being edited in the DataList. If not defined, ItemTemplate is used.
FooterTemplate: If defined, provides the content and layout for the footer section of the DataList. If not defined, a footer section will not be displayed.
HeaderTemplate: If defined, provides the content and layout for the header section of the DataList. If not defined, a header section will not be displayed.
ItemTemplate: Required template that provides the content and layout for items in the DataList.
SelectedItemTemplate: If defined, provides the content and layout for the currently selected item in the DataList. If not defined, ItemTemplate is used.
SeparatorTemplate: If defined, provides the content and layout for the separator between items in the DataList. If not defined, a separator will not be displayed.
At the very minimum, the ItemTemplate needs to be defined to display the items in the DataList control. Additional templates can be used to provide a custom look to the DataList control.
The appearance of the DataList control may be customized by setting the style properties for the different parts of the control. The following table lists the different style properties.
AlternatingItemStyle: Specifies the style for alternating items in the DataList control.
EditItemStyle: Specifies the style for the item being edited in the DataList control.
FooterStyle: Specifies the style for the footer in the DataList control.
HeaderStyle: Specifies the style for the header in the DataList control.
ItemStyle: Specifies the style for the items in the DataList control.
SelectedItemStyle: Specifies the style for the selected item in the DataList control.
SeparatorStyle: Specifies the style for the separator between the items in the DataList control.
You can also show or hide different parts of the control. The following table lists the properties that control which parts are shown or hidden.
ShowFooter: Shows or hides the footer section of the DataList control.
ShowHeader: Shows or hides the header section of the DataList control.
The display direction of a DataList control can be vertical or horizontal. Set the RepeatDirection property to specify the display direction.
The layout of the DataList control is controlled with the RepeatLayout property. Setting this property to RepeatLayout.Table will display the DataList in a table format, while RepeatLayout.Flow displays the DataList without a table structure.
<asp:DataList ID=" dlMyCountry" runat="server">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Country_Code") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Country_Name") %>'></asp:Label>
</ItemTemplate>
</asp:DataList>
private void BindGrid()
{
string sql = "Select * from Country Order By Country_Name";
SqlDataAdapter da = new SqlDataAdapter(sql, “Yourconnectionstring”);
DataTable dt = new DataTable();
da.Fill(dt);
dlMyCountry.DataSource = dt;
dlMyCountry.DataBind();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
The DataList, DataGrid and Repeater control are similar in their behavior; they all connect to a data source from which they retrieve data to display as a repeated list of items. The DataList control uses HTML tables to format the data provided by the data source. The data source can be a SQL database, an XML file or an array.
Datalist Control in ASP.Net
Hot on Web: