DataList is a bound list control that displays items using templates. It is a customized
control.which doesn’t have a default representation of data.so we have to
provide structure.
DataList is Templeted based control.
HeaderTemplet,FooterTemplet,SperatorTemplet,ItemTemplet,AlternativeItemTemplet….are
properties of DataList control. To represent data in customized format.
DataList has good Properties:
RepeatDirection,RepeatColumns,EditItemIndex=-1,and
many style properties.
DataList shows all the data Items as a
List.so we don’t required any pagging for datalist control.
DataList is more customized control.which
supports Editing,Updating,Command,…ect
events
DataList
Overview
DataList members
public const string
CancelCommandName = "Cancel";
public const string
DeleteCommandName = "Delete";
public const string
EditCommandName = "Edit";
public const string
SelectCommandName = "Select";
public const string
UpdateCommandName = "Update";
when we customize our DataList then take
commandName as “Cancle,Delete,Edit,Select,Update are DataList events
public event DataListCommandEventHandler
CancelCommand;
public event DataListCommandEventHandler
DeleteCommand;
public event DataListCommandEventHandler
EditCommand;
public event DataListCommandEventHandler
ItemCommand;
public event DataListItemEventHandler ItemCreated;
public event DataListItemEventHandler ItemDataBound;
public event DataListCommandEventHandler
UpdateCommand;
DataList classes:
DataListItem:
Represents an item in a System.Web.UI.WebControls.DataList control.(like
gridviewRow, DetailsViewRow)
ListItemType(DataRowType):
Header = 0,
Footer = 1,
Item = 2,
AlternatingItem = 3,
SelectedItem = 4,
EditItem = 5,
Separator = 6,
Pager = 7,
DataList Demo:
Source
<asp:DataList ID="DataList1"
runat="server"
DataKeyField="Empid"
AlternatingItemStyle-BorderStyle="Dashed" BackColor="Aquamarine"
AlternatingItemStyle-BackColor="ButtonFace" OnItemDataBound="DataList1_ItemDataBound"
OnEditCommand="DataList1_EditCommand"
RepeatDirection="Horizontal" ShowFooter="False" ShowHeader="False" RepeatColumns="1"
SelectedIndex="4">
<ItemTemplate>
<table>
<tr>
<td colspan="2">
<b>Name
<%# Eval("Empname")%></b>
</td>
</tr>
<tr>
<td colspan="2">
Designation
:
<%# Eval("EmpDesignation")%>
Address :
<%# Eval("EmpAddress")%><br />
</td>
</tr>
<tr>
<td>
country:
</td>
<td>
<%# Eval("EmpJoindate")%>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton1"
runat="server"
Text="Edit"
CommandName="edt"
CommandArgument='<%#Eval("Empid")
%>'></asp:LinkButton>
</td>
</tr>
<tr>
</table>
</ItemTemplate>
<AlternatingItemStyle BorderStyle="Dashed"></AlternatingItemStyle>
<EditItemTemplate>
<table>
<tr>
<td colspan="2">
<asp:TextBox ID="TextBox1"
runat="server"
Text='<%#
Eval("Empname")%>'></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="TextBox2"
runat="server"
Text='<%#
Eval("EmpDesignation")%>'></asp:TextBox>
<asp:TextBox ID="TextBox3"
runat="server"
Text=' <%# Eval("EmpAddress")%>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
country:
</td>
<td>
<asp:TextBox ID="TextBox4"
runat="server"
Text='<%#
Eval("EmpJoindate")%>'></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="LinkButton2"
runat="server"
CommandName="upt"
CommandArgument='<%#Eval("Empid")
%>'>Update</asp:LinkButton>
</td>
</tr>
</table>
</EditItemTemplate>
</asp:DataList>Design
Code Behind
SqlConnection con = new SqlConnection("uid=sa;pwd=zolt123$;database=PropercodeDB");
DataSet
ds;
SqlDataAdapter
da;
protected void Page_Load(object
sender, EventArgs e)
{
}
public void EmployeeDetails()
{
da = new
SqlDataAdapter("select
* from dbo.EmployeeDetails", con);
ds = new
DataSet();
da.Fill(ds);
DataList1.DataSource = ds;
DataList1.DataBind();
}
protected void DataList1_ItemDataBound(object
sender, DataListItemEventArgs e)
{
if
(e.Item.ItemType == ListItemType.Header ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
e.Item.ForeColor = Color.Red;
}
}
protected void Button1_Click(object
sender, EventArgs e)
{
EmployeeDetails();
}
protected void DataList1_EditCommand(object
source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex =
e.Item.ItemIndex;
EmployeeDetails();
}
Note: Don’t use ItemCommand evnet for Crud
operations.Itemcommand event for handling Inner controls/child control events.
No comments:
Post a Comment