用户控件内的内容

发布于 2024-09-25 11:08:54 字数 982 浏览 3 评论 0原文

我几乎不好意思问这个问题,但是我们开始了......

我不是用户控件方面的专家,并且需要一些关于尝试实现特定所需功能的设计建议。

目标是拥有一个用户控件,它可以呈现为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

逆流 2024-10-02 11:08:54

您正在寻找的东西绝对是可能的。一种解决方案是创建模板化用户控件。最后,您可以定义与示例类似的内容。类似于:

<uc:MyControl Title="Some TItle" runat="server">
    <ContentsTemplate>
        <p>some random html content</p>
    </ContentsTemplate>
</uc:MyControl>

这里有一个简单的操作方法。我过去曾成功地做到过这一点。 通过 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:

<uc:MyControl Title="Some TItle" runat="server">
    <ContentsTemplate>
        <p>some random html content</p>
    </ContentsTemplate>
</uc:MyControl>

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.

天涯沦落人 2024-10-02 11:08:54

是的,这很有可能。 首先,您可以创建一个自定义控件,如我在本文中所述。 控件中的属性将成为示例中的“标题”等属性。然后您可以使用 本文中展示的技术为您需要的其他 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!

浊酒尽余欢 2024-10-02 11:08:54

看起来您的自定义服务器控件需要两个属性。

Title 属性渲染第一个内部 div 的内容,

Content 渲染第二个内部 div 的内容。如果要在服务器控件内设置内容,则应该为属性使用 [PersistenceMode(PersistenceMode.InnerDefaultProperty)] 属性,如下所示:

[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Contents
{
    get
    {
        string contents = (string)ViewState["Contents"];
        return (contents == null) ? String.Empty : contents;
    }
    set
    {
        ViewState["Contents"] = value;
    }
}

PersistenceModeAttribute 在 MSDN 中的解释如下:

PersistenceModeAttribute 传递
内部默认属性参数
指定视觉设计师
应保留该属性
该属性作为内部应用
默认属性。这意味着一个
视觉设计师保留该属性
在控件的标签内。这
属性只能应用于一个
财产,因为只有一个财产
可以在控件内持久化
标签。房产价值不
包裹在特殊标签中。

有关详细信息,请查看此处的最后一个示例

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 :

[PersistenceMode(PersistenceMode.InnerDefaultProperty)]
public string Contents
{
    get
    {
        string contents = (string)ViewState["Contents"];
        return (contents == null) ? String.Empty : contents;
    }
    set
    {
        ViewState["Contents"] = value;
    }
}

PersistenceModeAttribute is explained in MSDN like that :

PersistenceModeAttribute Passing the
InnerDefaultProperty parameter
specifies that a visual designer
should persist the property to which
the attribute is applied as an inner
default property. This means that a
visual designer persists the property
within the control's tags. The
attribute can be applied to only one
property, because only one property
can be persisted within the control's
tags. The property value is not
wrapped in a special tag.

For more information take a look at the last example here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文