Example of how to use Intraweb

When asp.net first started, it was also a drag-and-drop control, but then it had MVC and xNext.
Use IntraWeb in another way: The interface is all implemented with html+js+css (some will be dynamically generated with Delphi), and then use js to call Delphi’s method through Ajax.
The template IWForm1.html to be used by the test program:
<!doctype html>
<html>
<head>
<meta charset=”utf-8″ />
<link rel=”stylesheet” href=”IWForm1.css” type=”text/css”/>
<script type=”text/javascript” src=”IWForm1.js”></script>
</head>
<body>
<div id=”div1″>
<a href=”#” onclick=”AjaxFun1(1);”>添加</a>
<a href=”#” onclick=”AjaxFun1(2);”>删除</a>
</div>
<div id=”div2″>{%IWLabel1%}</div>
</body>
</html>
1.Create a Templates folder in the directory where the program is located, and put IWForm1.html in it.
2.Create a wwwroot folder in the directory where the program is located, and put IWForm1.js and IWForm1.css used in the template.
3. The AjaxFun1() method is implemented in IWForm1.js; here the two call parameters are 1, 2
4. IWLabel1 in (%IWLabel1%) is a control added in Delphi, which is used to return data results.
WForm1.js:
function AjaxFun1(n) {
executeAjaxEvent(“&x=”+n, null, “IWCallBack1”, false, null, false);
}
1.The executeAjaxEvent or processAjaxEven method is provided by IntraWeb’s built-in IWLib.js; its third parameter determines that it will call the IWCallBack1 method in Delphi.
2.The parameter received by Delphi will be a TStringList, the above n is the parameter to be passed, and x is the parameter label with arbitrary names.
IWForm1.css:
div { padding: 16px; }
#div2 { background-color: green; color: #fff; }
#IWLABEL1 { font-size: large; }
This is arbitrarily defined; IWLABEL1 corresponds to IWLabel1 added in Delphi.
//Place two controls, IWTemplateProcessorHTML1 and IWLabel1, on the new IW main form. 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); //This is the method that IWForm1.js will call; the following also needs to be registered through WebApplication.RegisterCallBack end; implementation {$R *.dfm} uses IW.Common.AppInfo; //Get path required var gPath: string; procedure TIWForm1.IWAppFormCreate(Sender: TObject); begin LayoutMgr := IWTemplateProcessorHTML1; //Associated template (IWForm1.html) IWTemplateProcessorHTML1.RenderStyles := False; //Disable IW style settings WebApplication.RegisterCallBack('IWCallBack1', DoCallBack1); //Register the callback; js will call the DoCallBack1 method here by specifying the name ("IWCallBack1") gPath := TIWAppInfo.GetAppPath + 'Data.txt'; //Path to test file if not FileExists(gPath) then //Initialize the test file begin with TStringList.Create do begin Add(DateTimeToStr(Now)); SaveToFile(gPath, TEncoding.UTF8); Free; end; end; IWLabel1.RawText := True; //Specifies to render its content in Html; among several controls with RawText properties, IWLabel1 is found to be the most flexible. IWLabel1.StyleRenderOptions.RenderSize := False; //Since IWTemplateProcessorHTML1.RenderStyles := False; the following should be unnecessary, but not under 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; //Get the parameters passed by js List := TStringList.Create; List.LoadFromFile(gPath, TEncoding.UTF8); case x of 1: List.Add(DateTimeToStr(Now)); //The parameter is 1 means add 2: if List.Count > 0 then List.Delete(0); //The parameter is 2 means delete end; IWLabel1.Text := List.Text.Replace(sLineBreak, '<br/>'); //Present; List.SaveToFile(gPath, TEncoding.UTF8); List.Free; end; initialization TIWForm1.SetAsMainForm; end.
The idea is very simple, and some small problems may be encountered in practice, such as:
1. If it is transmitted to the server, the Data.txt will be modified to be writable;
2. When passing Different Languages parameters through executeAjaxEvent, I didn’t find any problems on this machine, but it couldn’t be transmitted to the server. Finally, I wrote an encoding (js) and decoding (Delphi) function; if you need it, you can tell me.
But it’s all small problems.