using
System;
using
System.Collections;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Web;
using
System.Web.SessionState;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.HtmlControls;
namespace
Web_VCnet
{
///
<summary>
///
Summary description for Dataweb_booknote.
///
</summary>
public
class
Dataweb_booknote : System.Web.UI.Page
{
protected
System.Data.OleDb.OleDbCommand oleDbSelectCommand1;
protected
System.Data.OleDb.OleDbCommand oleDbInsertCommand1;
protected
System.Data.OleDb.OleDbCommand oleDbUpdateCommand1;
protected
System.Data.OleDb.OleDbConnection oleDbConnection1;
protected
System.Data.OleDb.OleDbDataAdapter oleDbDataAdapter1;
protected
Web_VCnet.Dataweb_booknotedset objDataweb_booknotedset;
protected
System.Web.UI.WebControls.DataGrid masterDataGrid;
protected
System.Web.UI.WebControls.Label Label_add1;
protected
System.Web.UI.WebControls.Label Label_add2;
protected
System.Web.UI.WebControls.Button btn_add;
protected
System.Web.UI.WebControls.Label Label_addinfo;
protected
System.Web.UI.WebControls.Label lbnavigation;
protected
System.Web.UI.WebControls.Button LastPage;
protected
System.Web.UI.WebControls.Button NextPage;
protected
System.Web.UI.WebControls.Button PreviousPage;
protected
System.Web.UI.WebControls.Button FirstPage;
protected
System.Web.UI.WebControls.Label Label_nav2;
protected
System.Web.UI.WebControls.Label Label_nav1;
protected
System.Web.UI.WebControls.HyperLink HyperLink2;
protected
System.Web.UI.WebControls.Label Label1;
protected
System.Web.UI.WebControls.Panel Panel1;
protected
System.Web.UI.WebControls.TextBox txtoperation;
protected
System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private
void
Page_Load(object
sender, System.EventArgs e)
{
// Put user code
to initialize the page here
// controls
visible ...
sub_showcontrol(true );
if (Session["mydatset"]
== null)
{
try
{
this.LoadDataSet();
this.masterDataGrid.SelectedIndex
= -1;
this.masterDataGrid.DataBind();
}
catch
(System.Exception eLoad)
{
this.Response.Write(eLoad.Message);
}
Session["mydataset"] =
this.objDataweb_booknotedset;
// Enable paging.
masterDataGrid.AllowPaging = true ;
masterDataGrid.PageSize
= 5;
if
(!Page.IsPostBack)
{
FirstPage_Click(sender,
e);
this.Label_addinfo.Text
= "";
this.txtoperation.Text
= "";
}
}
else
{
this.objDataweb_booknotedset
= ((Web_VCnet.Dataweb_booknotedset)(Session["mydatset"]));
}
}
Wwb Form
Designer generated
code |
public
void
LoadDataSet()
{
// Create a new
dataset to hold the records returned from the call to
FillDataSet.
// A temporary dataset
is used because filling the existing dataset would
// require the
databindings to be rebound.
Web_VCnet.Dataweb_booknotedset objDataSetTemp;
objDataSetTemp = new
Web_VCnet.Dataweb_booknotedset();
try
{
// Attempt to fill the
temporary dataset.
this.FillDataSet(objDataSetTemp);
}
catch (System.Exception
eFillDataSet)
{
// Add your error
handling code here.
throw
eFillDataSet;
}
try
{
// Empty the old records
from the dataset.
objDataweb_booknotedset.Clear();
// Merge the records
into the main dataset.
objDataweb_booknotedset.Merge(objDataSetTemp);
}
catch (System.Exception
eLoadMerge)
{
// Add your error
handling code here.
throw eLoadMerge;
}
}
public
void
FillDataSet(Web_VCnet.Dataweb_booknotedset dataSet)
{
// Turn off constraint
checking before the dataset is filled.
// This allows the
adapters to fill the dataset without concern
// for dependencies
between the tables.
dataSet.EnforceConstraints =
false;
try
{
// Open the connection.
this.oleDbConnection1.Open();
// Attempt to fill the
dataset through the OleDbDataAdapter1.
this.oleDbDataAdapter1.Fill(dataSet);
}
catch (System.Exception
fillException)
{
// Add your error
handling code here.
throw
fillException;
}
finally
{
// Turn constraint
checking back on.
dataSet.EnforceConstraints =
true;
// Close the connection
whether or not the exception was thrown.
this.oleDbConnection1.Close();
}
}
//controls visible ...
private void
sub_showcontrol( bool bval)
{
this.Label_nav1.Visible
= bval;
this.Label_nav2.Visible
= bval;
this.FirstPage.Visible
= bval;
this.PreviousPage.Visible
= bval;
this.NextPage.Visible
= bval;
this.LastPage.Visible
= bval;
this.lbnavigation.Visible
= bval;
this.Label_add1.Visible
= bval;
this.Label_add2.Visible
= bval;
this.btn_add.Visible
= bval;
this.Label_addinfo.Visible
= bval;
}
//***************************** Navigation ...
private void
FirstPage_Click(object sender, System.EventArgs e)
{
masterDataGrid.CurrentPageIndex = 0;
//Enabled buttons and
bind the data
show_Button(masterDataGrid.CurrentPageIndex);
}
//Enabled buttons and
bind the data
private void show_Button(
int vCurrentPage)
{
int vcpage =
vCurrentPage + 1;
int vPagecount;
if (vCurrentPage == 0)
{
FirstPage.Enabled =
false;
PreviousPage.Enabled =
false;
NextPage.Enabled = true;
LastPage.Enabled = true;
goto pt1;
}
else
{
if (vCurrentPage == (masterDataGrid.PageCount
- 1))
{
FirstPage.Enabled =
true;
PreviousPage.Enabled =
true;
NextPage.Enabled =
false;
LastPage.Enabled =
false;
goto pt1;
}
else
{
FirstPage.Enabled =
true;
PreviousPage.Enabled =
true;
NextPage.Enabled = true;
LastPage.Enabled = true;
}
}
pt1:
masterDataGrid.DataBind();
vPagecount =
masterDataGrid.PageCount;
this.lbnavigation.Text
= "Page : " + System.Convert.ToString(vcpage) + " of " +
System.Convert.ToString(vPagecount) + " ............ info:
Page size = 5";
}
private void
PreviousPage_Click(object
sender, System.EventArgs e)
{
masterDataGrid.CurrentPageIndex = Math.Max(0,
masterDataGrid.CurrentPageIndex - 1);
//Enabled buttons
and bind the data
show_Button(masterDataGrid.CurrentPageIndex);
}
private void
NextPage_Click(object sender, System.EventArgs e)
{
masterDataGrid.CurrentPageIndex =
Math.Min(masterDataGrid.PageCount - 1,
masterDataGrid.CurrentPageIndex + 1);
//Enabled buttons and
bind the data
show_Button(masterDataGrid.CurrentPageIndex);
}
private void
LastPage_Click(object sender, System.EventArgs e)
{
masterDataGrid.CurrentPageIndex = masterDataGrid.PageCount
- 1;
//Enabled buttons and
bind the data
show_Button(masterDataGrid.CurrentPageIndex);
}
//
***************************** Edit, Update, Cancel
private void
masterDataGrid_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.masterDataGrid.EditItemIndex
= e.Item.ItemIndex;
this.masterDataGrid.DataBind();
this.txtoperation.Text
= "Edit operation";
}
private void
masterDataGrid_CancelCommand(object
source, System.Web.UI.WebControls.DataGridCommandEventArgs
e)
{
this.masterDataGrid.EditItemIndex
= -1;
this.masterDataGrid.DataBind();
this.Label_addinfo.Text
= "";
this.txtoperation.Text
= "";
}
private void
masterDataGrid_UpdateCommand(object
source, System.Web.UI.WebControls.DataGridCommandEventArgs
e)
{
string
vfirstname;
string
vfathername;
string
vname;
string
vcountry;
string
vcity;
if (this.txtoperation.Text
== "Edit operation")
{
// Gets the value
of the key field of the row being updated
string key =
masterDataGrid.DataKeys[e.Item.ItemIndex].ToString();
// Gets get the value of
the controls (textboxes) that the user
// updated. The DataGrid
columns are exposed as the Cells collection.
// Each cell has a
collection of controls. In this case, there is only one
// control in each cell
-- a TextBox control. To get its value,
// you copy the TextBox
to a local instance (which requires casting)
// and extract its Text
property.
//
// The first column --
Cells(0) -- contains the Update and Cancel buttons.
System.Web.UI.WebControls.TextBox tb;
// Gets the value the
TextBox control in the 2nd column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[1].Controls[0]));
vname = tb.Text;
// Gets the value the
TextBox control in the 3th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[2].Controls[0]));
vfirstname = tb.Text;
// Gets the value the
TextBox control in the 4th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[3].Controls[0]));
vfathername = tb.Text;
// Gets the value the
TextBox control in the 5th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[4].Controls[0]));
vcountry = tb.Text;
// Gets the value the
TextBox control in the 6th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[5].Controls[0]));
vcity = tb.Text;
// Finds the row in the
dataset table that matches the
// one the user updated
in the grid. This example uses a
// special Find method
defined for the typed dataset, which
// returns a reference
to the row.
Dataweb_booknotedset.booknoteRow r;
r =
objDataweb_booknotedset.booknote.FindBybookid(int.Parse(key));
// Updates the dataset
table.
r.name = vname;
r.first_name =
vfirstname;
r.father_name =
vfathername;
r.country = vcountry;
r.city = vcity;
// Calls a OLE statement
to update the database from the dataset
this.oleDbDataAdapter1.Update(this.objDataweb_booknotedset,
"booknote");
// Takes the DataGrid
row out of editing mode
masterDataGrid.EditItemIndex = -1;
// Refreshes the grid
masterDataGrid.DataBind();
return;
}
if (this.txtoperation.Text
== "Add operation")
{
// The first
column -- Cells(0) -- contains the Update and Cancel
buttons.
System.Web.UI.WebControls.TextBox tb;
// Gets the value the
TextBox control in the 2nd column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[1].Controls[0]));
vname = tb.Text;
// Gets the value the
TextBox control in the 3th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[2].Controls[0]));
vfirstname = tb.Text;
// Gets the value the
TextBox control in the 4th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[3].Controls[0]));
vfathername = tb.Text;
// Gets the value the
TextBox control in the 5th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[4].Controls[0]));
vcountry = tb.Text;
// Gets the value the
TextBox control in the 6th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[5].Controls[0]));
vcity = tb.Text;
// Finds the row in the
dataset table that matches the
// one the user updated
in the grid. This example uses a
// special Find method
defined for the typed dataset, which
// returns a reference
to the row.
Dataweb_booknotedset.booknoteRow r;
r =
objDataweb_booknotedset.booknote.AddbooknoteRow(vcity,
vcountry, vfathername, vfirstname, vname);
// Updates the dataset
table.
// Calls a OLE statement
to update the database from the dataset
this.oleDbDataAdapter1.Update(this.objDataweb_booknotedset,
"booknote");
r.Table.AcceptChanges();
//Go to last page of
DataGrid, or last row of DataGrid
masterDataGrid.CurrentPageIndex = masterDataGrid.PageCount
- 1;
//Eenabled buttons and
bind the data
show_Button(masterDataGrid.CurrentPageIndex);
// Takes the DataGrid
row out of editing mode
masterDataGrid.EditItemIndex = -1;
// Refreshes the grid
masterDataGrid.DataBind();
this.Label_addinfo.Text
= "";
this.txtoperation.Text
= "";
return;
}
}
private void
btn_add_Click(object
sender, System.EventArgs e)
{
//show first page
...
this.FirstPage_Click(sender,
e);
this.txtoperation.Text
= "Add operation";
//Add new record
DataRow drow = this.objDataweb_booknotedset.Tables["booknote"].NewRow();
drow["name"] = "";
drow["first_name"] = "";
drow["father_name"] =
"";
drow["country"] = "";
drow["city"] = "";
this.Label_addinfo.Text
= "New row to be add ...";
this.objDataweb_booknotedset.booknote.Rows.InsertAt(drow,
0);
masterDataGrid.EditItemIndex = 0;
masterDataGrid.DataBind();
}
}
} |