创建此内容的最佳 WebControl 是什么

发布于 2024-09-01 23:14:29 字数 2495 浏览 12 评论 0原文

当前输出

替代文本 http://www.balexandre.com/temp/2010- 05-19_1159.png

想要输出

替代文本 http://www .balexandre.com/temp/2010-05-19_1158.png

当前代码

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            populateData();
    }

    private void populateData()
    {
        List<temp> ls = new List<temp>();

        ls.Add(new temp { a = "AAA", b = "aa", c = "a", dt = DateTime.Now });
        ls.Add(new temp { a = "BBB", b = "bb", c = "b", dt = DateTime.Now });
        ls.Add(new temp { a = "CCC", b = "cc", c = "c", dt = DateTime.Now.AddDays(1) });
        ls.Add(new temp { a = "DDD", b = "dd", c = "d", dt = DateTime.Now.AddDays(1) });
        ls.Add(new temp { a = "EEE", b = "ee", c = "e", dt = DateTime.Now.AddDays(2) });
        ls.Add(new temp { a = "FFF", b = "ff", c = "f", dt = DateTime.Now.AddDays(2) });


        TemplateField tc = (TemplateField)gv.Columns[0];  // <-- want to assign here just day
        gv.Columns.Add(tc); // <-- want to assign here just day + 1
        gv.Columns.Add(tc); // <-- want to assign here just day + 2

        gv.DataSource = ls; 
        gv.DataBind(); 
    }
}

public class temp
{
    public temp() { }

    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
    public DateTime dt { get; set; }
}

和 HTML

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("a") %>' Font-Bold="true" /><br />
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("b") %>' Font-Italic="true" /><br />
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("dt") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

我试图避免的是重复代码,所以我只能使用一个唯一的 TemplateField

我可以使用 3 个 GridView 来完成此操作,每天一个,但我确实在尝试简化代码,因为网格将完全相同(如 HTML 代码所示) ,只是数据源发生变化。

非常感谢任何帮助,谢谢。

current output

alt text http://www.balexandre.com/temp/2010-05-19_1159.png

wanted output

alt text http://www.balexandre.com/temp/2010-05-19_1158.png

current code

public partial class test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
            populateData();
    }

    private void populateData()
    {
        List<temp> ls = new List<temp>();

        ls.Add(new temp { a = "AAA", b = "aa", c = "a", dt = DateTime.Now });
        ls.Add(new temp { a = "BBB", b = "bb", c = "b", dt = DateTime.Now });
        ls.Add(new temp { a = "CCC", b = "cc", c = "c", dt = DateTime.Now.AddDays(1) });
        ls.Add(new temp { a = "DDD", b = "dd", c = "d", dt = DateTime.Now.AddDays(1) });
        ls.Add(new temp { a = "EEE", b = "ee", c = "e", dt = DateTime.Now.AddDays(2) });
        ls.Add(new temp { a = "FFF", b = "ff", c = "f", dt = DateTime.Now.AddDays(2) });


        TemplateField tc = (TemplateField)gv.Columns[0];  // <-- want to assign here just day
        gv.Columns.Add(tc); // <-- want to assign here just day + 1
        gv.Columns.Add(tc); // <-- want to assign here just day + 2

        gv.DataSource = ls; 
        gv.DataBind(); 
    }
}

public class temp
{
    public temp() { }

    public string a { get; set; }
    public string b { get; set; }
    public string c { get; set; }
    public DateTime dt { get; set; }
}

and in HTML

<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("a") %>' Font-Bold="true" /><br />
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("b") %>' Font-Italic="true" /><br />
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("dt") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

What I'm trying to avoid is repeat code so I can only use one unique TemplateField

I can accomplish this with 3 x GridView, one per each day, but I'm really trying to simplify code as the Grid will be exactly the same (as the HTML code goes), just the DataSource changes.

Any help is greatly appreciated, Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

围归者 2024-09-08 23:14:29

为此,请使用ListView

use ListView for this.

游魂 2024-09-08 23:14:29

您可以创建自定义模板并提供该模板三次,如下所示: http://msdn.microsoft.com/en-us/library/aa289501%28VS.71%29.aspx

You could create a custom template and supply that template three times, as in : http://msdn.microsoft.com/en-us/library/aa289501%28VS.71%29.aspx

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