我正在使用第 3 方系统在网站中实现某些表单。
第 3 方系统为我提供了这些表单的 XML 定义。例如
<form>
<segment>
<label>The header</label>
<fields>
...
<field>
<id>field_Dob</id>
<type>Date</type>
<label>Date of Birth</label>
<required>1</required>
</field>
...
</fields>
</segment>
...
</form>
我在服务器控件中解析这个 XML 并以编程方式生成控件树。控件的标签在 XML 中传递。
将小帮助文本“注入”到此表单中是我们建议的一部分。
理想情况下,我想从顶级控件的标记中传递这些帮助文本,以便非开发人员(HTML 僧侣)可以更改帮助文本,并通过其 ID 将它们与字段相关联。像这样的东西
<controls:MyCrazyForm runat="server">
<helpTexts>
<helpText for="field_Dob">
Some rambling nonsense to do with the DOB field
</helpText>
...
</helpTexts>
</controls:MyCrazyForm>
控件被递归解析。
The Form creates a fieldset for each segment, fieldsets create many FieldXXX (where XXX = date, text, combobox etc) depending on the data type.
The FieldXXX types create a div and then several standard .net controls (TextBox, DropDownList, etc) to actually render themselves. It is at this point, within the containing div that I need to output the help text.
My Question
What is the "best" way to get these texts from the top-level form control to these child controls which are 3 or 4 levels deeper in the control tree.
There will only ever be one of these forms on a page. Should I make the top level form as Singleton and get it like so...?
if(MyCrazyForm.Instance.HelpTexts.ContainsKey("theIdOfTheCurrentField"))
{
this.HelpText = MyCrazyForm.Instance.HelpTexts["theIdOfTheCurrentField"];
}
Should I pass a reference to the form into every control all the way down the tree (this seems messy)?
Am I miles of target with my architecture of this (although it's working realyl nicely at the moment) form and should I look at a different method of implementation?
Thanks