6

关于表(在这种情况下为单列)人口的简单问题。尽管这似乎是一个简单的问题,但我从未参与过前端领域,所以就这样吧。

布局为 2 列 8 行。就像是。

Name        A
LastName    B
Age         C
BirthDate   D
...

第 1 列是稳定的,如果你愿意,“标题”不会​​改变。

A,B,C,D 是查询数据库的结果。所以,我能想到的选择是:

  1. 绘制一个 2Column - 8Row 表并将 TextBoxes 放置在 A、B、C、D... 字段中。所以稍后可以用查询的结果填充它们(这个选项不是最“漂亮”的选项,因为 TextBoxes 改变了旨在使用 .CSS 文件被整个页面吸收的设计。

  2. 设置数据网格。我认为这里的问题是必须更改某些 A、B、C、D 字段以供以后查询使用。而且我不确定 Datagrids 是否适合这一点。

我有解决这个问题的“好方法”吗?提前致谢。

编辑。

A、B、C、D 数据保存在 DataSet 中。

4

2 回答 2

10

To me, the way you're describing the data doesn't really lend itself too well for a DataGrid. This control works best for data that you plan to display in a standard tabular style, where the column names go across the top and then you display the row(s) of values underneath. It's also a little unclear to me if you are intending to bind one or many instances of your Object (which I will just call Person for now) to the UI.

Let's go ahead and define that object:

public class Person {
    public String Name { get; set; }
    public String LastName { get; set; }
    public int Age { get; set; }
    public DateTime BirthDate { get; set; }
}

To bind a single instance of Person to your UI, a simple HTML table should work fine. I'm using TextBoxes to display the values here, but if you don't need to edit them then just use Labels instead.

<table>
    <tr><td>Name:</td><td><asp:TextBox ID="txtName" runat="server" /></td></tr>
    <tr><td>Last Name:</td><td><asp:TextBox ID="txtLastName" runat="server" /></td></tr>
    <tr><td>Age:</td><td><asp:TextBox ID="txtAge" runat="server" /></td></tr>
    <tr><td>Birthdate:</td><td><asp:TextBox ID="txtBirthDate" runat="server" /></td></tr>                  
</table>  

It's pretty trivial at this point to bind the properties from Person to their respective controls on the page using the code-behind.

If you wanted to use this same layout to display multiple instances of Person on a page, go with the ASP.net Repeater. The markup for this would look more like:

 <asp:Repeater ID="repPeople" runat="server">
    <ItemTemplate>
        <table>
            <tr><td>Name:</td><td><asp:TextBox ID="txtName" runat="server" Text='<%# Eval("Name") %>' /></td></tr>
            <tr><td>Last Name:</td><td><asp:TextBox ID="txtLastName" runat="server" Text='<%# Eval("LastName") %>' /></td></tr>
            <tr><td>Age:</td><td><asp:TextBox ID="txtAge" runat="server" Text='<%# Eval("Age") %>' /></td></tr>
            <tr><td>Birthdate:</td><td><asp:TextBox ID="txtBirthDate" runat="server" Text='<%# String.Format("{0:d}", Eval("BirthDate")) %>' /></td></tr>                  
        </table>                
    </ItemTemplate>
</asp:Repeater>

In the code-behind, you just bind a collection of Person to the DataSource property on the Repeater:

protected void Page_Load(object sender, EventArgs e) {

    // A simple example using Page_Load
    List<Person> people = new List<Person>();
    for (int i = 0; i < 10; i++) {
        people.Add(new Person() {Name = "Test", Age = 10, BirthDate=DateTime.Now, LastName = "Test"});
    }

    if (!IsPostBack) {
        repPeople.DataSource = people;
        repPeople.DataBind();
    }

}

Note: You could accomplish a similar layout using CSS instead of tables, however the same principles apply between binding a single vs multiple objects. Just replace the table layout in this example with whatever markup you ultimately define.

于 2012-08-13T15:09:28.137 回答
1

WebForms 中有一个表格控件,您应该可以从 DataSet 中填充它,请参阅 MSDN 文档:http: //msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.table.aspx

于 2012-08-13T14:04:57.873 回答