using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASP_DEMO.CODE;
using System.Data;
namespace ASP_DEMO
{
public partial class OfflineDBA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DBAccess objdb = new DBAccess();
protected void btnLoadData_Click(object sender, EventArgs e)
{
DataLoad();
}
void DataLoad() {
DataSet ds = null;
if (Cache["data"] == null)
{
string sql = "SELECT ID, PID, QUESTION, ANSWER FROM TB_QUESTIONS";
ds = objdb.Select(sql);
// ASSING THE PRIMARY KEY
ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns["ID"] };
Cache["data"] = ds;
lblMessage.Text = "get data from db";
}
else
{
ds = (DataSet)Cache["data"];
lblMessage.Text = "get data from cache";
}
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataLoad();
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataLoad();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (Cache["data"] != null)
{
DataSet ds = (DataSet)Cache["data"];
DataRow dr = ds.Tables[0].Rows.Find(e.Keys["ID"]);
dr["PID"] = e.NewValues["PID"];
dr["QUESTION"] = e.NewValues["QUESTION"];
dr["ANSWER"] = e.NewValues["ANSWER"];
Cache["data"] = ds;
DataLoad();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (Cache["data"] != null)
{
DataSet ds = (DataSet)Cache["data"];
DataRow dr = ds.Tables[0].Rows.Find(e.Keys["ID"]);
ds.Tables[0].Rows.Remove(dr);
Cache["data"] = ds;
DataLoad();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Cache.Remove("data");
}
}
}
///////////////////////////////////
<table style="width: 100%;">
<tr>
<td> </td>
<td>
<asp:Button ID="btnLoadData" runat="server" OnClick="btnLoadData_Click" Text="Load Data" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="PID" HeaderText="PID" SortExpression="PID" />
<asp:BoundField DataField="QUESTION" HeaderText="QUESTION" SortExpression="QUESTION" />
<asp:BoundField DataField="ANSWER" HeaderText="ANSWER" SortExpression="ANSWER" />
</Columns>
</asp:GridView>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
</td>
<td> </td>
</tr>
</table>
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASP_DEMO.CODE;
using System.Data;
namespace ASP_DEMO
{
public partial class OfflineDBA : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
DBAccess objdb = new DBAccess();
protected void btnLoadData_Click(object sender, EventArgs e)
{
DataLoad();
}
void DataLoad() {
DataSet ds = null;
if (Cache["data"] == null)
{
string sql = "SELECT ID, PID, QUESTION, ANSWER FROM TB_QUESTIONS";
ds = objdb.Select(sql);
// ASSING THE PRIMARY KEY
ds.Tables[0].PrimaryKey = new DataColumn[] {ds.Tables[0].Columns["ID"] };
Cache["data"] = ds;
lblMessage.Text = "get data from db";
}
else
{
ds = (DataSet)Cache["data"];
lblMessage.Text = "get data from cache";
}
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
DataLoad();
}
protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
DataLoad();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (Cache["data"] != null)
{
DataSet ds = (DataSet)Cache["data"];
DataRow dr = ds.Tables[0].Rows.Find(e.Keys["ID"]);
dr["PID"] = e.NewValues["PID"];
dr["QUESTION"] = e.NewValues["QUESTION"];
dr["ANSWER"] = e.NewValues["ANSWER"];
Cache["data"] = ds;
DataLoad();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
if (Cache["data"] != null)
{
DataSet ds = (DataSet)Cache["data"];
DataRow dr = ds.Tables[0].Rows.Find(e.Keys["ID"]);
ds.Tables[0].Rows.Remove(dr);
Cache["data"] = ds;
DataLoad();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Cache.Remove("data");
}
}
}
///////////////////////////////////
<table style="width: 100%;">
<tr>
<td> </td>
<td>
<asp:Button ID="btnLoadData" runat="server" OnClick="btnLoadData_Click" Text="Load Data" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:Label ID="lblMessage" runat="server"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing" OnRowUpdated="GridView1_RowUpdated" OnRowUpdating="GridView1_RowUpdating" >
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
<asp:BoundField DataField="PID" HeaderText="PID" SortExpression="PID" />
<asp:BoundField DataField="QUESTION" HeaderText="QUESTION" SortExpression="QUESTION" />
<asp:BoundField DataField="ANSWER" HeaderText="ANSWER" SortExpression="ANSWER" />
</Columns>
</asp:GridView>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td>
</td>
<td> </td>
</tr>
</table>
No comments:
Post a Comment