自定义DataPager的问题

自定义DataPager的问题

本文介绍了自定义DataPager的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在asp.net DataPager的控制延伸到与predefined模板的自定义控制。这是所需的输出

I am trying to extend the asp.net DataPager control into a custom control with predefined templates. This is the desired output

问题


  1. 当我加载包含自定义DataPager的首次页面时, SelectMethod 我的 ObjectDataSource控件被称为3次。

  2. 当我尝试加载使用DataPager的数据的另一个页面时,SelectMethod被调用两次。

  3. 当我尝试,通过使用下一个/ previous按钮或DropDownList的加载数据的另外一个页面,页面不会改变。我跑了一些调试,发现它并没有传递正确的值到SelectMethod的StartRowIndexParameter(它刚过0每次它调用的方法)。

  1. When I load the page containing the custom DataPager for the first time, the SelectMethod of my ObjectDataSource is called 3 times.
  2. When I try to load another page of data using the DataPager, the SelectMethod is called twice.
  3. When I try to load another page of data, either by using the Next/Previous buttons or the DropDownList, the page does not change. I ran some debugging and found that it was not passing the correct value to the StartRowIndexParameter of the SelectMethod (it just passed 0 everytime it called the method).

这里的code为我的自定义控件。

Here's the code for my custom control.

public class DataPagerDDL : DataPager
{
    protected override void RenderContents(HtmlTextWriter writer)
    {
        //add custom template
        TemplatePagerField templateField = new TemplatePagerField();
        templateField.PagerTemplate = new CustomTemplate();
        Fields.Add(templateField);

        //add previous/next page template
        NextPreviousPagerField nextPreviousField = new NextPreviousPagerField();
        nextPreviousField.ShowFirstPageButton = false;
        nextPreviousField.ShowLastPageButton = false;
        nextPreviousField.PreviousPageText = "<<";
        nextPreviousField.NextPageText = ">>";
        Fields.Add(nextPreviousField);

        base.RenderContents(writer);
    }

    public void cmbPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList cmbPage = (DropDownList)sender;
        this.SetPageProperties(cmbPage.SelectedIndex * MaximumRows, MaximumRows, true);
    }
}

public class CustomTemplate : ITemplate
{
    /// <summary>
    /// Insert an instance of text and controls into the specified container.
    /// </summary>
    public void InstantiateIn(Control container)
    {
        DataPagerFieldItem caller = (DataPagerFieldItem)container;
        DataPagerDDL pager = (DataPagerDDL)caller.Parent;
        int totalPages = pager.TotalRowCount / pager.MaximumRows;
        if (pager.TotalRowCount % pager.MaximumRows > 0) totalPages += 1;
        int currentPage = (pager.StartRowIndex / pager.MaximumRows) + 1;

        DropDownList cmbPage = new DropDownList();
        cmbPage.ID = "cmbPage";
        cmbPage.AutoPostBack = true;
        cmbPage.SelectedIndexChanged += new EventHandler(pager.cmbPage_SelectedIndexChanged);
        for (int i = 1; i <= totalPages; i++)
        {
            ListItem item = new ListItem(i.ToString(), i.ToString());
            if (i == currentPage) item.Selected = true;
            cmbPage.Items.Add(item);
        }

        pager.Controls.Add(new LiteralControl("Page "));
        pager.Controls.Add(cmbPage);
        pager.Controls.Add(new LiteralControl(" of " + totalPages.ToString() + " pages | "));
    }
}

这是我的网页看起来是这样的:

And this is what my page looks like:

<asp:ListView ID="ListView1" DataSourceID="ods1" ... >
...
</asp:ListView>

<custom:DataPagerDDL ID="CustomDataPager" runat="server" PagedControlID="ListView1"
        PageSize="25">
</custom:DataPagerDDL>

<asp:ObjectDataSource ID="ods1" ... >
</asp:ObjectDataSource>

我应该怎么做才能让我的自定义DataPager的工作如预期?提前致谢! :)

What should I do to make my custom DataPager work as intended? Thanks in advance! :)

推荐答案

我怀疑你正在创建的寻呼机领域在页面生命周期为时已晚。尝试从 DataPagerDDL 类的初始化事件创建它们。

I suspect you're creating the pager fields too late in the page lifecycle. Try creating them from the Init event of the DataPagerDDL class.

另外,你的 CustomTemplate 应添加控件到容器,而不是寻呼机

Also, your CustomTemplate should be adding the controls to the container, not the pager.

public class DataPagerDDL : DataPager
{
    protected override void OnInit(EventArgs e)
    {
        CreateDefaultPagerFields();
        base.OnInit(e);
    }

    protected virtual void CreateDefaultPagerFields()
    {
        //add custom template
        TemplatePagerField templateField = new TemplatePagerField();
        templateField.PagerTemplate = new CustomTemplate();
        Fields.Add(templateField);

        //add previous/next page template
        NextPreviousPagerField nextPreviousField = new NextPreviousPagerField();
        nextPreviousField.ShowFirstPageButton = false;
        nextPreviousField.ShowLastPageButton = false;
        nextPreviousField.PreviousPageText = "<<";
        nextPreviousField.NextPageText = ">>";
        Fields.Add(nextPreviousField);
    }

    public void cmbPage_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList cmbPage = (DropDownList)sender;
        SetPageProperties(cmbPage.SelectedIndex * MaximumRows, MaximumRows, true);
    }
}

public class CustomTemplate : ITemplate
{
    public void InstantiateIn(Control container)
    {
        DataPagerFieldItem caller = (DataPagerFieldItem)container;
        DataPagerDDL pager = (DataPagerDDL)caller.Parent;
        int totalPages = pager.TotalRowCount / pager.MaximumRows;
        if (pager.TotalRowCount % pager.MaximumRows > 0) totalPages += 1;
        int currentPage = (pager.StartRowIndex / pager.MaximumRows) + 1;

        DropDownList cmbPage = new DropDownList();
        cmbPage.ID = "cmbPage";
        cmbPage.AutoPostBack = true;
        cmbPage.SelectedIndexChanged += pager.cmbPage_SelectedIndexChanged;

        for (int i = 1; i <= totalPages; i++)
        {
            ListItem item = new ListItem(i.ToString(), i.ToString());
            if (i == currentPage) item.Selected = true;
            cmbPage.Items.Add(item);
        }

        container.Controls.Add(new LiteralControl("Page "));
        container.Controls.Add(cmbPage);
        container.Controls.Add(new LiteralControl(" of " + totalPages + " pages | "));
    }
}

这篇关于自定义DataPager的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 10:46