是否可以拥有一个具有单个模板的自定义服务器控件(意味着用户可以放置他们想要的任何文本),而不必像 FormView 控件中那样需要“ItemTemplate”?
我希望源视图中的控件看起来像这个
<foo:mycontrol runat="server" id="controlid">
用户在这里放置他们想要的任何 html 内容
</foo:mycontrol>
而不是这个
<foo:mycontrol runat="server" id="controlid">
<ItemTemplate>
用户在这里放置他们想要的任何 html 内容
</ItemTemplate>
</foo:mycontrol>
我的自定义服务器控件需要添加 2 个 asp.net 面板控件和 ajax collapsiblepanel 控件。一个面板将是展开/折叠面板,另一个面板是我想要将用户文本放入其中的面板,然后让可折叠面板折叠并隐藏面板。
我知道如何执行此操作(至少我认为我这样做)创建复合服务器控件并使用 ITemplate 但这需要子 >源视图中的标记。
有什么想法吗?
Is it possible to have a custom server control with a single template (meaning the user can put any text they want) without having to require the an "ItemTemplate" like in a FormView control?
I would want the control in Source View to look like this
<foo:mycontrol runat="server" id="controlid">
User puts whatever html content they want here
</foo:mycontrol>
INSTEAD OF THIS
<foo:mycontrol runat="server" id="controlid">
<ItemTemplate>
User puts whatever html content they want here
</ItemTemplate>
</foo:mycontrol>
My custom server control needs to add 2 asp.net panel controls and the ajax collapsiblepanel control. one panel will be the expand/collapse panel and the other panel is what I would want to put the user text into and then have the collapsible panel collapse and hide the panel.
I know how to do this (at least I think I do) creating a composite server control and using ITemplate but that requires the child <ItemTemplate
> tag in source view.
Any ideas?
发布评论
评论(3)
我还没有对此进行测试,但我认为您可以通过继承文字或标签控件,然后读取/写入 Text 属性来实现此目的。
ps 下次当您发布问题时,请检查预览以查看其是否可读,并在前面添加 4 个空格来格式化代码,以便实际显示并突出显示语法。
I haven't tested this but I would think you could do this by inheriting from the literal or label control and then reading/writing to the Text property.
p.s. next time when you post a question check the preview to see if it's readable and format code with 4 spaces in-front so it's actually shown and syntax highlighted.
嗯,为什么不使用仅指定要折叠的控件的方法。例如,您的声明可能看起来像
在内部,您的 mycontrol 通过使用 FindControl 方法获取指定 TargetControlId 的实例 (这是一个递归版本)。对于您需要的第二个面板也可以执行相同的操作。
因此,您的服务器控件只获取配置信息并且不渲染任何内容,但在这种情况下控制其他面板的渲染。在我看来,这是一个更加灵活的解决方案。
Hmm why don't you use an approach where you just specify the controls to be collapsed. For instance your declaration could look like
Internally, your mycontrol gets an instance of the specified TargetControlId by using the FindControl method (here's a recursive version). The same could be done for the 2nd panel you need.
Your server control does therefore just take configuration info and doesn't render anything, but controls the rendering of the other panels in this case. It is a much more flexible solution in my eyes.
Juri,
我应该澄清一下,这个控件可能会被非开发人员或我们不希望需要太多设置属性知识的开发人员使用,所以我正在尝试开发一个控件,他们可以像面板一样将其拖动到上面,只需输入文本,然后设置一个属性,即标题。我能够创建一个复合控件,我创建了一个标题面板、可折叠面板并利用了 asp.net ajax collapsiblepanel 控件。我必须添加一个模板,但我不想这样做。
达雷尔
Juri,
I should have clarified that this control might be used by non-developers or developers who we do not want to require too much knowledge of setting properties so I'm trying to develop a control where they would drag it on like a panel and just enter text and maybe set one property which would be the title. I was able to create a compositecontrol which I created a title panel, the collapsible panel and utilized the asp.net ajax collapsiblepanel control. I had to add an Template though which I would prefer not to do.
Darrell