简体   繁体   English

占位符控件和对齐子控件

[英]Placeholder control, and aligning child controls

I have a program that displays messages posted to a forum.我有一个程序可以显示发布到论坛的消息。 Right now, I am using the Response.Write method in the code behind to insert the HTML I want.现在,我正在使用后面代码中的 Response.Write 方法来插入我想要的 HTML。 But I would much rather avoid putting HTML in the code behind (because it is not in the spirit of the whole code behind/design separation feature of .NET).但我宁愿避免将 HTML 放在后面的代码中(因为它不符合 .NET 的整个代码背后/设计分离功能的精神)。 I would like to use a content placeholder, and insert child controls dynamically.我想使用内容占位符,并动态插入子控件。 However, I can only seem to insert them side by side, and one after another.但是,我似乎只能将它们并排插入,一个接一个。 I would like to have something that looks like this:我想要一些看起来像这样的东西:

Table Column 1 Table Column 2表列 1 表列 2
Username: [UserName]用户名:[用户名]
[MessageSubject] [信息主题]
Posted on: [PostDate]发表于:[发布日期]
User Rating: [UserRating]用户评分:[UserRating]

But the only thing I can seem to accomplish is:但我似乎唯一能完成的是:

Username: [UserName]Posted On: [PostDate] User Rating [UserRating][MessageSubject], without links or formatting.用户名:[UserName]发布于:[PostDate] 用户评分 [UserRating][MessageSubject],没有链接或格式。

How do I put paragraphs, line breaks, form buttons and regular hyperlinks into a content placeholder, and make them align the way I want them to?如何将段落、换行符、表单按钮和常规超链接放入内容占位符,并使它们按照我想要的方式对齐?

You do it much the same way as you would int the.aspx page, for example:您执行此操作的方式与 int the.aspx 页面的方式大致相同,例如:

Table table = new Table();
TableRow row1 = new TableRow();
table.Rows.Add(row1);
TableCell cell1 = new TableCell();
TableCell cell2 = new TableCell();
row1.Cells.Add(cell1);
row1.Cells.Add(cell2);
Label label1 = new Label();
label1.Text = "Username:";
cell1.Controls.Add(label1);
TextBox textBox1 = new TextBox();
textBox1.ID = "userNameTextBox";
cell2.Controls.Add(textBox1);
// and so on...
myPlaceholder.Controls.Add(table);

would be equivalent to:相当于:

<asp:Table runat="server">
  <asp:TableRow runat="server">
    <asp:TableCell runat="server">
      <asp:Label runat="server" Text="Username:" />
    </asp:TableCell>
    <asp:TableCell runat="server">
      <asp:TextBox runat="server" ID="userNameTextBox" />
    </asp:TableCell>
  </asp:TableRow>
</asp:Table>

Of course the code could be cleverer, this is just to illustrate the point.当然代码可以更聪明,这只是为了说明这一点。 Any hierarchy of controls you can build declaratively to control you layout, you can do programatically too.您可以以声明方式构建以控制布局的任何控件层次结构,您也可以以编程方式进行。

Try this overview on Web Parts .试试这个关于Web 零件的概述。 Or more simply, just use User Controls或者更简单地说,只需使用用户控件

You could do something like this:你可以这样做:

<table>
<tr><td>Table Column 1</td><td>Table Column 2</td></tr>
<tr><td>Username:</td><td>[UserName]</td></tr>
<tr><td></td><td><p>[MessageSubject]</td></tr>
<tr><td>Posted On:</td><td>[PostDate]</td></tr>
<tr><td>User Rating:</td><td>[UserRating]</td></tr>
</table>

This will produce something like this:这将产生如下内容:

Some may argue with me or vote me down, but tables are not evil, it's how you use them that is.有些人可能会与我争论或投票否决我,但桌子并不邪恶,这就是你如何使用它们。

Tom, I've implemented essentially the exact same pattern as you are attempting.汤姆,我已经实现了与您尝试的基本完全相同的模式。

Keep in mind that the important distinction isn't between the ASPX and the code-behind but rather between the UI and the business logic.请记住,重要的区别不是 ASPX 和代码隐藏之间的区别,而是 UI 和业务逻辑之间的区别。 Indeed, the code-behind for ASPX pages is all about ensuring appropriate UI behavior.事实上,ASPX 页面的代码隐藏都是为了确保适当的 UI 行为。 Worse yet, the attempt to stay pure in this regard is preventing you from executing the design you seek.更糟糕的是,在这方面保持纯粹的尝试会阻止您执行您寻求的设计。 In the end, I did end up placing spacing HTML - in code - between my controls in order to make the layout work and have never regretted it.最后,我确实在我的控件之间放置了间距 HTML - 在代码中 - 以使布局工作并且从未后悔过。

Rather than focusing on the ASPX/Code-behind distinction, I'd strongly suggest that you focus on pulling apart your UI code from your business logic.与其专注于 ASPX/代码隐藏的区别,我强烈建议您专注于将您的 UI 代码与业务逻辑分开。 To do this, just add a Class Library (DLL) project to your solution and then place a reference to the DLL in your BIN directory.为此,只需将 Class 库 (DLL) 项目添加到您的解决方案中,然后在您的 BIN 目录中放置对 DLL 的引用。 You will then have the ASPX page make calls into the DLL to execute business rules and pull information from your database.然后,您将让 ASPX 页面调用 DLL 以执行业务规则并从数据库中提取信息。 That is the distinction that makes sense from a best-practices perspective.从最佳实践的角度来看,是有意义的区别。

I agree that turning this into a UserControl is probably the right way to go.我同意将其转换为 UserControl 可能是 go 的正确方法。 I would propose outputting html similar to the following:我建议输出类似于以下内容的 html:

<div class="message">
    <div>
        <span class="column1">Username:</span>
        <span class="column2">[UserName]</span>
    </div>
    <div>[MessageSubject]</div>
    <div>
        <span class="column1">Posted On:</span>
        <span class="column2">[PostedOn]</span>
    </div>
    <div>
        <span class="column1">User Rating:</span>
        <span class="column2">[UserRating]</span>
    </div>
</div>

You can then apply styling via CSS such as:然后,您可以通过 CSS 应用样式,例如:

<style>
    .message .column1
    {
        display: inline-block;
        width: 150px;
    }
    .message .column2
    {
        display: inline-block;
        width: 300px;
    }
</style>

You'll see that the <div> tag is generally the answer to "how do I put this on the next line?"您会看到<div>标签通常是“我如何将它放在下一行?”的答案。

EDIT: If you're new to web layout, you might appreciate discovering blueprint sooner, rather than later.编辑:如果您不熟悉 web 布局,您可能会喜欢早点发现蓝图,而不是晚点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM