用户控件内的内容
我几乎不好意思问这个问题,但是我们开始了......
我不是用户控件方面的专家,并且需要一些关于尝试实现特定所需功能的设计建议。
目标是拥有一个用户控件,它可以呈现为 html 元素和 css 的复杂结构,从而形成一个优雅的容器框。问题在于如何填充框中的内容,因为用户控件的每个实例都有其自己的单独的 HTML 内容。用户控件的内容容器 div 将深深嵌套在呈现的 html 结构中。以编程方式设置用户控件的内容或使用属性是不可取的。
在伪代码中,所需的语法类似于:
<usercontrol Title="Some Title"><p>some random html content</p></usercontrol>
呈现的用户控件的示例如下:
<div class="CommonBox">
<div class="Title">Some Title</div>
<div class="Content"><p>some random html content</p></div>
</div>
我希望我的解释是足够的。这对任何人都有意义还是所需的功能无法实现?
干杯!
编辑
我尝试了模板化的用户控制建议,希望能找到一个不错的解决方案。我“退回”此站点,现在有一个可用的模板化用户控件。我的下一个问题是,如何以编程方式访问模板中嵌套的控件...假设示例链接中的“描述”模板中有一个文本框控件,并且我想以编程方式设置其值?这可能吗?
I am almost too embarrassed to ask this question, but here we go...
I am not an expert with user controls, and need some design advise regarding trying to achieve a specific desired functionality.
The goal is to have a usercontrol that would render as a complex structure of html elements and css to form an elegant container box. The problem lies in how to populate the contents of the box as each instance of the usercontrol would have its own individual HTML content. The content container div of the usercontrol would be nested deep within the structure of the rendered html. It is undesirable to programmatically set the contents of the usercontrol, or use properties.
In psuedo-code, the desired syntax would be something like:
<usercontrol Title="Some Title"><p>some random html content</p></usercontrol>
A sample of the rendered usercontrol would be:
<div class="CommonBox">
<div class="Title">Some Title</div>
<div class="Content"><p>some random html content</p></div>
</div>
I hope my explanation is adequate. Does this make sense to anyone or is the desired functionality unachievable?
Cheers!
EDIT
I attempted the templated user control suggestion in the hopes of coming to a decent solution. I "waybacked" this site and now have a working templated user control. My next question is, how can I programmatically access the controls nested within the template... say there is a textbox control in the "Description" template from the example link and I want to set its value programmatically? Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在寻找的东西绝对是可能的。一种解决方案是创建
模板化用户控件
。最后,您可以定义与示例类似的内容。类似于:这里有一个简单的操作方法。我过去曾成功地做到过这一点。 通过 Google 找到有关该主题的大量资源。
What you're looking for is definitely possible. One solution is to create a
templated user control
. In the end you can define the contents similar to how your example looks. Something like:Here's a simple how-to. I've done this in the past with success. There are lots of resources found through Google as well on the topic.
是的,这很有可能。 首先,您可以创建一个自定义控件,如我在本文中所述。 控件中的属性将成为示例中的“标题”等属性。然后您可以使用 本文中展示的技术为您需要的其他 html 输入添加子节点。
享受!
Yes this is very possible. First you can create a custom control as I describe in this post. Properties in the control become attributes like "Title" you have in your example. Then you can extend your control with the technique shown in this post to add child nodes for the other html inputs you will require.
Enjoy!
看起来您的自定义服务器控件需要两个属性。
Title 属性渲染第一个内部 div 的内容,
Content 渲染第二个内部 div 的内容。如果要在服务器控件内设置内容,则应该为属性使用
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
属性,如下所示:PersistenceModeAttribute 在 MSDN 中的解释如下:
有关详细信息,请查看此处的最后一个示例。
It looks like you need two properties for your custom server control.
Title property to render first inner div's content,
Content for the second one. If you want to set contents inside your server control, you should use
[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
attribute for your property, like this :PersistenceModeAttribute is explained in MSDN like that :
For more information take a look at the last example here.