Friday, September 2, 2011

Listview

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

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>Untitled Pagetitle>

head>

<body>

<form id="form1" runat="server">

<div>

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"

style="height: 26px" />

<br />

<br />

<asp:TextBox ID="TextBox1" runat="server">asp:TextBox>

<br />

<br />

<asp:TextBox ID="TextBox2" runat="server">asp:TextBox>

<br />

<br />

<asp:TextBox ID="TextBox3" runat="server">asp:TextBox>

<br />

<br />

<br />

<br />

<asp:Label ID="Label1" runat="server" Text="Label">asp:Label>

<br />

<asp:Label ID="Label2" runat="server" Text="Label">asp:Label>

div>

<div>

<asp:ListView ID="ListView1" runat="server"

onsorting="ListView1_Sorting" InsertItemPosition="LastItem"

onitemcommand="ListView1_ItemCommand"

oniteminserting="ListView1_ItemInserting"

onitemediting="ListView1_ItemEditing" >

<LayoutTemplate>

<table border="0" cellpadding="1">

<tr style="background-color:#E5E5FE">

<th align="left"><asp:LinkButton ID="lnkId" runat="server" CommandName="Sort" CommandArgument="ID">Idasp:LinkButton>th>

<th align="left"><asp:LinkButton ID="lnkName" runat="server" CommandName="Sort" CommandArgument="FirstName">Nameasp:LinkButton>th>

<th align="left"><asp:LinkButton ID="lnkType" runat="server" CommandName="Sort" CommandArgument="ContactType">Typeasp:LinkButton>th>

<th>th>

tr>

<tr id="itemPlaceholder" runat="server">tr>

table>

<asp:DataPager ID="ItemDataPager" runat="server" PageSize="25">

<Fields>

<asp:NumericPagerField ButtonCount="2" />

Fields>

asp:DataPager>

LayoutTemplate>

<ItemTemplate>

<tr>

<td><asp:Label runat="server" ID="lblId"><%#Eval("s_no")%>asp:Label>td>

<td><asp:Label runat="server" ID="lblName"><%#Eval("s_name")%>asp:Label>td>

<td><asp:Label runat="server" ID="lblType"><%#Eval("s_class")%>asp:Label>td>

<td><asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Editasp:LinkButton>td>

tr>

ItemTemplate>

<AlternatingItemTemplate>

<tr style="background-color:#EFEFEF">

<td><asp:Label runat="server" ID="lblId"><%#Eval("s_no")%>asp:Label>td>

<td><asp:Label runat="server" ID="lblName"><%#Eval("s_name")%>asp:Label>td>

<td><asp:Label runat="server" ID="lblType"><%#Eval("s_class")%>asp:Label>td>

<td><asp:LinkButton ID="lnkEdit" runat="server" CommandName="Edit">Editasp:LinkButton>td>

tr>

AlternatingItemTemplate>

<EditItemTemplate>

<td>

<asp:TextBox ID="txtUpId" runat="server" Text='<%#Eval("s_no") %>' Enabled="false" Width="20px">asp:TextBox>

td>

<td>

<asp:TextBox ID="txtUpFname" runat="server" Text='<%#Eval("s_name") %>' Width="100px">asp:TextBox>

<asp:TextBox ID="txtUpLname" runat="server" Text='<%#Eval("s_class") %>' Width="100px">asp:TextBox>

td>

<%--

--%>

<td>

<asp:LinkButton ID="lnkUpdate" runat="server" CommandName="Update">Updateasp:LinkButton>

<asp:LinkButton ID="lnkDelete" runat="server" CommandName="Delete">Deleteasp:LinkButton>

<asp:LinkButton ID="lnkCancel" runat="server" CommandName="Cancel">Cancelasp:LinkButton>

td>

tr>

EditItemTemplate>

<InsertItemTemplate>

<tr id="Tr1" runat="server">

<td>td>

<td>

<asp:TextBox ID="txtFname" runat="server" Text='<%#Eval("s_name") %>' Width="100px">First Nameasp:TextBox>

<asp:TextBox ID="txtLname" runat="server" Text='<%#Eval("s_class") %>' Width="100px">Last Nameasp:TextBox>

td>

<%--Contact Type

--%>

<td><asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />td>

tr>

InsertItemTemplate>

asp:ListView>

<br />

div>

form>

body>

html>



In cs file code


//……………………in page load……………………………………

public void data()

{

string s = "select * from tbl_student";

SqlDataAdapter da = new SqlDataAdapter(s, con);

DataSet ds = new DataSet();

da.Fill(ds);

ListView1.DataSource = ds;

ListView1.DataBind();

}

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 ins = "Insert into [tbl_student] ([s_name],[s_class]) Values('" + txtFname.Text + "', '" + txtLname.Text + "');";

// SqlDataSource1.InsertCommand = insertCommand;

SqlCommand cmd = new SqlCommand(ins,con);

cmd.Connection = con;

con.Open();

cmd.ExecuteNonQuery();

}

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 [tbl_student] set [s_name]='" + txtFname.Text + "', [s_class]='" + txtLname.Text + "', [section]='" + txtCtype.Text + "' where Id=" + Convert.ToInt32(txtId.Text) + ";";

SqlCommand cmd = new SqlCommand(updateCommand, con);

cmd.Connection = con;

con.Open();

cmd.ExecuteNonQuery();

}

else if (e.CommandName == "Delete")

{

TextBox txtId = (TextBox)e.Item.FindControl("txtUpId");

string deleteCommand = "delete from [tbl_student] where Id=" + Convert.ToInt32(txtId.Text);

SqlCommand cmd = new SqlCommand(deleteCommand, con);

cmd.Connection = con;

con.Open();

cmd.ExecuteNonQuery();

}

else if (e.CommandName == "Edit")

{

string s = "select * from tbl_student";

SqlDataAdapter da = new SqlDataAdapter(s, con);

DataSet ds = new DataSet();

da.Fill(ds);

TextBox1.Text = ds.Tables[0].Rows[0]["s_no"].ToString();

TextBox2.Text = ds.Tables[0].Rows[0]["s_name"].ToString();

TextBox3.Text = ds.Tables[0].Rows[0]["s_class"].ToString();

}

}

protected void ListView1_Sorting(object sender, ListViewSortEventArgs e)

{

}

protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)

{

}

protected void ListView1_ItemEditing(object sender, ListViewEditEventArgs e)

{

}

No comments:

Post a Comment