Data List Operation.
How Datalist is binded with backend and edit,delete operation done
in back end using Data list.Edit is done with help od Edit Item Template..
And Following with Link button..Please find below coding.
DataList Operation
Data List is used display the repeated list of item.We Can edit ,delete,Update the data using list.However, the DataList control adds a table around the data items by default. we create a DataList in an .aspx page. The contents of the <HeaderTemplate> element are rendered first and only once within the output, then the contents of the <ItemTemplate> element are repeated for each "record" in the DataSet, and last, the contents of the <FooterTemplate> element are rendered once within the output:Here Is the Example
In Html Coding:
<asp:DataList ID="DataList1" runat="server" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Vertical">
<ItemTemplate >
ID:
<asp:Label ID="id" runat="server"
Text=' <%# Eval("ID") %>'>
<asp:Label>
<br >
Salary:
<asp:Label ID="salary" runat="server"
Text=' <%# Eval("Salary") %>'>
<asp:Label>
<br >
YearofSalary:
<asp:Label ID="yearofsalary" runat="server"
Text=' <%# Eval("YearofSalary") %>'>
<asp:Label>
<br />
<asp:LinkButton runat="server" ID="LinkButton1"
CommandName="edit" >
Edit
<asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server" CommandName="delete">Delete </asp:LinkButton>
<ItemTemplate>
<EditItemTemplate >
ID: <asp:Label ID="Label1" runat="server"
Text=' <%# Eval("id") %>'>
<asp:Label>
<br >
Salary: <asp:TextBox ID="textCategoryName" runat="server"
Text=' <%# Eval("salary") %>'>
<asp:TextBox>
<br >
YearofSalary: <asp:TextBox ID="textDescription"
runat="server"
Text=' <%# Eval("yearofsalary") %>'>
<asp:TextBox>
<br >
<asp:LinkButton ID="LinkButton4" runat="server"
CommandName="update" >
Save
<asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server"
CommandName="cancel" >
Cancel
<asp:LinkButton>
<EditItemTemplate>
<AlternatingItemStyle BackColor="#DCDCDC" >
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" >
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" >
<ItemStyle BackColor="#EEEEEE" ForeColor="Black" >
<SelectedItemStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" >
<asp:DataList>In Vb Coding:
Imports System.Data.SqlClient
Partial Class _Default
Inherits System.Web.UI.Page
Dim con As SqlConnection = New SqlConnection("SERVER=192.164.4.178\Sql2008;DATABASE=master;uid=sa;pwd=sfsaer;")
Sub bindata()
con.Open()
Dim da As SqlDataAdapter = New SqlDataAdapter("Select * from employee_salary", con)
Dim ds As Data.DataSet = New Data.DataSet()
da.Fill(ds)
DataList1.DataSource = ds
DataList1.DataBind()
con.Close()
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
bindata()
End If
End Sub
Protected Sub DataList1_CancelCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.CancelCommand
DataList1.EditItemIndex = -1
bindata()
End Sub
Protected Sub DataList1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.DeleteCommand
con.Open()
Dim com As SqlCommand = New SqlCommand("delete from employee_salary where id=" + CType(e.Item.FindControl("id"), Label).Text + "", con)
com.ExecuteNonQuery()
con.Close()
bindata()
End Sub
Protected Sub DataList1_EditCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.EditCommand
DataList1.EditItemIndex = e.Item.ItemIndex
bindata()
End Sub
Protected Sub DataList1_UpdateCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataListCommandEventArgs) Handles DataList1.UpdateCommand
con.Open()
Dim com As SqlCommand = New SqlCommand("update employee_salary set salary=" + CType(e.Item.FindControl("textCategoryName"), TextBox).Text + ",yearofsalary=" + CType(e.Item.FindControl("textDescription"), TextBox).Text + " where id=" + CType(e.Item.FindControl("Label1"), Label).Text + " ", con)
com.ExecuteNonQuery()
con.Close()
DataList1.EditItemIndex = -1
bindata()
End Sub
End Class