如何将 AJAX 树视图转换为链接按钮?

发布于 2024-11-24 05:37:17 字数 327 浏览 1 评论 0原文

我需要一些有关将 AJAX TreeView 控件转换为 LinkBut​​ton 的建议。 为什么?让ASP.NET页面变得更轻量。目前,当选择TreeViewNodes 时,与该节点相关的记录将显示在网格中。这种实现使页面变得很重。因此我们决定将其更改为 LinkBut​​tons

我只是想知道我需要做、考虑哪些事情,或者这是一个好主意吗?老实说,无论这是好还是坏主意,我仍然需要这样做。

我最初的理解是,这将像一个列表例如:项目符号列表

I need some advice regarding conversion of an AJAX TreeView control to a LinkButton.
Why? To make the ASP.NET page lighter. Currently, when the Nodes of the TreeView is selected, records related to that node are displayed in a grid. This implementation makes the page heavy. So we decided to changed this to LinkButtons.

I just want know what are the things I need to do, consider, or is this a good idea? Honestly, whether this is a good or bad idea, I still need to do this.

My initial understanding is that this will turn out like a list ex: bullet list.

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

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

发布评论

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

评论(1

白云不回头 2024-12-01 05:37:42

以下答案基于您的评论,并基于 您的其他问题

以下示例使用 ListView 而不是 Repeater。 ListView 很棒,因为它们比 Repeater 具有更大的灵活性。此外,正如您在下面的示例代码中看到的,绑定嵌套/子 ListView 和绑定嵌套/孙 ListView 都可以以声明方式完成,无需任何隐藏代码

以下代码生成的示例

在此处输入图像描述

ASPX

<asp:ListView runat="server" ID="lvw">
    <LayoutTemplate>
        <ul>
            <li id="itemPlaceholder" runat="server" />
        </ul>
    </LayoutTemplate>
    <ItemTemplate>
        <li>    
            <asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton>
            <asp:ListView runat="server" ID="lvw2" DataSource='<%# Eval("Children")%>'>
                <LayoutTemplate>
                    <ul>
                        <li id="itemPlaceholder" runat="server" />
                    </ul>
                </LayoutTemplate>
                <ItemTemplate>
                    <li><asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton>
                        <asp:ListView runat="server" ID="lvw3" DataSource='<%# Eval("Children")%>'>
                            <LayoutTemplate>
                                <ul>
                                    <li id="itemPlaceholder" runat="server" />
                                </ul>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <li><asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton></li>
                            </ItemTemplate>
                        </asp:ListView>
                    </li>
                </ItemTemplate>
            </asp:ListView>
        </li>
    </ItemTemplate>
</asp:ListView>

C#

lvw.DataSource = personList;
lvw.DataBind();

如您所见,在 C# 代码中,我创建了一个“Person”列表,如下所示。每个 Person 对象都有一个子 Person 对象列表,每个子 Person 对象都有一个子 Person 对象列表。通过以这种方式创建对象,绑定 ListView 实际上就像我所展示的一样简单。使用下面的 Person 对象运行一个快速示例,以便您亲自查看。

Person 对象

public class Person
{
    public string name { get; set; }
    public List<Person> Children { get; set; }
}

对于您的测试,您可以创建一个 Page_Load 方法,如下所示:

protected void Page_Load(object sender, EventArgs e)
    {
        List<Person> personList = new List<Person>();
        Person person1 = new Person() { name = "Child 1" };
        Person person2 = new Person() { name = "Child 2" };
        List<Person> childPersonList1 = new List<Person>();
        childPersonList1.Add(person1);
        childPersonList1.Add(person2);
        Person person3 = new Person() { name = "Person 1" };
        person3.Children = childPersonList1;
        personList.Add(person3);
        Person person4 = new Person() { name = "Child 3" };
        Person person10 = new Person() { name = "Grandchild 1" };
        Person person11 = new Person() { name = "Grandchild 2" };
        Person person12 = new Person() { name = "Grandchild 3" };
        List<Person> grandchildPersonList1 = new List<Person>();
        grandchildPersonList1.Add(person10);
        grandchildPersonList1.Add(person11);
        grandchildPersonList1.Add(person12);
        person4.Children = grandchildPersonList1;
        Person person5 = new Person() { name = "Child 4" };
        List<Person> childPersonList2 = new List<Person>();
        childPersonList2.Add(person4);
        childPersonList2.Add(person5);
        Person person6 = new Person() { name = "Person 2" };
        person6.Children = childPersonList2;
        personList.Add(person6);
        Person person7 = new Person() { name = "Child 5" };
        Person person8 = new Person() { name = "Child 6" };
        List<Person> childPersonList3 = new List<Person>();
        childPersonList3.Add(person7);
        childPersonList3.Add(person8);
        Person person9 = new Person() { name = "Person 3" };
        person9.Children = childPersonList3;
        personList.Add(person9);

        lvw.DataSource = personList;
        lvw.DataBind();
    }

请参阅以下 StackOverflow 问题以了解有关 Repeater 和 ListView 之间差异的更多信息:Repeater、ListView、DataList、DataGrid、GridView ... 选择哪个?

The following answer is based on your comment and builds on the answer provided in your other question.

The following example uses a ListView instead of a Repeater. ListViews are great because they'll give you much more flexibility over a Repeater. Moreover, as you can see in the sample code below, binding the nested/child ListView and binding the nested/grandchild ListView can all be done declaratively without any code-behind.

Example of what the following code will produce

enter image description here

ASPX

<asp:ListView runat="server" ID="lvw">
    <LayoutTemplate>
        <ul>
            <li id="itemPlaceholder" runat="server" />
        </ul>
    </LayoutTemplate>
    <ItemTemplate>
        <li>    
            <asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton>
            <asp:ListView runat="server" ID="lvw2" DataSource='<%# Eval("Children")%>'>
                <LayoutTemplate>
                    <ul>
                        <li id="itemPlaceholder" runat="server" />
                    </ul>
                </LayoutTemplate>
                <ItemTemplate>
                    <li><asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton>
                        <asp:ListView runat="server" ID="lvw3" DataSource='<%# Eval("Children")%>'>
                            <LayoutTemplate>
                                <ul>
                                    <li id="itemPlaceholder" runat="server" />
                                </ul>
                            </LayoutTemplate>
                            <ItemTemplate>
                                <li><asp:LinkButton runat="server" CommandArgument='<%# Eval("Name")%>'><%# Eval("Name")%></asp:LinkButton></li>
                            </ItemTemplate>
                        </asp:ListView>
                    </li>
                </ItemTemplate>
            </asp:ListView>
        </li>
    </ItemTemplate>
</asp:ListView>

C#

lvw.DataSource = personList;
lvw.DataBind();

As you can see, in the C# code, I've created a list of "Person" as follows. Each Person object has a list of child Person objects and aach child Person object has a list of child Person objects. By creating your objects in this manner, binding the ListView is really as simple as I've shown. Use the Person object below to run a quick sample so you can see for yourself.

Person object

public class Person
{
    public string name { get; set; }
    public List<Person> Children { get; set; }
}

For your test, you can create a Page_Load method as follows:

protected void Page_Load(object sender, EventArgs e)
    {
        List<Person> personList = new List<Person>();
        Person person1 = new Person() { name = "Child 1" };
        Person person2 = new Person() { name = "Child 2" };
        List<Person> childPersonList1 = new List<Person>();
        childPersonList1.Add(person1);
        childPersonList1.Add(person2);
        Person person3 = new Person() { name = "Person 1" };
        person3.Children = childPersonList1;
        personList.Add(person3);
        Person person4 = new Person() { name = "Child 3" };
        Person person10 = new Person() { name = "Grandchild 1" };
        Person person11 = new Person() { name = "Grandchild 2" };
        Person person12 = new Person() { name = "Grandchild 3" };
        List<Person> grandchildPersonList1 = new List<Person>();
        grandchildPersonList1.Add(person10);
        grandchildPersonList1.Add(person11);
        grandchildPersonList1.Add(person12);
        person4.Children = grandchildPersonList1;
        Person person5 = new Person() { name = "Child 4" };
        List<Person> childPersonList2 = new List<Person>();
        childPersonList2.Add(person4);
        childPersonList2.Add(person5);
        Person person6 = new Person() { name = "Person 2" };
        person6.Children = childPersonList2;
        personList.Add(person6);
        Person person7 = new Person() { name = "Child 5" };
        Person person8 = new Person() { name = "Child 6" };
        List<Person> childPersonList3 = new List<Person>();
        childPersonList3.Add(person7);
        childPersonList3.Add(person8);
        Person person9 = new Person() { name = "Person 3" };
        person9.Children = childPersonList3;
        personList.Add(person9);

        lvw.DataSource = personList;
        lvw.DataBind();
    }

See the following StackOverflow question to learn more about the differences between a Repeater and a ListView: Repeater, ListView, DataList, DataGrid, GridView ... Which to choose?

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