Dynamically change GridView item template

前端 未结 2 1537
予麋鹿
予麋鹿 2020-12-17 22:54

I have a fairly big asp.net website that use GridView bound to the same object in lots of places. I\'m using an item template to customize each row. However to have the same

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-17 23:22

    In order to accomplish what you want, you have two options as I see it:

    1.) Build each TemplateField dynamically in code, and switch these based on some configuration.
    2.) Create user controls for your custom grids and use those instead.

    I know you said you don't want to use a UserControl because that will take away your ability to dynamically change your layout, but let me challenge that presupposition with an example.

    You can make use of built-in ASP.Net features in order to dynamically switch out user controls to your liking by using a PlaceHolder Control.

    
    

    Your custom grids can be built declaratively in .ascx files and then loaded into place dynamically at runtime like so:

    GridViewPlaceHolder.Controls.Add(LoadControl("~/Controls/MyCustomControl.ascx"));
    

    Now, if you really want to make your life easier, then you can create an abstract base class that all your custom grid controls will inherit from. In this way, your controls can be treated generically when loaded.

    public abstract class CustomGridControl: System.Web.UI.UserControl
    {
        public abstract Object DataSource { get; set; }
    }
    

    A simple grid can be defined in markup:

    
        
            
                
                    
                
            
            
                
                    
                
            
        
    
    

    And your code behind for that control would look something like this:

    public partial class SimpleGrid : CustomGridControl
    {
        public override object DataSource
        {
            get { return myGridView.DataSource; }
            set { myGridView.DataSource = value; }
        }
    }
    

    Now the page or control that utilizes this only has to cast to the base class, and you can use it generically. The following is a simple example of how you might use this, but I think it makes the point clearly:

    protected void Page_Load(object sender, EventArgs e)
    {
        var dataSource = new List
                            {
                                new MyCustomClass{Name = "Josh", Age = 43},
                        new MyCustomClass{Name = "Bob", Age = 14},
                        new MyCustomClass{Name = "Ashley", Age = 32},
                            };
    
        DynamicallyLoadUserControlGrid("~/GridViewTemplates/SimpleGrid.ascx", dataSource);
    }
    
    private void DynamicallyLoadUserControlGrid(String controlLocation, List dataSource)
    {
        var ctrl = (CustomGridControl)LoadControl(controlLocation);
        ctrl.DataSource = dataSource;
        ctrl.DataBind();
    
        GridViewPlaceHolder.Controls.Add(ctrl);
    }
    

    So, there you have it. Custom templated controls without all the nasty headache of trying to build them all up manually in code. I am going to post the completely manual way of doing this in another answer, but once you see it, I think you will agree that this method is preferred.

提交回复
热议问题