Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Complete List View</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1"
onsorting="ListView1_Sorting" InsertItemPosition="LastItem"
onitemcommand="ListView1_ItemCommand" >
<LayoutTemplate>
<table border="0" cellpadding="1">
<tr style="background-color:#E5E5FE">
<th align="left"><asp:LinkButton ID="lnkId" runat="server" CommandName="Sort" CommandArgument="ID">ID</asp:LinkButton></th>
<th align="left"><asp:LinkButton ID="lnkName" runat="server" CommandName="Sort" CommandArgument="FirstName">NAME</asp:LinkButton></th>
<th align="left"><asp:LinkButton ID="lnkType" runat="server" CommandName="Sort" CommandArgument="ContactType">AGE</asp:LinkButton></th>
<th></th>
</tr>
<tr id="itemPlaceholder" runat="server"></tr>
</table>
<asp:DataPager ID="ItemDataPager" runat="server" PageSize="5">
<Fields>
<asp:NumericPagerField ButtonCount="2" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:Label runat="server" ID="lblId"><%#Eval("id") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblName"><%#Eval("name")%></asp:Label></td>
<td><asp:Label runat="server" ID="lblType"><%#Eval("age") %></asp:Label></td>
<td><asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:#EFEFEF">
<td><asp:Label runat="server" ID="lblId"><%#Eval("id") %></asp:Label></td>
<td><asp:Label runat="server" ID="lblName"><%#Eval("name")%></asp:Label></td>
<td><asp:Label runat="server" ID="lblType"><%#Eval("age") %></asp:Label></td>
<td><asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Edit</asp:LinkButton></td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<td>
<asp:TextBox ID="txtUpId" runat="server" Text='<%#Eval("id") %>' Enabled="false" Width="20px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtUpFname" runat="server" Text='<%#Eval("name") %>' Width="100px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtUpCtype" runat="server" Width="100px" Text='<%#Eval("age") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr runat="server">
<td></td>
<td>
<asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("id") %>' Width="100px">id</asp:TextBox>
<asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("name") %>' Width="100px"> Name</asp:TextBox>
</td>
<td><asp:TextBox ID="txtCtype" runat="server" Text='<%#Eval("age") %>' Width="100px">age</asp:TextBox></td>
<td><asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" /></td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:gridConnectionString2 %>"
SelectCommand="SELECT * FROM [emp]"></asp:SqlDataSource>
<br />
<br />
</div>
</form>
</body>
</html>
Default.aspx.cs
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)
{
}
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
TextBox txtFname = (TextBox)e.Item.FindControl("txtFname");
TextBox txtLname = (TextBox)e.Item.FindControl("txtLname");
TextBox txtCtype = (TextBox)e.Item.FindControl("txtCtype");
string insertCommand = "Insert into [emp] ([id],[name],[age]) Values('" + txtFname.Text + "', '" + txtLname.Text + "', '" + txtCtype.Text + "');";
SqlDataSource1.InsertCommand = insertCommand;
}
else if (e.CommandName == "Update")
{
TextBox txtId = (TextBox)e.Item.FindControl("txtUpId");
TextBox txtFname = (TextBox)e.Item.FindControl("txtUpFname");
//TextBox txtLname = (TextBox)e.Item.FindControl("txtUpLname");
TextBox txtCtype = (TextBox)e.Item.FindControl("txtUpCtype");
string updateCommand = "Update [emp] set [name]='"+txtFname.Text+"', [age]='"+txtCtype.Text+"' where id="+Convert.ToInt32(txtId.Text)+";";
SqlDataSource1.UpdateCommand = updateCommand;
}
else if (e.CommandName == "Delete")
{
TextBox txtId = (TextBox)e.Item.FindControl("txtUpId");
string deleteCommand = "delete from [emp] where id="+Convert.ToInt32(txtId.Text);
SqlDataSource1.DeleteCommand = deleteCommand;
}
}
}
No comments:
Post a Comment