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_personal.
///
</summary>
public
class
Dataweb_personal : 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_personaldset objDataweb_personaldset;
protected
System.Web.UI.WebControls.DataGrid masterDataGrid;
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.HyperLink HyperLink1;
protected
System.Web.UI.WebControls.Label Label2;
protected
System.Web.UI.WebControls.Panel Panel1;
protected
System.Data.OleDb.OleDbCommand oleDbDeleteCommand1;
private
void
Page_Load(object
sender, System.EventArgs e)
{
// Put user code
to initialize the page here
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_personaldset;
// Enable paging.
masterDataGrid.AllowPaging = true ;
masterDataGrid.PageSize = 5;
if
(!Page.IsPostBack)
{
FirstPage_Click(sender, e);
}
}
else
{
this.objDataweb_personaldset
= ((Web_VCnet.Dataweb_personaldset)(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_personaldset objDataSetTemp;
objDataSetTemp =
new Web_VCnet.Dataweb_personaldset();
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_personaldset.Clear();
// Merge the
records into the main dataset.
objDataweb_personaldset.Merge(objDataSetTemp);
}
catch
(System.Exception eLoadMerge)
{
// Add your error
handling code here.
throw
eLoadMerge;
}
}
public
void
FillDataSet(Web_VCnet.Dataweb_personaldset 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();
}
}
//***************************** 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();
}
private
void
masterDataGrid_CancelCommand(object
source, System.Web.UI.WebControls.DataGridCommandEventArgs
e)
{
this.masterDataGrid.EditItemIndex
= -1;
this.masterDataGrid.DataBind();
}
private
void
masterDataGrid_UpdateCommand(object
source, System.Web.UI.WebControls.DataGridCommandEventArgs
e)
{
string
vfirstname;
string
vname;
string
vsex;
string
vcountry;
// 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]));
vfirstname = tb.Text;
// Gets the value the
TextBox control in the 3th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[2].Controls[0]));
vname = tb.Text;
// Gets the value the
TextBox control in the 4th column
tb = ((System.Web.UI.WebControls.TextBox)(e.Item.Cells[3].Controls[0]));
vsex = 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;
// 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_personaldset.personalRow
r;
r =
objDataweb_personaldset.personal.FindBypersonal_id(int.Parse(key));
// Updates the dataset
table.
r.first_name = vfirstname;;
r.name = vname;
r.sex = vsex;
r.country = vcountry;
// Calls a OLE statement
to update the database from the dataset
this.oleDbDataAdapter1.Update(this.objDataweb_personaldset,
"personal");
// Takes the DataGrid
row out of editing mode
masterDataGrid.EditItemIndex =
-1;
// Refreshes the grid
masterDataGrid.DataBind();
}
}
} |