How To Create A Nested LinkButtons Inside A Repeater?

前端 未结 2 662
死守一世寂寞
死守一世寂寞 2021-01-24 14:04

I need to create a nested linkbuttons in a asp.net page that looks like a treeview, but all are linkbuttons. Example is shown below:

ParentLinkButton1
    ChildL         


        
2条回答
  •  别那么骄傲
    2021-01-24 14:17

    I would recommend doing a repeater inside of a repeater.

    
        " />
        
            
                
                    " />
                
            
        
    
    

    And then in the ItemDataBound events you can set the data source for the child repeater and bind it.

    Feel free to ask for any clarifications. i don't want to write the whole thing for you.

    EDIT:

    To bind the parent, you would want to use Page Load or some other page event to add the datasource to the outer repeater and then bind it.

    protected override void OnLoad(EventArgs e)
            {
                base.OnLoad(e);
    
                rptParentLinkButtons.DataSource = myParentItemCollection;
                rptParentLinkButtons.DataBind();
            }
    

    And then you have 2 options, wither the way I showed it in the asp above by accessing your databound object using the eval, or by using the parent's ItemDataBound event.

    void rptParentLinkButtons_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
    
              // This event is raised for the header, the footer, separators, and items.
    
              Repeater childRepeater = (Repeater)e.Item.FindControl("rptChildLinkButtons");
              // Set the source of the child equal to a collection on the parent object for it to make the child links.
              childRepeater.DataSource = myParentItemCollection[e.Item.ItemIndex].childElements;
           }    
    

    The above code is not perfect, but it should get you a good idea on how to get the rest of the way,

    Cheers,

提交回复
热议问题