How can I hide a repeater in ASP.NET C# if the DataSource contains no items?

前端 未结 9 1914
小鲜肉
小鲜肉 2021-01-02 17:29

I have an ASP.NET page that uses a repeater nested within another repeater to generate a listing of data. It\'s to the effect of the following:



        
9条回答
  •  旧巷少年郎
    2021-01-02 18:04

    I don't think what you are doing is going to work I get an error when I try and set the DataSource as you are trying to do; however, in the code behind you do this:

    Assuming you added a listener to your parent repeater's ItemDataBoundEvent, then you will need to change your linq query slightly to not use an anonymous type (Create a protected class that has your properties) In mjy case I am using dto as the class name.

    void rep1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
    
        Repeater rep2 = (Repeater)e.Item.FindControl("rep2");
        rep2.DataSource = ((dto)e.Item.DataItem).y;
        rep2.DataBind();
    }
    

    I'd love to learn why you think you can't solve this in the code behind.

提交回复
热议问题