How to delegate telerik grid view common methods to be call from parent page from every child page?

风格不统一 提交于 2019-12-04 23:56:46

May be you can put you common logic inside abstract class with method (or property) that returns reference on concrete GridView and inherit from this class. Then on each page your just have to implement that method.

Something like this:

public abstract class ParentPage
{
    public virtual void DisplayRecords()
    {
        var gridView = this.GetGridView();
        gridView.DataSource = this.GetAllData();
        gridView.DataBind();
    }

    protected abstract  DataTable GetAllData();

    protected string GetSortOrder()
    {
        if (this.sortOrder != GridSortOrder.Assending)
            return string.Format("{0} DESC", this.sortExpression)
        return this.sortExpression;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        DisplayRecords();
    }

    protected void GridView1_SortCommand(object sender, GridSortCommandEventArgs e)
    {
        if (!e.Item.OwnerTableView.SortExpressions.ContainsExpression(e.SortExpression))
        {
            GridSortExpression sortExpr = new GridSortExpression();
            sortExpr.FieldName = e.SortExpression;
            sortExpr.SortOrder = GridSortOrder.Ascending;
            e.Item.OwnerTableView.SortExpressions.AddSortExpression(sortExpr);
        }
    }

    protected void GridView1_PageIndexChanged(object sender, GridPageChangedEventArgs e)
    {
        e.Item.OwnerTableView.PageIndex = e.NewPageIndex;
        DisplayRecords();
    }

    protected void GridView1_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
    {
        e.Item.OwnerTableView.PageSize = e.NewPageSize;
        DisplayRecords();
    }
}

Page 1:**ttt.aspx**
public class **tttPage : BasePage 
{
    protected override GridView GetGridView()
    {
        //return GridView of this page 
        return GridView1;
    }

    protected override  DataTable GetAllData()
    {
        using (var context = new MyDataContext())
        {
            var data = c in context.ttt select c;
            return MyDataContext.LINQToDataTable(data);
        }
    }
}

Page 1:**bbb.aspx**
public class **bbbPage : BasePage
{
    protected override GridView GetGridView()
    {
        //return GridView of this page 
        return GridView1;
    }

    protected override  DataTable GetAllData()
    {
        using (var context = new MyDataContext())
        {
            var data = c in context.bbb select c;
            return MyDataContext.LINQToDataTable(data);
        }
    }
}

Or you can put you common logic inside base class with virtual methods where use event args for getting GridView like e.Item.OwnerTableView.

By making it virtual you will be able to override this logic in any page class

Something like this:

public abstract class ParentPage<TEntity>
{
    public virtual void DisplayRecords(GridView gridView)
    {
        gridView.DataSource = this.GetAllData();
        gridView.DataBind();
    }

    protected abstract  DataTable GetAllData();

    protected void Page_Load(object sender, EventArgs e)
    {
        DisplayRecords(e.Item.OwnerTableView);
    }

    protected void GridView_SortCommand(object sender, GridSortCommandEventArgs e)
    {
        DisplayRecords(e.Item.OwnerTableView);
    }

    protected void GridView_PageIndexChanged(object sender, GridPageChangedEventArgs e)
    {
        DisplayRecords(e.Item.OwnerTableView);
    }

    protected void GridView_PageSizeChanged(object sender, GridPageSizeChangedEventArgs e)
    {
        DisplayRecords(e.Item.OwnerTableView);
    }
}

public class **tttPage : ParentPage 
{
    protected override  DataTable GetAllData()
    {
        using (var context = new MyDataContext())
        {
            var data = c in context.ttt select c;
            return MyDataContext.LINQToDataTable(data);
        }
    }
}

public class **bbbPage : ParentPage 
{
    protected override  DataTable GetAllData()
    {
        using (var context = new MyDataContext())
        {
            var data = c in context.bbb select c;
            return MyDataContext.LINQToDataTable(data);
        }
    }
}

Also you can use generic parameter for getting values from db.

There are many principles that you generally apply when trying to refactor code. Currently you are trying to refactor your code as to not violate the DRY principle (DRY = don't repeat yourself).
But, some other principals might come in to play that you might want to consider. The single responsibility principle would suggest that each method does only one unambiguous thing.

As per your scenario, You need to generate whole GridView dynamically or runtime with custom columns and custom GridView configuration logic which will be more expensive than writing those events in each page.

Also all events must be tied up to 'GridView' to trigger properly. You can not separate those events to other files otherwise events will not fire.

You have another option of using ASP.Net user controls but again you need to write logic to manipulate GridView custom columns and custom GridView configuration logic. So again making it more expensive. So I won't recommend to do so. It's better off as to keep methods individually for each page.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!