Id e n tific a tio n o f ta b le s
VIEW CODE:
STEP1: go to source and write the following code
<div>
<HeaderTemplate>
<tr><th>EMPID</th>
<th>EMP NAME</th>
<th>DESIGNATION</th>
<th>DOJ</th>
<th>SALARY</th>
<th>DEPTNO</th></tr>
</HeaderTemplate>
<ItemTempla <tr>
<td><%#DataBinder.Eval(Container.DataItem,"empid")
<td><%#DataBinder.Eval(Container.DataItem,"salary")
%></td>
<td><%#DataBinder.Eval(Container.DataItem,"deptno")
%></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
</div>
Step-2: go to c# code window and write the following code using System;
public partial class _Default : System.Web.UI.Page {
SqlConnection con;
SqlDataAdapter da;
da = new SqlDataAdapter("select * from emp_details", con);
• Data list control is similar to repeater control
• Data list control provides very good designing facilities
• It provides editing facilities
• It provide adding new record facilities
• We can also delete any record using data list control
• It does not provide sorting facilities
• It does not provides paging facilities
• Execution of data list control is faster as compared with grid view and slow as compared with repeater control
• Properties with data list control:
• Data Source source from which we want to display the data in data list control
• Data Member : this property is used to set the table name of the dataset from which we want to display the data within the data list control
• EdititemIndex: this property is used to set the data list control in static mode or editable mode. If EdititemIndex is -1 then data list control will be displayed in editable mode otherwise if
• Gridlines: it is used to set whether gridlines are to be displayed within the data list or not
• Selected index: it is used to store index values of the row i.e.
selected by he user at runtime default value of selected index is -1
• Show Header : when set to true header template will be displayed to the user
When set to false header template will not be displayed to the user
• Show Footer: when set to true Footer Template will be displayed to the user
When set to false Footer Template will not be displayed to the user
• Events associated with data list control:
• ItemCommand: this event will be fired when user selects any selectable control within the data list
• This event has a class known as Data List “CommandEventArgs”
class
• This class has important properties like 1. CommandName
2. CommandArgument 3. Item
Item :this property stores the complete row from which item command event is raised
• Property with Item property:
• ItemIndex:this property stores the index value of the row from which item command event has been raised.
• Method or function with item property:
• FindControl(“Controlname /Id”) : This function is used to search the required control within the row from which Item Command event has been raised
• This function also can be used along with data list control
• Properties of data list control at runtime
• Items: this property stores the complete list of rows present within the data list
• This is a collection property
• Each rows can be identified using its index value
• Data list examples: displaying the details in data list control
Example1 with datalist control to display database data
</HeaderTemplate>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"empid") %>
<%#DataBinder.Eval(Container.DataItem,"empname") %>
<%#DataBinder.Eval(Container.DataItem,"designation") %>
<%#DataBinder.Eval(Container.DataItem,"doj") %>
<%#DataBinder.Eval(Container.DataItem,"salary") %>
<%#DataBinder.Eval(Container.DataItem,"deptno") %>
</ItemTemplate>
</asp:DataList>
</div>
Step-2:go to C# code window and write the following code
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class datalist : System.Web.UI.Page {
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e) {
//step-2 prepare datasource
con = new SqlConnection("user id=sa;password=sa;database=kanna;data source=localhost");
da = new SqlDataAdapter("select * from emp_details", con);
ds = new DataSet();
da.Fill(ds, "emp_details");
//step-3 attach database to databound control dl1.DataSource = ds;
dl1.DataMember = "emp_details";
dl1.DataBind();}}
example 2 to delete a record from database using datalist contro with command arguments
<HeaderTemplate>
EMPID EMP NAME DESIGNATION DOJ SALARY DEPTNO </HeaderTemplate>
<ItemTemplate>
<asp:Button ID="b1" runat="server" Text="Delete"
CommandArgument='<%#DataBinder.Eval(Container.DataItem,"empid")
%>' />
<%#DataBinder.Eval(Container.DataItem,"empid") %>
<%#DataBinder.Eval(Container.DataItem,"empname") %>
<%#DataBinder.Eval(Container.DataItem,"designation") %>
<%#DataBinder.Eval(Container.DataItem,"doj") %>
<%#DataBinder.Eval(Container.DataItem,"salary") %>
<%#DataBinder.Eval(Container.DataItem,"deptno") %>
</ItemTemplate>
</asp:DataList></div>
using System.Data;
public partial class datalist2 : System.Web.UI.Page {
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e) {
protected void dl1_ItemCommand(object source, DataListCommandEventArgs e)
{
empid="+e.CommandArgument.ToString();
SqlCommand cmd=new SqlCommand(s,con);
cmd.CommandType = CommandType.Text;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
GetData();
}}
Example 3 to delete a record from database using datalist contro with out using command arguments
• note: we can include any standard control within the data list control by writing the code in item template or Header template like
• <ItemTemplate>
<asp:Button ID="b1" Text="Delete" runat ="server" />
</ItemTemplate>
Step1:designing
<div>
<asp:DataList ID="DL3" runat="server"
OnItemCommand="DL3_ItemCommand">
<HeaderTemplate>
DELETE EMPID EMP NAME DESIGNATION DOJ SALARY DEPTNO </HeaderTemplate>
<ItemTemplate>
<asp:Button ID="CMDDELETE" Text="Delete" runat="server" />
<asp:Label ID="lblempid" runat="server" Text='<
%#DataBinder.Eval(Container.DataItem,"empid")%>'/>
<%#DataBinder.Eval(Container.DataItem,"empname") %>
<%#DataBinder.Eval(Container.DataItem,"designation") %>
<%#DataBinder.Eval(Container.DataItem,"doj") %>
<%#DataBinder.Eval(Container.DataItem,"salary") %>
<%#DataBinder.Eval(Container.DataItem,"deptno") %>
</ItemTemplate>
</asp:DataList>
</div>
Step-3:go to C# code window and write the following code
using System.Web.Security;
public partial class DATALIST3 : System.Web.UI.Page {
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e) {
protected void DL3_ItemCommand(object source, DataListCommandEventArgs e)
{
con = new SqlConnection("user
id=sa;password=sa;database=kanna;data source=localhost");
Label l1 = (Label)e.Item.FindControl("lblempid");
String s = "delete emp_details where empid=" + l1.Text;
SqlCommand cmd = new SqlCommand(s, con);
con.Open();
} }
N o te :
t o tr a p th e r o w : d a t a lis t c o m m a n d e v e n t a r g s c la s s h a s a p r o p e r t y k n o w n a s it e m
p r o p e r t y S O E . I te m w ill s to r e t h e r e s p e c t iv e r o w in w h ic h u s e r c lic k s o n d e le t e b u t t o n i. e . if u s e r c lic k s o n 1 0 3 e m p id r e c o r d d e le t e b u t t o n th e n e .I t e m w ill s t o r e
d e le t e 1 0 3 K a n n a T L 1 2 / 1 1 / 2 0 0 9 4 0 0 0 0 1 0
T o t r a p t h e la b e l:
W e u s e a f u n c t io n k n o w n a s F in d C o n t r o l w it h I t e m P r o p e r t y lik e e . I t e m . F in d C o n t r o l ( “lb le m p id ” ) 1 0 3
F in d c o n t r o l f u n c t io n w ill r e t u r n a n y c o n t r o l w it h c o n t r o l d a t a t y p e
• T o d is p la y d a t a lis t d a t a in e d it a b le m o d e t h e n s e t
• D A T L I S T I N S T A T IC M O D E
• D A T A L IS T IN E D IT A B L E M O D E
E M P I DE N A M ED E S I G N A T I O N D O J s a la r y D e p t n o
1 0 1 X x x X x x X x x X x x X x x
1 0 2 X x x X x x X x x X x x X x x
1 0 3 X x x X x x X x x X x x X x x
1 0 4 X x x X x x X x x X x x X x x
1 0 5 X x x X x x X x x X x x x x x
E D I T / S A V E E D I T
E D I T
E D I T
E D I T
E D I T
Step-1: design
Step2: go to source and write the following code
<div>
<asp:DataList ID="DL4" runat="server"
OnItemCommand="DL4_ItemCommand">
<HeaderTemplate>
EMPID EMP NAME DESIGNATION DOJ SALARY DEPTNO </HeaderTemplate>
<ItemTemplate>
<%#DataBinder.Eval(Container.DataItem,"empid") %>
<%#DataBinder.Eval(Container.DataItem,"empname") %>
<%#DataBinder.Eval(Container.DataItem,"designation") %>
<%#DataBinder.Eval(Container.DataItem,"doj") %>
<%#DataBinder.Eval(Container.DataItem,"salary") %>
<%#DataBinder.Eval(Container.DataItem,"deptno") %>
<asp:Button ID="cmdedit" runat="server" Text="edit"
CommandName="edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtdesignation" runat="server" Text='<
%#DataBinder.Eval(Container.DataItem,"designation")
%>'></asp:TextBox>
<asp:TextBox ID="txtdoj" runat="server" Text='<
%#DataBinder.Eval(Container.DataItem,"doj") %>'></asp:TextBox>
<asp:TextBox ID="txtsalary" runat="server" Text='<
%#DataBinder.Eval(Container.DataItem,"salary")
%>'></asp:TextBox>
<asp:TextBox ID="txtdeptno" runat="server" Text='<
%#DataBinder.Eval(Container.DataItem,"deptno")
%>'></asp:TextBox>
<asp:Button ID="cmdupdate" runat="server" Text="update"
CommandName="update" />
<asp:Button ID="cmdcancel" runat="server" Text="cancel"
CommandName="cancel" />
</EditItemTemplate>
</asp:DataList>
</div>
Step-3:go to C# code window and write the following code using System;
public partial class datalist4 : System.Web.UI.Page {
SqlConnection con;
SqlDataAdapter da;
DataSet ds;
protected void Page_Load(object sender, EventArgs e) {
if (Page.IsPostBack == false) {
private void GetData()
protected void DL4_ItemCommand(object source, DataListCommandEventArgs e)
{
if(e.CommandName.ToString()=="edit") {
DL4.EditItemIndex=e.Item.ItemIndex;
GetData();
}
else if(e.CommandName.ToString()=="cancel") {
DL4.EditItemIndex=-1;
GetData();
}
else if(e.CommandName.ToString()=="update") {
TextBox t1=(TextBox)e.Item.FindControl("txtename");
string mename=t1.Text;
t1=(TextBox)e.Item.FindControl("txtdesignation");
string mdesig=t1.Text;
con.Close();
DL4.EditItemIndex=-1;
GetData();
}}
}