简体   繁体   English

使用asp.net用户控件按属性值对列表进行排序

[英]Sorting a list with asp.net usercontrols by property value

I am populating a div with many usercontrols, right now they are being added in the order that they are being read from the db. 我正在用许多usercontrol填充div,现在按从数据库中读取的顺序添加它们。 The UserControls markup looks like this: UserControls标记如下所示:

<div class="Content_Thumb">

    <div class="Video_pic">
        <a href="tutorialname.aspx">
        <asp:Label ID="TutInfo" CssClass="CoverLbl_top" runat="server" Text="Label"></asp:Label>
        <asp:Label ID="TutInfo2" CssClass="CoverLbl_bottom" runat="server" Text="Description..."></asp:Label>
        <asp:Image ID="ThumbPic" CssClass="Thumb_Pic" runat="server" ImageUrl="/Images/Video_Thumb.png" />
        </a>
    </div>

    <div class="Thumb_Info">
        <asp:Label ID="Views" CssClass="ViewsLbl" runat="server" Text="680"></asp:Label>
        <asp:Label ID="Comments" CssClass="CommentsLbl" runat="server" Text="11"></asp:Label>
        <asp:Label ID="Likes" CssClass="LikesLbl" Style="TEXT-ALIGN: right" runat="server" Text="133"></asp:Label>
    </div>

    <div class="Thumb_Border">
            <asp:Image ID="UserPic" CssClass="Thumb_UploaderPic" runat="server" ImageUrl="/Images/Profile_Placeholder.png" />
            <a href="google.com">
                <asp:Label ID="UserLbl" CssClass="Thumb_UploaderInfo" runat="server" Text="Label"></asp:Label>
            </a>
    </div>

Then i've got these properties that are bound to the labels (some removed since i dont want to clutter the post). 然后,我将这些属性绑定到标签(由于我不想弄乱文章,所以删除了一些属性)。

        public string TutorialInfo 
    { 
        get; 
        set; 
    }

    public int TutorialViews
    {
        get;
        set;  
    }

    public override void DataBind()
    {
        TutInfo.Text = TutorialInfo;
        Views.Text = TutorialViews.ToString();
        Comments.Text = TutorialComments.ToString();
        Likes.Text = TutorialLikes.ToString();
        UserLbl.Text = TutorialUploader;
        base.DataBind();
    }

Now, what I want to do with these controls are to sort them by the property values. 现在,我要对这些控件进行的操作是按属性值对它们进行排序。 (highest number of views loads first etc). (最多的视图数首先加载等)。 I'm currently at the stage where i've added them to a list, and now I'm trying to sort that list using LINQ's OrderBy like I saw on a post here. 我目前正处于将它们添加到列表的阶段,现在我正尝试使用LINQ的OrderBy对列表进行排序,就像我在此处的帖子中看到的那样。

List<UserControl> controls = new List<UserControl>();

        foreach (var Tutorial in dataconnection.Tutorial)
        {
            var control = LoadControl("~/WebUserControl1.ascx") as WebUserControl1;

            control.TutorialInfo = Tutorial.Title;
            control.TutorialComments = (int)Tutorial.Comments;
            control.TutorialViews = (int)Tutorial.Views;
            control.TutorialLikes = (int)Tutorial.Likes;
            control.TutorialUploader = Tutorial.Uploader;
            control.DataBind();
            //base.OnPreRender(e);
            ThumbTest.Controls.Add(control);
            controls.Add(control);
            var testcount = controls.Count();
            control.Visible = false;

        }

        var SortedList = controls.OrderBy(o => o.TutorialViews).ToList();

However i'm not able to access the properties even though they are public? 但是,即使它们是公共的,我也无法访问它们? "o.TutorialViews" is throwing an error. “ o.TutorialViews”引发错误。 So how can i sort these controls? 那么我该如何对这些控件进行排序?

I'm Fairly new to programming in general so my apologies if this turns out to be a silly mistake. 我一般对编程都不熟悉,因此如果这是一个愚蠢的错误,我深表歉意。

TLDR: How do I sort Usercontrols by property value. TLDR:如何按属性值对Usercontrols进行排序。

Your list is a list of UserControl objects, which is the generic user control provided by ASP.NET: 您的列表是UserControl对象的列表,它是ASP.NET提供的通用用户控件:

List<UserControl> controls = new List<UserControl>();

The UserControl object doesn't have a property called TutorialViews , so you can't access that property on that type. UserControl对象没有名为TutorialViews的属性,因此您无法在该类型上访问该属性。 However, you're adding custom user controls to the list: 但是,您要将自定义用户控件添加到列表中:

var control = LoadControl("~/WebUserControl1.ascx") as WebUserControl1;

If every item in the list is going to be of type WebUserControl1 then you can declare the list itself with that type: 如果列表中的每个项目都将是WebUserControl1类型,则可以使用该类型声明列表本身:

List<WebUserControl1> controls = new List<WebUserControl1>();

Since WebUserControl1 does define that property, you'll be able to access it. 由于WebUserControl1 确实定义了该属性,因此您可以访问它。

Another approach might be to cast it when you access it, like this: 另一种方法可能是在您访问它时对其进行强制转换,如下所示:

controls.OrderBy(o => ((WebUserControl1)o).TutorialViews)

But that's kind of messy and shouldn't be necessary. 但这有点混乱,不应该这样做。 It's better to use the correct type in the first place. 最好首先使用正确的类型。

Essentially, this is a symptom of the statically typed nature of C# (and many other languages). 本质上,这是C#(和许多其他语言)的静态类型化性质的症状。 Type B inherits from Type A, so every instance of B is an instance of A. But not every instance of A is an instance of B, so you can't call a B property on an instance of A. 类型B继承自类型A,因此B的每个实例都是A的实例。但是,并非A的每个实例都是B的实例,因此您不能在A的实例上调用B属性。

Think of an analogy... You have a list of Shape objects, and you populate it with Circle objects. 考虑一个类比...您有一个Shape对象列表,并用Circle对象填充它。 This works, because Circle s are Shape s. 这是Circle的,因为CircleShape But then you try to access the Radius property on an element in that list, and you get an error. 但是,然后您尝试访问该列表中某个元素的Radius属性,就会收到错误消息。 This is because it's a list of Shape s and not every shape has a Radius . 这是因为它是Shape的列表,并且并非每个形状都有Radius You'd need to either make it a list of Circle s or cast that element to a Circle for the compiler to know that there's a Radius . 您需要将其设置为Circle的列表,或将该元素转换为Circle以使编译器知道存在Radius

For lots of further reading on the subject, take a look at Polymorphism and the Liskov Substitution Principle . 有关该主题的更多信息,请阅读多态性Liskov替代原理

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

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