c# - 将 HTML Div 代码转换为控件

标签 c# asp.net html

我有一堆 HTML 代码,用于在控件上制作圆角边框。有没有办法获取此代码并将其转换为某种控件或其他东西,这样我就不必在我所做的一切周围粘贴 10 行 HTML 代码?

<div id="BottomBody">
    <div class="box1024" >
        <div class="content1024">
            <div class="top1024"></div>
            <h1>My Header Information</h1>
            <hr />
            <p>Some text for everyone!</p>
        </div>
        <div class="bottom1024">
            <div></div>
        </div>
    </div>
</div>

需要注意的另一件事是,最里面的 DIV 内使用的 HTML 标签数量将根据我在网站中使用它的位置而变化。因此,在某些情况下,我只会有 1 个标签和 1 个

标签,但在其他情况下,我可能有 1 个标签、1 个

标签、3 个标签和一个 HTML 表。我怎样才能做到这一点?

最佳答案

是的,您正在考虑 UserControl 。提取相关 HTML,将其粘贴到 UserControl .ascx 模板中。

现在,就您的情况而言,您可能希望文本可自定义,对吗?所以你需要替换 <h1>通过</p>带有 ASP.NET 标签的位。生成的 .ascx HTML(不包括 @Control 指令)将如下所示:

<div id="BottomBody">
    <div class="box1024" >
        <div class="content1024">
            <div class="top1024"></div>
            <asp:Label runat="Server" id="label1" />
        </div>
        <div class="bottom1024">
            <div></div>
        </div>
    </div>
</div>

或者,您可以创建两个标签——一个用于标题,一个用于正文。或者甚至只是将标题设置为 runat="Server"本身。

接下来,您将在 .ascx 代码隐藏文件中编写一些代码以公开标签(或多个标签,视情况而定)Text属性(property)。这可能看起来像:

public string Text
{
    get { return label1.Text; }
    set { label1.Text = value; }
}

如果您处于 ASP.NET MVC 世界,请使用 Model data而不是标签,并传入所需的显示文本 string作为模型数据。

编辑

解决问题的更新:

One additional thing to note, the number HTML tags used inside the inner most DIV will change depending on where I use it in my site. So in some cases I will only have 1 tag and 1

tag but in other cases I could have 1 tag, 1

tag, 3 tags, and a HTML table. How can I make that work?

完全相同的技术,假设您引用的内容位于 <div class="content1024"> 内。 。 Text标签的属性可以包含任何所需的任意 HTML,当然,如果您使用 MVC,您可以将任意数量的 HTML 作为字符串传递给模型。

关于c# - 将 HTML Div 代码转换为控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2088345/

相关文章:

javascript - 使用多个 JS 文件——变量/函数重叠。

javascript - 在 D3.js 中操作不透明度和文本

php - 如何制作一个可以连接mysql的简单api?

c# - 使用 C# .net 检查字符串是否包含数组值之一

c# - 错误:Cannot implicitly convert type 'System.DateTime' to 'System.Data.Common.DbParameter'

asp.net - 如何在 ASP.NET 中动态生成列表项到无序列表?

c# - C# 中的 Azure Function App 是否可以执行命令行应用程序?

c#打印xaml创建的页面

c# - Visual Studio 2010 认为它处于 Debug模式(但它设置为 Release模式)

c# - C# 中使用按位求反运算符写入 "byte"类型常量的最短方法?