Problem with "Web" components (i.e. TWebLabel/TLabel)

I have many places where I query the type and then access it with a typecast.
Here is an example:

procedure TWebBasisForm.InitDefaultElementID;
var
  i: Integer;
  s: String;
  c: TComponent;
begin
  s := StringReplace( self.UnitName, '.', '_', [rfReplaceAll]);
  for i := 0 to ComponentCount-1 do begin
    c := Components[i];
         if (c is TWebLabel)       and (TWebLabel(      c).ElementID = '') then TWebLabel(      c).ElementID := s+'_'+TWebLabel(      c).Name
    else if (c is TWebEdit)        and (TWebEdit(       c).ElementID = '') then TWebEdit(       c).ElementID := s+'_'+TWebEdit(       c).Name
    else if (c is TWebSpinEdit)    and (TWebSpinEdit(   c).ElementID = '') then TWebSpinEdit(   c).ElementID := s+'_'+TWebSpinEdit(   c).Name
    else if (c is TWebCheckBox)    and (TWebCheckBox(   c).ElementID = '') then TWebCheckBox(   c).ElementID := s+'_'+TWebCheckBox(   c).Name
    else if (c is TWebRadioButton) and (TWebRadioButton(c).ElementID = '') then TWebRadioButton(c).ElementID := s+'_'+TWebRadioButton(c).Name
    else if (c is TWebComboBox)    and (TWebComboBox(   c).ElementID = '') then TWebComboBox(   c).ElementID := s+'_'+TWebComboBox(   c).Name
    else if (c is TWebMemo)        and (TWebMemo(       c).ElementID = '') then TWebMemo(       c).ElementID := s+'_'+TWebMemo(       c).Name
    else if (c is TWebListControl) and (TWebListControl(c).ElementID = '') then TWebListControl(c).ElementID := s+'_'+TWebListControl(c).Name
    else if (c is TWebButton)      and (TWebButton(     c).ElementID = '') then TWebButton(     c).ElementID := s+'_'+TWebButton(     c).Name
    else if (c is TWebHTMLDiv)     and (TWebHTMLDiv(    c).ElementID = '') then TWebHTMLDiv(    c).ElementID := s+'_'+TWebHTMLDiv(    c).Name
    else if (c is TWebHTMLSpan)    and (TWebHTMLSpan(   c).ElementID = '') then TWebHTMLSpan(   c).ElementID := s+'_'+TWebHTMLSpan(   c).Name;
  end;
end;

When I debug it later in the browser, WebLabel sometimes becomes TLabel and it no longer works.

Here is the code from browser debug:

procedure TBasisForm.InitDefaultElementID;
var
  i: Integer;
  s: String;
  c: TComponent;
begin
  s := StringReplace( self.UnitName, '.', '_', [rfReplaceAll]);
  for i := 0 to ComponentCount-1 do begin
    c := Components[i];
         if (c is TLabel)       and (TLabel(      c).ElementID = '') then TLabel(      c).ElementID := s+'_'+TWebLabel(      c).Name
    else if (c is TEdit)        and (TEdit(       c).ElementID = '') then TEdit(       c).ElementID := s+'_'+TWebEdit(       c).Name
    else if (c is TSpinEdit)    and (TSpinEdit(   c).ElementID = '') then TSpinEdit(   c).ElementID := s+'_'+TWebSpinEdit(   c).Name
    else if (c is TCheckBox)    and (TCheckBox(   c).ElementID = '') then TCheckBox(   c).ElementID := s+'_'+TWebCheckBox(   c).Name
    else if (c is TRadioButton) and (TRadioButton(c).ElementID = '') then TRadioButton(c).ElementID := s+'_'+TWebRadioButton(c).Name
    else if (c is TComboBox)    and (TComboBox(   c).ElementID = '') then TComboBox(   c).ElementID := s+'_'+TWebComboBox(   c).Name
    else if (c is TMemo)        and (TMemo(       c).ElementID = '') then TMemo(       c).ElementID := s+'_'+TWebMemo(       c).Name
    else if (c is TListControl) and (TListControl(c).ElementID = '') then TListControl(c).ElementID := s+'_'+TWebListControl(c).Name
    else if (c is TButton)      and (TButton(     c).ElementID = '') then TButton(     c).ElementID := s+'_'+TWebButton(     c).Name
    else if (c is THTMLDiv)     and (THTMLDiv(    c).ElementID = '') then THTMLDiv(    c).ElementID := s+'_'+TWebHTMLDiv(    c).Name
    else if (c is THTMLSpan)    and (THTMLSpan(   c).ElementID = '') then THTMLSpan(   c).ElementID := s+'_'+TWebHTMLSpan(   c).Name;
  end;
end;

Sometime is TWebLabel changed to TLabel and sometimes not.
And my code does not work.

I have a lot of places with type queries. I'm not sure which ones work and which ones don't.

Is there a rule here?

I don't think it's the type at all.
Is it possible that the ElementID is not empty?

TWebLabel is a direct descendent of TLabel in the WEB core framework. TWebLabel is used at design-time to ensure it has a different class name than the VCL TLabel and not conflict with it.
But you can do your class checks in runtime code with TLabel (and the same for all other classes, i.e. TButton, TListBox, TEdit, ...)

Ok, I understand.
I found out that my problem was with the ElementID.
I wanted to leave this empty so that I could see whether it was being used. If it is not being used individually, then I want to set an automatic value.
But Webcore itself sets a value that I cannot do anything with. And if I remove the automatic setting in WebCore, I get errors in the JavaScript.
Can I change the behavior?

I don't want to have to set all the element IDs myself in hundreds of forms with thousands of elements.
Our automatic system sets the ElementID to unit name + component name ("." -> "_")

The framework needs a unique ID to programmatically interact with the element. Either that ID is uniquely auto-generated or it is an ID of an element that was added to the HTML template for the form.

Ok, thanks.
I'll check the ElementID for StartWith classname.
I'll never use the classname as the Element ID myself. That should work.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.