Posts: 21
Threads: 8
Joined: Sep 2023
Reputation:
0
Code: procedure TIWForm1.btnTestAsyncClick(Sender: TObject; EventParams: TStringList);
begin
var B := TIWButton.Create(Self);
B.Parent := Self;
end;
this can be used to create control at runtime, however, when refresh page, the html element of button is lost and not redrawn.
This issue only occur when using template processor. if possible I did like to create the control at runtime, and not add extra thing to the template.html.
Posts: 173
Threads: 18
Joined: Jun 2018
Reputation:
23
Location: US
I do something similar and found that the template must still contain a tag for the control and the control also be properly named:
Template snippet:
Code: <div class="row"><div class="col-sm-12 text-center p-1">{%btnAdmin%}</div></div>
Creation:
Code: TIWButton *b = new TIWButton(this);
b->Parent = this;
b->Name = "btnAdmin";
b->Caption = "Admin";
b->Css = "btn btn-secondary form-control";
b->HotKey = 'A';
b->StyleRenderOptions->RenderFlags = TRenderFlags() << rfRenderVisibility;
Posts: 21
Threads: 8
Joined: Sep 2023
Reputation:
0
(01-23-2024, 04:16 PM)MJS@mjs.us Wrote: I do something similar and found that the template must still contain a tag for the control and the control also be properly named:
Template snippet:
Code: <div class="row"><div class="col-sm-12 text-center p-1">{%btnAdmin%}</div></div>
Creation:
Code: TIWButton *b = new TIWButton(this);
b->Parent = this;
b->Name = "btnAdmin";
b->Caption = "Admin";
b->Css = "btn btn-secondary form-control";
b->HotKey = 'A';
b->StyleRenderOptions->RenderFlags = TRenderFlags() << rfRenderVisibility;
I see, thanks, I was hoping if its possible to not include in the template, but it seems that its impossible.
Posts: 2,301
Threads: 204
Joined: Mar 2018
Reputation:
87
Location: Auckland, New Zealand
01-24-2024, 05:57 AM
(This post was last modified: 01-24-2024, 05:58 AM by Alexandre Machado.)
Hi Ren,
when using a template you can still create controls at runtime but you need to specify the parent Id of the HTML element that will be the parent of the control. In this case you must use the event OnFindParentId of the TIWTemplateProcessorHTML component.
Have a look at this simple code that reflects your example above:
Code: procedure TIWForm1.IWTemplateProcessorHTML1FindParentId(Sender: TIWTemplateProcessorHTML; AContainerContext: TIWContainerContext;
const AComponent: TComponent; out AParentId: string);
begin
if (AComponent is TIWButton) and SameText(AComponent.Name, 'IWButton1') then
begin
AParentId := 'SOME_DIV_ID';
end;
end;
Here I'm telling IW to put the newly created TIWButton parented to a DIV (an HTML element) named 'SOME_DIV_ID'
Let's say you want to the button parented to an IWRegion, named IWRegion1, then you will have:
Code: procedure TIWForm1.IWTemplateProcessorHTML1FindParentId(Sender: TIWTemplateProcessorHTML; AContainerContext: TIWContainerContext;
const AComponent: TComponent; out AParentId: string);
begin
if (AComponent is TIWButton) and SameText(AComponent.Name, 'IWButton1') then
begin
AParentId := 'IWREGION1';
end;
end;
Can you please give it a try and let me know if it worked?
Posts: 21
Threads: 8
Joined: Sep 2023
Reputation:
0
(01-24-2024, 05:57 AM)Alexandre Machado Wrote: Hi Ren,
when using a template you can still create controls at runtime but you need to specify the parent Id of the HTML element that will be the parent of the control. In this case you must use the event OnFindParentId of the TIWTemplateProcessorHTML component.
Have a look at this simple code that reflects your example above:
Code: procedure TIWForm1.IWTemplateProcessorHTML1FindParentId(Sender: TIWTemplateProcessorHTML; AContainerContext: TIWContainerContext;
const AComponent: TComponent; out AParentId: string);
begin
if (AComponent is TIWButton) and SameText(AComponent.Name, 'IWButton1') then
begin
AParentId := 'SOME_DIV_ID';
end;
end;
Here I'm telling IW to put the newly created TIWButton parented to a DIV (an HTML element) named 'SOME_DIV_ID'
Let's say you want to the button parented to an IWRegion, named IWRegion1, then you will have:
Code: procedure TIWForm1.IWTemplateProcessorHTML1FindParentId(Sender: TIWTemplateProcessorHTML; AContainerContext: TIWContainerContext;
const AComponent: TComponent; out AParentId: string);
begin
if (AComponent is TIWButton) and SameText(AComponent.Name, 'IWButton1') then
begin
AParentId := 'IWREGION1';
end;
end;
Can you please give it a try and let me know if it worked?
I tried it can add to even a <div> that only exist on html templates,
but my main concern is, on page refresh its gone, although the component still exist.
TestFindParentId.zip (Size: 1.6 KB / Downloads: 6)
Posts: 2,301
Threads: 204
Joined: Mar 2018
Reputation:
87
Location: Auckland, New Zealand
I'll have a look and let you know
Posts: 227
Threads: 4
Joined: Mar 2018
Reputation:
22
You can also specify a container in the template, then fill the container with what you want with runtime code (change the container contents).
Dan
Posts: 2,301
Threads: 204
Joined: Mar 2018
Reputation:
87
Location: Auckland, New Zealand
01-26-2024, 06:08 AM
(This post was last modified: 01-26-2024, 06:59 PM by Alexandre Machado.)
The thing is that whenever you refresh the form, it will be rendered considering whatever you have in your template. If the control is not in the template, it won't be rendered.
So, if you expect a control to be created art runtime, parented to that region, declare the control in the template. When the control doesn't exist, IntraWeb template engine will just ignore it. However, after being created it renders correctly until you free it again.
Please check the modified project
Project1.zip (Size: 52.82 KB / Downloads: 7)
PS: There are othe ways to deal with this, including modifiying the template at runtime on the fly. However this is the simplest and most effective way.
Posts: 21
Threads: 8
Joined: Sep 2023
Reputation:
0
(01-26-2024, 06:08 AM)Alexandre Machado Wrote: The thing is that whenever you refresh the form, it will be rendered considering whatever you have in your template. If the control is not in the template, it won't be rendered.
So, if you expect a control to be created in runtime, parented to that region, declare the control in the template. When the control doesn't exist, IntraWeb template engine will just ignore it. However, after being created it renders correctly until you free it again.
Please check the modified project
PS: There are othe ways to deal with this, including modifiying the template at runtime on the fly. However this is the simplest and most effective way.
Alright, thanks for your reply~
|