asp.net 剛開始時, 也是拖拉控件, 但后來有了 MVC、xNext.
換個思路使用 IntraWeb 吧:
界面全部用 html+js+css 實現(有些會是用 Delphi 動態生成), 然后用 js 通過 Ajax 調用 Delphi 的方法.
測試程序要使用的模板 IWForm1.html:
1、在程序所在目錄建立 Templates 文件夾, 把 IWForm1.html 放其中.
2、在程序所在目錄建立 wwwroot 文件夾, 把模板中用到的 IWForm1.js 和 IWForm1.css 放其中.
3、其中的 AjaxFun1() 方法是在 IWForm1.js 中實現的; 這里兩次調用參數分別是 1、2
4、{%IWLabel1%} 中的 IWLabel1 是在 Delphi 中添加的控件, 它用來返回數據結果.
IWForm1.js:
1、executeAjaxEvent 或 processAjaxEven 方法是 IntraWeb 內置的 IWLib.js 提供的; 其第三個參數決定了它將調用 Delphi 中的 IWCallBack1 方法.
2、Delphi 收到的參數將是一個 TStringList, 上面的 n 是將要傳遞的參數, x 是隨意命名的參數標示.
IWForm1.css:
這是隨意定義的; IWLABEL1 對應的是在 Delphi 添加的 IWLabel1.
//在新建 IW 主窗體上放置 IWTemplateProcessorHTML1、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.
思路很簡單, 在實踐中可能會碰到一些小問題, 譬如:
1、如果真傳到服務器, 那個 Data.txt 修改其屬性為可寫;
2、通過 executeAjaxEvent 傳遞中文參數時, 我在本機沒發現問題, 但傳到服務器不行了, 最后自己寫了一個編碼(js)、解碼(Delphi)函數; 如果你需要可以告訴我.
不過都是小問題.
測試源文件(IW_MVC_Test.rar), 傳到 "intraweb交流群 319037363" 了; 歡迎指正!