如何将分页应用于数据列表控件

时间:2015-02-04 10:00:08

标签: asp.net c#-4.0 asp.net-4.0

我只想用pagging制作数据列表。这是我的代码:

public partial class Template_Management : System.Web.UI.Page
{
    PagedDataSource adsource = null;
    int pos;
    protected void Page_Init(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            adsource = new PagedDataSource();
        }
    }
  protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
          .....
         Session["StartAlpha"] = "All";
            Session["GroupByCategory"] = -1;
            Session["ColumnName"] = null;
            Session["SearchText"] = null;
            this.FillGrid((String)Session["StartAlpha"] ?? null, (int)Session["GroupByCategory"], (String)Session["ColumnName"] ?? null, (String)Session["SearchText"] ?? null);
 this.ViewState["vs"] = 0;
        }
        pos = (int)this.ViewState["vs"];
      }
  }
 public void FillGrid(string StartAlpha, int GroupByCategory, string ColumnName, string SearchText)
    {
       if (myDataSet.Tables[0].Rows.Count > 0)
            {
                adsource.DataSource = myDataSet.Tables[0].DefaultView;
                adsource.AllowPaging = true;
                adsource.PageSize = 20;
                DL_ViewTemplate.DataSource = adsource;
            }
            if (DL_ViewTemplate.Items.Count != 0)
            {
                SetPageNumbers();
            }
            UpdatePanel8.Update();
            DL_ViewTemplate.DataBind();
    }
private void SetPageNumbers()
    {
        foreach (DataListItem item in DL_ViewTemplate.Items)
        {
            if (item.ItemType == ListItemType.Footer)
            {
                if (pos == 0)
                {
                    ImageButton img = (ImageButton)item.FindControl("ImageButton1");
                    img.Enabled = false;
                }
                if (pos == adsource.PageCount - 1)
                {
                    ImageButton img = (ImageButton)item.FindControl("ImageButton4");
                    img.Enabled = false;
                }
            }
        }
    }
 protected void DL_ViewTemplate_ItemCommand(object source, DataListCommandEventArgs e)
    {
      int intCurIndex = adsource.CurrentPageIndex;
        switch (e.CommandArgument.ToString())
        {
            case "first":
                adsource.CurrentPageIndex = 0;
                break;
            case "prev":
                CurrentPage -= 1;
                break;
            case "next":
                CurrentPage += 1;
                break;
            case "last":
                adsource.CurrentPageIndex = adsource.PageCount;
                break;
        }
        this.FillGrid((String)Session["StartAlpha"] ?? null, (int)Session["GroupByCategory"], (String)Session["ColumnName"] ?? null, (String)Session["SearchText"] ?? null);
    }
protected void DL_ViewTemplate_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        foreach (DataListItem item in DL_ViewTemplate.Items)
        {
            if (item.ItemType == ListItemType.Footer && item.ItemType == ListItemType.Item)
            {
                // get your controls from the gridview
                DropDownList ddlPages = (DropDownList)item.FindControl("ddlPages");
                Label lblPageCount = (Label)item.FindControl("lblPageCount");
                if (ddlPages != null)
                {
                    // populate pager
                    for (int i = 0; i < adsource.PageCount; i++)
                    {
                        int intPageNumber = i + 1;
                        ListItem lstItem = new ListItem(intPageNumber.ToString());
                        if (i == adsource.CurrentPageIndex)
                            lstItem.Selected = true;
                        ddlPages.Items.Add(lstItem);
                    }
                }

                // populate page count
                if (lblPageCount != null)
                    lblPageCount.Text = adsource.PageCount.ToString();
            }
        }
    }
    protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (DataListItem item in DL_ViewTemplate.Items)
        {
            if (item.ItemType == ListItemType.Footer)
            {
                DropDownList ddlPages = (DropDownList)item.FindControl("ddlPages");
                adsource.CurrentPageIndex = ddlPages.SelectedIndex;
                //a method to populate your grid
                this.FillGrid((String)Session["StartAlpha"] ?? null, (int)Session["GroupByCategory"], (String)Session["ColumnName"] ?? null, (String)Session["SearchText"] ?? null);
            }
        }
    }
}

如何用ddl控制页脚没有填充。

了解更多信息我放置html标记:

<asp:DataList ID="DL_ViewTemplate" runat="server" RepeatColumns="6" 
ShowFooter="True" HorizontalAlign="Left" RepeatDirection="Horizontal" 
DataKeyField="Id" onitemcommand="DL_ViewTemplate_ItemCommand" 
ShowHeader="False" onitemcreated="DL_ViewTemplate_ItemCreated" 
onitemdatabound="DL_ViewTemplate_ItemDataBound" CellPadding="1" 
CellSpacing="3">
<ItemStyle HorizontalAlign="Left" Wrap="True" Width="40%" />
<ItemTemplate>
<div class="thumb" align="center" style="height:150px;width:130px">
<table width="40%" align="center">
<tr>
<td>
<asp:Literal ID="Literal4" runat="server"></asp:Literal><!-- Text=<'#Eval("TemplateBody")%>'-->
<ajaxToolkit:HoverMenuExtender ID="hme1" runat="Server" 
TargetControlID="Literal4" 
PopupControlID="Panel2" 
DynamicContextKey='<%# Eval("Id") %>'
DynamicControlID="Panel2"
DynamicServiceMethod="GetDynamicContent" 
PopupPosition="Right" 
OffsetX="-25" 
OffsetY="15"/>
</td>
</tr>
</table>
</div>
<table width="145px" align="left" style="border-color:Black;border-style:solid;border-width:1px;height:50px">
<tr>
<td align="center">
<table>
<tr>
<td><asp:CheckBox ID="ChkSelect" runat="server" onclick = "Check_Click(this)"/></td>
<td>&nbsp;&nbsp;</td>
<td><asp:LinkButton ID="LinkButton2" runat="server" CssClass="quicklink" 
Text='<%# Eval("TemplateName").ToString().Length > 12 ? Eval("TemplateName").ToString().Substring(0,12)+"..." :Eval("TemplateName") %>' CommandName="ViewTemplate" 
ToolTip ='<%# Eval("TemplateName")%>' CommandArgument='<%# Eval("Id") %>'></asp:LinkButton>
<br/>
<asp:Label ID="Label2" runat="server" CssClass="normaltext" 
Text='<%# DataBinder.Eval(Container.DataItem, "CreatedDate", "{0:dd/MM/yyyy}") %>' 
ToolTip='<%# Bind("CreatedDate","{0:F}") %>'></asp:Label></td>
</tr>
</table>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>&nbsp;&nbsp;</SeparatorTemplate>
<FooterTemplate>
<table>
<tr>
<td>
<asp:ImageButton ID="ImageButton1" runat="server" 
AlternateText="Go to First Page" CommandArgument="First" CommandName="Page" 
ImageUrl="images/1330128819_resultset_first.png" />
</td>
<td>
<asp:ImageButton ID="ImageButton2" runat="server" AlternateText="Previous Page" 
CommandArgument="Prev" CommandName="Page" 
ImageUrl="images/1330128981_resultset_previous.png" />
</td>
<td>
Page&nbsp;<asp:DropDownList ID="ddlPages" runat="server" AutoPostBack="True" 
onselectedindexchanged="ddlPages_SelectedIndexChanged" Width="50px">
</asp:DropDownList>
of
<asp:Label ID="lblPageCount" runat="server"></asp:Label>
</td>
<td>
<asp:ImageButton ID="ImageButton3" runat="server" AlternateText="Next Page" 
CommandArgument="Next" CommandName="Page" 
ImageUrl="images/Farm-Fresh_resultset_next.png" />
</td>
<td>
<asp:ImageButton ID="ImageButton4" runat="server" 
AlternateText="Go to Last Page" CommandArgument="Last" CommandName="Page" 
ImageUrl="images/1330128876_resultset_last.png" />
</td>
</tr>
</table>
</FooterTemplate>
<FooterStyle CssClass="pager" VerticalAlign="Bottom" 
HorizontalAlign="Center" />
</asp:DataList>

这里我设置adsource,这是首先声明的分页数据源,并在Page_Init()中分配是否有效?因为当其他事件想要访问adsource时,抛出了Object reference not found错误。

我出错了什么,请帮帮我....

1 个答案:

答案 0 :(得分:1)