webform1.pas.~1~

来自「Delphi 2005程序设计教程_实例源文件和教学课件」· ~1~ 代码 · 共 124 行

~1~
124
字号

unit WebForm1;

interface

uses
  System.Data.SqlClient,
  System.Collections, System.ComponentModel,
  System.Data, System.Drawing, System.Web, System.Web.SessionState,
  System.Web.UI, System.Web.UI.WebControls, System.Web.UI.HtmlControls;
type
  TWebForm1 = class(System.Web.UI.Page)
  {$REGION 'Designer Managed Code'}
  strict private
    procedure InitializeComponent;
    procedure DataGrid1_PageIndexChanged(source: System.Object; e: System.Web.UI.WebControls.DataGridPageChangedEventArgs);
    procedure DataGrid1_EditCommand(source: System.Object; e: System.Web.UI.WebControls.DataGridCommandEventArgs);
    procedure DataGrid1_CancelCommand(source: System.Object; e: System.Web.UI.WebControls.DataGridCommandEventArgs);
    procedure DataGrid1_UpdateCommand(source: System.Object; e: System.Web.UI.WebControls.DataGridCommandEventArgs);
  {$ENDREGION}
  strict private
    procedure Page_Load(sender: System.Object; e: System.EventArgs);
  strict protected
    DataGrid1: System.Web.UI.WebControls.DataGrid;
    procedure OnInit(e: EventArgs); override;
  private
    { Private Declarations }
    procedure BindSource;
  public
    { Public Declarations }
  end;

implementation

{$REGION 'Designer Managed Code'}
/// <summary>
/// Required method for Designer support -- do not modify
/// the contents of this method with the code editor.
/// </summary>
procedure TWebForm1.InitializeComponent;
begin
  Include(Self.DataGrid1.PageIndexChanged, Self.DataGrid1_PageIndexChanged);
  Include(Self.DataGrid1.CancelCommand, Self.DataGrid1_CancelCommand);
  Include(Self.DataGrid1.EditCommand, Self.DataGrid1_EditCommand);
  Include(Self.DataGrid1.UpdateCommand, Self.DataGrid1_UpdateCommand);
  Include(Self.Load, Self.Page_Load);
end;
{$ENDREGION}

procedure TWebForm1.BindSource;
var
  conn:SqlConnection;
  adp:SqlDataAdapter;
  ds:DataSet;
begin
  conn:=SqlConnection.Create('server=buaa-Hourse;uid=sa;pwd=;database=northwind');
  adp:=SqlDataAdapter.Create('select * from employees',conn);
  ds:=DataSet.Create;
  adp.Fill(ds);
  DataGrid1.DataSource:=ds;
  DataGrid1.DataKeyField:='EmployeeID';
  DataGrid1.DataBind;
end;

procedure TWebForm1.DataGrid1_UpdateCommand(source: System.Object; e: System.Web.UI.WebControls.DataGridCommandEventArgs);
var
  LastName,FirstName,BirthDate,HomePhone,EmployeeID:string;
  strSql:string;
  conn:SqlConnection;
  comm:SqlCommand;
begin
  LastName:=TextBox(e.Item.Cells[0].Controls[0]).Text;
  FirstName:=TextBox(e.Item.Cells[0].Controls[1]).Text;
  BirthDate:=TextBox(e.Item.Cells[1].Controls[0]).Text;
  HomePhone:=TextBox(e.Item.Cells[2].Controls[0]).Text;
  EmployeeID:=DataGrid1.DataKeys[e.Item.ItemIndex].ToString;
  conn:=SqlConnection.Create('server=buaa-hourse;uid=sa;pwd=;database=northwind');
  strSql:='update employees set LastName='''+LastName+''',FirstName='''+FirstName+''',BirthDate='+BirthDate+',HomePhone='''+HomePhone+''' where EmployeeID='+EmployeeID;
  comm:=SqlCommand.Create(strSql,conn);
  conn.Open;
  comm.ExecuteNonQuery;
  conn.Close;
  DataGrid1.EditItemIndex:=-1;
  BindSource;
end;

procedure TWebForm1.DataGrid1_CancelCommand(source: System.Object; e: System.Web.UI.WebControls.DataGridCommandEventArgs);
begin
  DataGrid1.EditItemIndex:=-1;
  BindSource;
end;

procedure TWebForm1.DataGrid1_EditCommand(source: System.Object; e: System.Web.UI.WebControls.DataGridCommandEventArgs);
begin
  DataGrid1.EditItemIndex:=e.Item.ItemIndex;
  BindSource;
end;

procedure TWebForm1.DataGrid1_PageIndexChanged(source: System.Object; e: System.Web.UI.WebControls.DataGridPageChangedEventArgs);
begin
  DataGrid1.CurrentPageIndex:=e.NewPageIndex;
  BindSource;
end;
procedure TWebForm1.Page_Load(sender: System.Object; e: System.EventArgs);
begin
  if not Page.IsPostBack then
  begin
    BindSource;
  end;
end;

procedure TWebForm1.OnInit(e: EventArgs);
begin
  //
  // Required for Designer support
  //
  InitializeComponent;
  inherited OnInit(e);
end;


end.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?