English 中文(简体)
创建一个富重复器,数据绑定会清除自定义添加的控件。 创造一个富重复器,数据绑定会清除自定义添加的控件。
原标题:
  • 时间:2008-12-02 17:14:00
  •  标签:

所以...我有一个聪明的想法,我会创建自己的 Repeater 控件,通过继承并扩展它的功能来实现分页和排序。我找到了一些信息和片段,如何去做这件事,一切都看起来不错...

我创建了一个WebControlLibrary来存放我的自定义控件。除了丰富的重复器之外,我还创建了一个组合控件,它将作为“分页栏”,具有前进、后退和页面选择功能。我的分页栏在其自身上完美地工作,当用户与之交互时适当地触发了分页更改事件。富重复器绑定数据没有问题,但当数据绑定触发时(当我调用基类的databind()时),控件集合被清除,我的分页栏都被移除了。这破坏了分页栏的视图状态,使它们无法适当地触发事件或维护其状态。

我已经尝试在base.databind()触发后重新将控件添加到集合中,但这并不能解决问题。我开始得到非常奇怪的结果,包括更改控件树层次结构的问题(通过添加[ViewStateModeById]解决)。

在我重新设计并创建第二个复合控件之前,其中包含一个重复器和分页栏(这样重复器就不需要负责分页栏的视图状态),有没有什么想法来解决这个问题?

为了利益共享,中继器本身的代码如下,呼叫器栏不如问题确实是维护任何其他子控件的状态。(原谅一些代码的粗糙...它仍然是一个正在进行中的工作)

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

[ViewStateModeById]
public class SortablePagedRepeater : Repeater, INamingContainer {

    private SuperRepeaterPagerBar topBar = new SuperRepeaterPagerBar();
    private SuperRepeaterPagerBar btmBar = new SuperRepeaterPagerBar();

    protected override void OnInit(EventArgs e) {
        Page.RegisterRequiresControlState(this);

        InitializeControls();
        base.OnInit(e);
        EnsureChildControls();
    }

    protected void InitializeControls() {
        topBar.ID = this.ID + "__topPagerBar";
        topBar.NumberOfPages = this._currentProperties.numOfPages;
        topBar.CurrentPage = this.CurrentPageNumber;
        topBar.PageChanged +=
            new SuperRepeaterPagerBar.PageChangedEventHandler(PageChanged);

        btmBar.ID = this.ID + "__btmPagerBar";
        btmBar.NumberOfPages = this._currentProperties.numOfPages;
        btmBar.CurrentPage = this.CurrentPageNumber;
        btmBar.PageChanged +=
            new SuperRepeaterPagerBar.PageChangedEventHandler(PageChanged);
    }

    protected override void CreateChildControls() {
        EnsureDataBound();

        this.Controls.Add(topBar);
        this.Controls.Add(btmBar);
        //base.CreateChildControls();
    }

    private void PageChanged(object sender, int newPage) {
        this.CurrentPageNumber = newPage;
    }

    public override void DataBind() {
        //pageDataSource();

        //DataBind removes all controls from control collection...
        base.DataBind();
        Controls.Add(topBar);
        Controls.Add(btmBar);
    }

    private void pageDataSource() {
        //Create paged data source
        PagedDataSource pds = new PagedDataSource();

        pds.PageSize = this.ItemsPerPage;
        pds.AllowPaging = true;

        // first get a PagedDataSource going and perform sort if possible...
        if (base.DataSource is System.Collections.IEnumerable) {
            pds.DataSource = (System.Collections.IEnumerable)base.DataSource;
        } else if (base.DataSource is System.Data.DataView) {
            DataView data = (DataView)DataSource;
            if (this.SortBy != null && data.Table.Columns.Contains(this.SortBy)) {
                data.Sort = this.SortBy;
            }
            pds.DataSource = data.Table.Rows;
        } else if (base.DataSource is System.Data.DataTable) {
            DataTable data = (DataTable)DataSource;
            if (this.SortBy != null && data.Columns.Contains(this.SortBy)) {
                data.DefaultView.Sort = this.SortBy;
            }
            pds.DataSource = data.DefaultView;
        } else if (base.DataSource is System.Data.DataSet) {
            DataSet data = (DataSet)DataSource;
            if (base.DataMember != null && data.Tables.Contains(base.DataMember)) {
                if (this.SortBy != null && data.Tables[base.DataMember].Columns.Contains(this.SortBy)) {
                    data.Tables[base.DataMember].DefaultView.Sort = this.SortBy;
                }
                pds.DataSource = data.Tables[base.DataMember].DefaultView;
            } else if (data.Tables.Count > 0) {
                if (this.SortBy != null && data.Tables[0].Columns.Contains(this.SortBy)) {
                    data.Tables[0].DefaultView.Sort = this.SortBy;
                }
                pds.DataSource = data.Tables[0].DefaultView;
            } else {
                throw new Exception("DataSet doesn t have any tables.");
            }
        } else if (base.DataSource == null) {
            // don t do anything?
        } else {
            throw new Exception("DataSource must be of type System.Collections.IEnumerable.  The DataSource you provided is of type " + base.DataSource.GetType().ToString());
        }


        if (pds != null && base.DataSource != null) {
            //Make sure that the page doesn t exceed the maximum number of pages 
            //available
            if (this.CurrentPageNumber >= pds.PageCount) {
                this.CurrentPageNumber = pds.PageCount - 1;
            }

            //Set up paging values...
            btmBar.CurrentPage = topBar.CurrentPage = pds.CurrentPageIndex = this.CurrentPageNumber;
            this._currentProperties.numOfPages = btmBar.NumberOfPages = topBar.NumberOfPages = pds.PageCount;

            base.DataSource = pds;
        }
    }

    public override object DataSource {
        get {
            return base.DataSource;
        }
        set {
            //init();  //reset paging/sorting values since we ve potentially changed data sources.
            base.DataSource = value;
            pageDataSource();
        }
    }

    protected override void Render(HtmlTextWriter writer) {
        topBar.RenderControl(writer);
        base.Render(writer);
        btmBar.RenderControl(writer);
    }

    [Serializable]
    protected struct CurrentProperties {
        public int pageNum;
        public int itemsPerPage;
        public int numOfPages;
        public string sortBy;
        public bool sortDir;
    }

    protected CurrentProperties _currentProperties = new CurrentProperties();

    protected override object SaveControlState() {
        return this._currentProperties;
    }

    protected override void LoadControlState(object savedState) {
        this._currentProperties = (CurrentProperties)savedState;
    }

    [Category("Status")]
    [Browsable(true)]
    [NotifyParentProperty(true)]
    [DefaultValue("")]
    [Localizable(false)]
    public string SortBy {
        get { return this._currentProperties.sortBy; }
        set {
            //If sorting by the same column, swap the sort direction.
            if (this._currentProperties.sortBy == value) {
                this.SortAscending = !this.SortAscending;
            } else {
                this.SortAscending = true;
            }
            this._currentProperties.sortBy = value;
        }
    }

    [Category("Status")]
    [Browsable(true)]
    [NotifyParentProperty(true)]
    [DefaultValue(true)]
    [Localizable(false)]
    public bool SortAscending {
        get { return this._currentProperties.sortDir; }
        set { this._currentProperties.sortDir = value; }
    }

    [Category("Status")]
    [Browsable(true)]
    [NotifyParentProperty(true)]
    [DefaultValue(25)]
    [Localizable(false)]
    public int ItemsPerPage {
        get { return this._currentProperties.itemsPerPage; }
        set { this._currentProperties.itemsPerPage = value; }
    }

    [Category("Status")]
    [Browsable(true)]
    [NotifyParentProperty(true)]
    [DefaultValue(1)]
    [Localizable(false)]
    public int CurrentPageNumber {
        get { return this._currentProperties.pageNum; }
        set { 
            this._currentProperties.pageNum = value;
            pageDataSource();
        }
    }
}
问题回答

You could also use the new ASP.NET ListView control (since .Net 3.5) It has paging/sorting databinding and templating, which means you can use it as a repeater or very easily do complex stuff like grid like displays with inline editing.





相关问题
热门标签