Ø ItemTemplate: Formats each item from the data source.
Ø AlternatingItemTemplate: formats every other item from the data source.
Ø SeparatorTemplate: Formats between each item from the data source.
Ø HeaderTemplate: Formats before all items from the data source.
Ø FooterTemplate: Formats after all items from the data source
In addition, the DataList supports the following templates:
Ø EditItemTemplate: Displayed when a row is selected for editing.
Ø SelectedItemTemplate: Displayed when a row is selected.
It has the following important properties and methods.
Properties:
Property
|
Description
|
DataSource
|
Used to specify the source of data for the DataList that can be a dataset or DataReader.
|
DataSourceID
|
Used to specify a DataSource control as source of data for the DataList.
|
RepeatColumn
|
Used to specify number of columns required in DataList to repeat items.
|
RepeatLayout
|
The layout to use to repeat items.
|
RepeatDirection
|
The direction to repeat the items.
|
EditItemIndex
|
Used to get or set index of the item that is selected.
|
DataKeyField
|
Used to specify the primary key columns of the table used as DataSource for the DataList.
|
SelectedIndex
|
Used to get or set index of the item that is selected.
|
SelectedValue
|
Used to get primary key column value of selected item.
|
SelectedItem
|
Used to get the selected item in DataList.
|
Items
|
Collection of items in DataList.
|
ShowHeader and ShowFooter
|
Indicates whether or not header and footer are displayed in DataList.
|
Events:
Event
|
Description
|
EditCommand
|
Raised when click on Edit button.
|
UpdateCommand
|
Raised when click on update button.
|
CancelCommand
|
Raised when click on cancel button.
|
DeleteCommand
|
Raised when click on delete button.
|
ItemCommand
|
Raised when click on any button.
|
SelectIndexChanged
|
Raised whenever an item is selected in DataList.
|
Example:-The following example demonstrates how to use DataList control.
1. Add a page to the website, take a DataList Control on it and within the html source of the page design layout for the DataList as follows.
<div style="border:solid 4px blue;width:910px;">
<table width="900px" align="center" >
<tr>
<td>
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand">
<HeaderTemplate>
<table width="900px">
<tr style="background-color:Blue;height:30px;color:White;font-size:14px">
<td width="150px">Title</td>
<td width="150px">Author</td>
<td width="150px">Publisher</td>
<td width="150px">Price</td>
</tr>
</table>
</HeaderTemplate>
<ItemTemplate>
<table width="900px" >
<tr>
<td width="150px"><%# Eval("booktit")%></td>
<td width="150px"><%# Eval("bookaut")%></td>
<td width="150px"><%# Eval("bookpub")%></td>
<td width="150px"><%# Eval("bookprc")%></td>
</tr>
</table>
</ItemTemplate>
<FooterTemplate>
<table width="900px">
<tr>
<td>
<asp:LinkButton ID="LnkInsert" runat="server" Text="Insert" CommandName="Insert" />
<asp:LinkButton ID="LnkCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</td>
</tr>
</table>
<div id="Insrt" runat="server">
<table width="900px">
<tr>
<td width="150px">
<asp:TextBox ID="Txtbit" runat="server" Text="<%#Bind('booktit') %>" />
</td>
<td width="150px">
<asp:TextBox ID="Txtbaut" runat="server" Text="<%#Bind('bookaut') %>" />
</td>
<td width="150px">
<asp:TextBox ID="Txtbpub" runat="server" Text="<%#Bind('bookpub') %>" />
</td>
<td width="150px">
<asp:TextBox ID="Txtbprc" runat="server" Text="<%#Bind('bookprc') %>" />
</td>
</tr>
</table>
</div>
</FooterTemplate>
</asp:DataList>
</td>
</tr>
<tr>
<td style="background-color:blue;">
<table id="tblPaging" runat="server" align="center" style="text-align:center;color:White;font-size:14px; background-color:Blue;">
<tr>
<td style="padding-right: 7px" valign="top" >
<asp:LinkButton ID="lnkbtnPrevious" ForeColor="White" runat="server" OnClick="lnkbtnPrevious_Click">Previous</asp:LinkButton>
</td>
<td valign="top">
<asp:DataList ID="dlPaging" runat="server" ForeColor="White" OnItemCommand="dlPaging_ItemCommand" OnItemDataBound="dlPaging_ItemDataBound"
RepeatDirection="Horizontal">
<ItemTemplate>
<asp:LinkButton ID="lnkbtnPaging" ForeColor="White" runat="server" CommandArgument='<%# Eval("PageIndex") %>'
CommandName="lnkbtnPaging" Text='<%# Eval("PageText") %>'></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</td>
<td style="padding-left: 7px" valign="top">
<asp:LinkButton ID="lnkbtnNext" ForeColor="White" runat="server" OnClick="lnkbtnNext_Click">Next</asp:LinkButton>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>
2. Now In the web.config file create the connection string as:
<connectionStrings><add name="constr" connectionString="uid=sa;pwd=nagesh;database=nagesh;" providerName="System.Data.SqlClient"/>
</connectionStrings>
3. Write the following code in code behind page.
string connn = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
PagedDataSource pds = new PagedDataSource();
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack == false)
{
bindDataList();
}
}
protected void lnkbtnPrevious_Click(object sender, EventArgs e)
{
CurrentPage -= 1;
bindDataList();
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
CurrentPage += 1;
bindDataList();
}
protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName.Equals("lnkbtnPaging"))
{
CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
bindDataList();
}
}
protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
{
LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
{
lnkbtnPage.Enabled = false;
lnkbtnPage.Font.Bold = true;
}
}
public int CurrentPage
{
get
{
if (this.ViewState["CurrentPage"] == null)
return 0;
else
return Convert.ToInt16(this.ViewState["CurrentPage"].ToString());
}
set
{
this.ViewState["CurrentPage"] = value;
}
}
private void doPaging()
{
DataTable dt = new DataTable();
dt.Columns.Add("PageIndex");
dt.Columns.Add("PageText");
for (int i = 0; i < pds.PageCount; i++)
{
DataRow dr = dt.NewRow();
dr[0] = i;
dr[1] = i + 1;
dt.Rows.Add(dr);
}
dlPaging.DataSource = dt;
dlPaging.DataBind();
}
void bindDataList()
{
SqlConnection con = new SqlConnection(connn);
con.Open();
string str = "select * from tbbook";
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
pds.DataSource = ds.Tables[0].DefaultView;
pds.AllowPaging = true;
//to set the paging change the number 3 to your desired value
pds.PageSize = 3;
pds.CurrentPageIndex = CurrentPage;
lnkbtnNext.Enabled = !pds.IsLastPage;
lnkbtnPrevious.Enabled = !pds.IsFirstPage;
DataList1.DataSource = pds;
DataList1.DataBind();
doPaging();
con.Close();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
Int32 bprc;
String btit, baut, bpub;
btit = ((TextBox)(e.Item.FindControl("Txtbit"))).Text;
baut = ((TextBox)(e.Item.FindControl("Txtbaut"))).Text;
bpub = ((TextBox)(e.Item.FindControl("Txtbpub"))).Text;
bprc = Convert.ToInt32(((TextBox)(e.Item.FindControl("Txtbprc"))).Text);
SqlConnection con = new SqlConnection(connn);
con.Open();
SqlCommand cmd = new SqlCommand("insert into tbbook(booktit,bookaut,bookpub,bookprc)values(@btit,@baut,@bpub,@bprc)", con);
cmd.Parameters.Add("@btit", SqlDbType.VarChar, 50).Value = btit;
cmd.Parameters.Add("@baut", SqlDbType.VarChar, 50).Value = baut;
cmd.Parameters.Add("@bpub", SqlDbType.VarChar, 50).Value = bpub;
cmd.Parameters.Add("@bprc", SqlDbType.VarChar, 50).Value = bprc;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
bindDataList();
}
}