Search This Blog

Sunday, January 17, 2010

Working With DataList Control Templates:



C# code:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;

public partial class _Default : System.Web.UI.Page
{
static DataSet ds;
static SqlDataAdapter da;
static SqlCommandBuilder cmdBuilder;
static string currentCno;

protected void Page_Load(object sender, EventArgs e)
{
if (! Page.IsPostBack)
{
Bind();
}

}
public void Bind()
{
SqlConnection con = new SqlConnection("Initial Catalog=sangram;Integrated Security=SSPI;Data Source=(local);");
con.Open();
da = new SqlDataAdapter("select * from courses", con);
cmdBuilder = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "courses");
DataList1.DataSource = ds.Tables[0];
DataList1.DataBind();
}

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{


if (e.CommandName == "Select")
{
DataList1.SelectedIndex = e.Item.ItemIndex;
currentCno = ((Label)e.Item.FindControl("LblCourseId")).Text;
Bind();
}
if (e.CommandName == "Insert")
{
string cno = ((TextBox)e.Item.FindControl("TxtCourseId")).Text;
string cname = ((TextBox)e.Item.FindControl("TxtCourseName")).Text;
string duration = ((TextBox)e.Item.FindControl("TxtCourseDuration")).Text;
string fees = ((TextBox)e.Item.FindControl("TxtCourseFees")).Text;

DataColumn[] Dcol = new DataColumn[1];
Dcol[0] = ds.Tables[0].Columns[0];
ds.Tables[0].PrimaryKey = Dcol;

DataRow drow;
drow = ds.Tables[0].NewRow();

drow[0] = cno;
drow[1] = cname;
drow[2] = duration;
drow[3] = fees;

ds.Tables[0].Rows.Add(drow);

da.Update(ds, "Courses");

Bind();

}


}
protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = -1;
DataList1.SelectedIndex = -1;
Bind();
}
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
// Retrieve the updated values from the selected item.
string cno =((TextBox) e.Item.FindControl("TxtCourseId")).Text;
string cname =((TextBox) e.Item.FindControl("TxtCourseName")).Text;
string duration =((TextBox) e.Item.FindControl("TxtCourseDuration")).Text;
string fees =((TextBox) e.Item.FindControl("TxtCourseFees")).Text;


//Adding Temp prim key
DataColumn[] Dcol=new DataColumn[1];
Dcol[0]=ds.Tables[0].Columns[0];
ds.Tables[0].PrimaryKey = Dcol;

DataRow drow ;
if (ds.Tables[0].Rows.Find(cno)!= null)
{
//modifing exiting row without changing cno
drow = ds.Tables[0].Rows.Find(cno);

drow[0] =cno;
drow[1] = cname;
drow[2] = duration;
drow[3] = fees;

}
else
{
//deleting old row
ds.Tables[0].Rows.Find(currentCno).Delete();
//adding new row
drow = ds.Tables[0].NewRow();
drow[0] =cno;
drow[1] = cname;
drow[2] = duration;
drow[3] = fees;

ds.Tables[0].Rows.Add(drow);
}

//updating deleted row and newly added row simultenously
da.Update(ds, "Courses");


DataList1.EditItemIndex = -1;
DataList1.SelectedIndex = -1;

Bind();

}
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{

}
protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
{
DataList1.EditItemIndex = e.Item.ItemIndex;
Bind();
}
protected void DataList1_DeleteCommand(object source, DataListCommandEventArgs e)
{
//Adding Temp prim key
DataColumn[] Dcol = new DataColumn[1];
Dcol[0] = ds.Tables[0].Columns[0];
ds.Tables[0].PrimaryKey = Dcol;

string cno = ((TextBox)e.Item.FindControl("TxtCourseId")).Text;

ds.Tables[0].Rows.Find(cno).Delete();
da.Update(ds, "Courses");
Bind();
}
public int GetMaxCno()
{

DataRow[] drow= ds.Tables[0].Select("cno=max(cno)");
string maxRow = drow[0][0].ToString();
return Convert.ToInt16( maxRow);
}

}

HTML:

<%@ 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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" OnCancelCommand="DataList1_CancelCommand" OnUpdateCommand="DataList1_UpdateCommand" OnEditCommand="DataList1_EditCommand" OnSelectedIndexChanged="DataList1_SelectedIndexChanged" OnDeleteCommand="DataList1_DeleteCommand" >
<HeaderTemplate>
<table style="border-style:solid;">
<tr>
<td >Course Id</td>
<td>Course Name</td>
<td>Course Duration</td>
<td>Course Fees</td>
<td>Command</td>
</tr>

</HeaderTemplate>
<FooterTemplate>
<tr>
<td><asp:TextBox ID="TxtCourseId" Enabled="false" Text='<%#GetMaxCno()+1%>' runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="TxtCourseName" Text="CourseName" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="TxtCourseDuration" Text="CourseDuration" runat="server"></asp:TextBox></td>
<td><asp:TextBox ID="TxtCourseFees" Text="CourseFees" runat="server"></asp:TextBox></td>
<td><asp:Button ID="Btn3" Text="Insert" CommandName="Insert" runat="server" /></td>
</tr>
</table>
</FooterTemplate>

<ItemTemplate>
<tr>
<td>
<asp:Label ID="LblCourseId" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno")%> ></asp:Label>
</td>
<td>
<asp:Label ID="LblCourseName" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname")%> ></asp:Label>
</td>
<td>
<asp:Label ID="LblCourseDuration" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> ></asp:Label>
</td>
<td>
<asp:Label ID="LblCourseFees" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> ></asp:Label>
</td>
<td>
<asp:Button ID="Button1" runat="server" CommandName="Select" Text="Select"></asp:Button >
</td>
</tr>

</ItemTemplate>
<AlternatingItemTemplate>
<tr style="background-color:Burlywood;">
<td>
<asp:Label ID="LblCourseId" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno")%> ></asp:Label>
</td>
<td>
<asp:Label ID="LblCourseName" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname")%> ></asp:Label>
</td>
<td>
<asp:Label ID="LblCourseDuration" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> ></asp:Label>
</td>
<td>
<asp:Label ID="LblCourseFees" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> ></asp:Label>
</td>
<td>
<asp:Button ID="Btn1" runat="server" CommandName="Select" Text="Select"></asp:Button>
</td>
</tr>
</AlternatingItemTemplate>
<SelectedItemTemplate>
<tr>
<td>
<asp:TextBox ID="TxtCourseId" Enabled="false" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno")%>/>
</td>
<td>
<asp:TextBox ID="TxtCourseName" Enabled="false" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname")%>/>
</td>
<td>
<asp:TextBox ID="TxtCourseDuration" Enabled="false" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> />
</td>
<td>
<asp:TextBox ID="TxtCourseFees" runat="server" Enabled="false" Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> />
</td>
<td>
<asp:Button ID="Btn1" runat="server" CommandName="Edit" Text="Edit"></asp:Button >
</td>
<td>
<asp:Button ID="Btn2" runat="server" CommandName="Delete" Text="Delete"></asp:Button >
</td>
</tr>
</SelectedItemTemplate>
<EditItemTemplate>
<tr>
<td>
<asp:TextBox ID="TxtCourseId" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cno")%>/>
</td>
<td>
<asp:TextBox ID="TxtCourseName" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"cname")%>/>
</td>
<td>
<asp:TextBox ID="TxtCourseDuration" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"duration")%> />
</td>
<td>
<asp:TextBox ID="TxtCourseFees" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"fees")%> />
</td>
<td>
<asp:Button ID="Btn1" runat="server" CommandName="Update" Text="Update"></asp:Button >
</td>
<td>
<asp:Button ID="Btn2" runat="server" CommandName="Cancel" Text="Cancel"></asp:Button >
</td>
</tr>
</EditItemTemplate>

</asp:DataList></div>
</form>
</body>
</html>

No comments:

Post a Comment