![]() |
|
How to create control at runtime if using template? - Printable Version +- Atozed Forums (https://www.atozed.com/forums) +-- Forum: Atozed Software (https://www.atozed.com/forums/forum-1.html) +--- Forum: IntraWeb (https://www.atozed.com/forums/forum-3.html) +---- Forum: English (https://www.atozed.com/forums/forum-16.html) +----- Forum: IntraWeb General Discussion (https://www.atozed.com/forums/forum-4.html) +----- Thread: How to create control at runtime if using template? (/thread-3816.html) |
How to create control at runtime if using template? - RenSword - 01-23-2024 Code: procedure TIWForm1.btnTestAsyncClick(Sender: TObject; EventParams: TStringList);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. RE: How to create control at runtime if using template? - MJS@mjs.us - 01-23-2024 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);RE: How to create control at runtime if using template? - RenSword - 01-24-2024 (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: I see, thanks, I was hoping if its possible to not include in the template, but it seems that its impossible. RE: How to create control at runtime if using template? - Alexandre Machado - 01-24-2024 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;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;Can you please give it a try and let me know if it worked? RE: How to create control at runtime if using template? - RenSword - 01-24-2024 (01-24-2024, 05:57 AM)Alexandre Machado Wrote: Hi Ren, 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)
RE: How to create control at runtime if using template? - Alexandre Machado - 01-24-2024 I'll have a look and let you know RE: How to create control at runtime if using template? - DanBarclay - 01-25-2024 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 RE: How to create control at runtime if using template? - Alexandre Machado - 01-26-2024 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: 9)
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. RE: How to create control at runtime if using template? - RenSword - 01-26-2024 (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. Alright, thanks for your reply~ |