Monday, September 13, 2010

List view with all possible functions

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="exp11.aspx.cs" Inherits="examples_Post.exp11" %>


<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSourcelv" runat="server" ConnectionString="<%$ ConnectionStrings:masterConnectionString %>"
SelectCommand="SELECT * FROM [student]"></asp:SqlDataSource>
<asp:ListView ID="lv" runat="server" onitemcommand="lv_ItemCommand"
onsorting="lv_Sorting">
<%--//Layout template makes the header of the control--%>
<LayoutTemplate>
<table border="0" cellpadding="2" cellspacing="0">
<tr style="background-color:Silver">
<th align="left">
<asp:LinkButton ID="lbId" runat="server" CommandName="Sort" CommandArgument="ID">id</asp:LinkButton>
</th>
<th align="left">
<asp:LinkButton ID="lbname" runat="server" CommandName="Sort" CommandArgument="Name">name</asp:LinkButton>
</th>
<th align="left">
<asp:LinkButton ID="lbdob" runat="server" CommandName="Sort" CommandArgument="Dataofbirth">dob</asp:LinkButton>
</th>
<th align="left">
<asp:LinkButton ID="lbfee" runat="server" CommandName="Sort" CommandArgument="Fee">fees</asp:LinkButton>
</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
<asp:DataPager ID="ItemDataPager" runat="server" PageSize="3">
<Fields>
<asp:NumericPagerField ButtonCount="2" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<%--//Item template binds the coulmns of the control with label control by taking the data from the table--%>
<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="lbldob"><%#Eval("dob")%></asp:Label>
</td>
<td>
<asp:Label runat="server" ID="lblfee"><%#Eval("fees")%></asp:Label>
</td>
</tr>
</ItemTemplate>
<%--//Alternate template does the same as item template , but, changes the style properties of the alternating columns--%>
<AlternatingItemTemplate>
<tr style="background-color: Gray">
<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="lbldob"><%#Eval("dob")%></asp:Label>
</td>
<td>
<asp:Label runat="server" ID="lblfee"><%#Eval("fees")%></asp:Label>
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<td>
<asp:TextBox ID="txtId" runat="server" Text='<%#Eval("id") %>' Enabled="false" Width="30px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>' Width="50px"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtdob" runat="server" Width="60px" Text='<%#Eval("dob") %>'></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtfee" runat="server" Width="60px" Text='<%#Eval("fees") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="lbDelete" runat="server" CommandName="Delete">Delete</asp:LinkButton>
<asp:LinkButton ID="lbCancel" runat="server" CommandName="Cancel">Cancel</asp:LinkButton>
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr id="Tr" runat="server">
<td>
</td>
<td>
<asp:TextBox ID="txtid" runat="server" Text='<%#Eval("id") %>' Width="100px">id</asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtname" runat="server" Text='<%#Eval("name") %>' Width="100px">name</asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtfee" runat="server" Text='<%#Eval("fees") %>' Width="100px">fees</asp:TextBox>
</td>
<td>
<asp:Button ID="InsertBtn" runat="server" CommandName="Insert" Text="INSERT" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
===
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace examples_Post
{
public partial class exp11 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void lv_Sorting(object sender, ListViewSortEventArgs e)
{

}

protected void lv_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
TextBox txtid = (TextBox)e.Item.FindControl("txtid");
TextBox txtname = (TextBox)e.Item.FindControl("txtname");
TextBox txtfees = (TextBox)e.Item.FindControl("txtfees");
string insertCommand = "Insert into [student] ([id],[name],[fees]) Values(" + txtid.Text + "," + txtname.Text + ", '" + txtfees.Text + "');";
SqlDataSourcelv.InsertCommand = insertCommand;
}
else if (e.CommandName == "Update")
{
TextBox txtid = (TextBox)e.Item.FindControl("txtid");
TextBox txtname = (TextBox)e.Item.FindControl("txtname");
TextBox txtfees = (TextBox)e.Item.FindControl("txtfees");
string updateCommand = "Update [student] set [name]='" + txtname.Text + "', [fees]='" + txtfees.Text + "' where id=" + Convert.ToInt32(txtid.Text) + ";";
SqlDataSourcelv.UpdateCommand = updateCommand;
}
else if (e.CommandName == "Delete")
{
TextBox txtid = (TextBox)e.Item.FindControl("txtid");
string deleteCommand = "delete from [student] where id=" + Convert.ToInt32(txtid.Text);
SqlDataSourcelv.DeleteCommand = deleteCommand;
}
}
}
}

0 comments:

Post a Comment

Related Posts with Thumbnails