asp.net 剛開始時, 也是拖拉控件, 但後來有了 MVC、xNext.
換個思路使用 IntraWeb 吧:
界面所有用 html+js+css 實現(有些會是用 Delphi 動態生成), 而後用 js 經過 Ajax 調用 Delphi 的方法.
測試程序要使用的模板 IWForm1.html:
一、在程序所在目錄創建 Templates 文件夾, 把 IWForm1.html 放其中.
二、在程序所在目錄創建 wwwroot 文件夾, 把模板中用到的 IWForm1.js 和 IWForm1.css 放其中.
三、其中的 AjaxFun1() 方法是在 IWForm1.js 中實現的; 這裏兩次調用參數分別是 一、2
四、{%IWLabel1%} 中的 IWLabel1 是在 Delphi 中添加的控件, 它用來返回數據結果.
IWForm1.js:
一、executeAjaxEvent 或 processAjaxEven 方法是 IntraWeb 內置的 IWLib.js 提供的; 其第三個參數決定了它將調用 Delphi 中的 IWCallBack1 方法.
二、Delphi 收到的參數將是一個 TStringList, 上面的 n 是將要傳遞的參數, x 是隨意命名的參數標示.
IWForm1.css:
這是隨意定義的; IWLABEL1 對應的是在 Delphi 添加的 IWLabel1.
//在新建 IW 主窗體上放置 IWTemplateProcessorHTML一、IWLabel1 兩個控件.
unit Unit1;
interface
uses
Classes, SysUtils, IWAppForm, IWApplication, IWColor, IWTypes, IWVCLComponent, IWBaseLayoutComponent, IWBaseContainerLayout, IWContainerLayout,
IWTemplateProcessorHTML, IWCompLabel, Vcl.Controls, IWVCLBaseControl, IWBaseControl, IWBaseHTMLControl, IWControl, IWCompButton, IWCompEdit;
type
TIWForm1 = class(TIWAppForm)
IWLabel1: TIWLabel;
IWTemplateProcessorHTML1: TIWTemplateProcessorHTML;
procedure IWAppFormCreate(Sender: TObject);
public
procedure DoCallBack1(EventParams: TStringList); //這是 IWForm1.js 將要調用的方法; 下面還須要經過 WebApplication.RegisterCallBack 註冊一下
end;
implementation
{$R *.dfm}
uses IW.Common.AppInfo; //獲取路徑須要
var gPath: string;
procedure TIWForm1.IWAppFormCreate(Sender: TObject);
begin
LayoutMgr := IWTemplateProcessorHTML1; //關聯模板(IWForm1.html)
IWTemplateProcessorHTML1.RenderStyles := False; //禁用 IW 的樣式設置
WebApplication.RegisterCallBack('IWCallBack1', DoCallBack1); //註冊回調; js 將經過指定名稱("IWCallBack1")調用這裏的 DoCallBack1 方法
gPath := TIWAppInfo.GetAppPath + 'Data.txt'; //用於測試文件的路徑
if not FileExists(gPath) then //初始化測試文件
begin
with TStringList.Create do begin
Add(DateTimeToStr(Now));
SaveToFile(gPath, TEncoding.UTF8);
Free;
end;
end;
IWLabel1.RawText := True; //指定以 Html 的方式呈現其內容; 具備 RawText 屬性的幾個控件中, 發現 IWLabel1 最靈活.
IWLabel1.StyleRenderOptions.RenderSize := False; //既然前面已經指定了 IWTemplateProcessorHTML1.RenderStyles := False; 下面這些就應該不須要了, 但在 IE 下不行
IWLabel1.StyleRenderOptions.RenderPosition := False;
IWLabel1.StyleRenderOptions.RenderFont := False;
IWLabel1.StyleRenderOptions.RenderZIndex := False;
IWLabel1.StyleRenderOptions.RenderVisibility := False;
IWLabel1.StyleRenderOptions.RenderStatus := False;
IWLabel1.StyleRenderOptions.RenderAbsolute := False;
IWLabel1.StyleRenderOptions.RenderPadding := False;
IWLabel1.StyleRenderOptions.RenderBorder := False;
end;
procedure TIWForm1.DoCallBack1(EventParams: TStringList);
var
List: TStringList;
x: Integer;
begin
x := EventParams.Values['x'].ToInteger; //獲取 js 傳來的參數
List := TStringList.Create;
List.LoadFromFile(gPath, TEncoding.UTF8);
case x of
1: List.Add(DateTimeToStr(Now)); //參數是 1 表示添加
2: if List.Count > 0 then List.Delete(0); //參數是 2 表示刪除
end;
IWLabel1.Text := List.Text.Replace(sLineBreak, '<br/>'); //呈現;
List.SaveToFile(gPath, TEncoding.UTF8);
List.Free;
end;
initialization
TIWForm1.SetAsMainForm;
end.
思路很簡單, 在實踐中可能會碰到一些小問題, 譬如: 一、若是真傳到服務器, 那個 Data.txt 修改其屬性爲可寫; 二、經過 executeAjaxEvent 傳遞中文參數時, 我在本機沒發現問題, 但傳到服務器不行了, 最後本身寫了一個編碼(js)、解碼(Delphi)函數; 若是你須要能夠告訴我. 不過都是小問題. 測試源文件(IW_MVC_Test.rar), 傳到 "intraweb交流羣 319037363" 了; 歡迎指正!