Page view counter

DataPager

Last post 02-08-2008 4:17 AM by Samu Zhang - MSFT. 2 replies.

Sort Posts:

  • DataPager

    02-05-2008, 7:54 PM
    • Loading...
    • shapper
    • Joined on 11-28-2004, 9:15 PM
    • Posts 2,523
    • Points 3,486

    Hello,

    Can I make a Data Pager to work with a ListView without using some kind of Data Source like LinqDataSource?

    Thanks,

    Miguel 

  • Re: DataPager

    02-06-2008, 2:56 AM
    • Loading...
    • Marco Buerckel
    • Joined on 01-03-2008, 9:28 AM
    • Germany
    • Posts 143
    • Points 908

    Hi,

    of course, you may bind the databound web controls to any Array or IEnumerable. You just have to declare the binding within your code (e.g. Page_Load event):

    First, asign your data object to ListView.DataSource (do not confuse with DataSourceID) and then call ListView.DataBind()

    Regards
    Marco Buerckel
     

    Don't forget to mark posts that were helpful as answers.
  • Re: DataPager

    02-08-2008, 4:17 AM
    Answer

    Hi shapper ,

    shapper:

    Can I make a Data Pager to work with a ListView without using some kind of Data Source like LinqDataSource?

    Yes, you can.

    See my sample,

                <asp:ListView ID="lvItems" runat="server" 
            ItemPlaceholderID="layoutTemplate"
            onpagepropertieschanged="lvItems_PagePropertiesChanged" onpagepropertieschanging="lvItems_PagePropertiesChanging"
                    >
                    <LayoutTemplate>
                        <div id="layoutTemplate" runat="server" />
                    </LayoutTemplate>
                    <ItemTemplate>
                        <div class="itemdisplay">
                            <b>
                                <asp:Label ID="lblid" runat="server" Text='<%# Eval("RegionID") %>'></asp:Label></b><br />
                            <asp:Label ID="lblreg" runat="server" Text='<%# Eval("RegionDescription")%>'></asp:Label></div>
                    </ItemTemplate>
                </asp:ListView>
          

           
           
           
                    <p>
                        <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvItems" PageSize="3">
                            <Fields>
                                <asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True"
                                    ShowLastPageButton="True" />
                            </Fields>
                        </asp:DataPager>
                    </p>

     

            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }

            private void bind()
            {
                SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\NORTHWND.MDF;Integrated Security=True;User Instance=True");
                DataSet ds = new DataSet();
                SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM [Region]", con);
                sda.Fill(ds);
                this.lvItems.DataSource = ds.Tables[0];
                lvItems.DataBind();
            }

     

            protected void lvItems_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
            {
                this.DataPager1.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
                bind();
            }

            protected void lvItems_PagePropertiesChanged(object sender, EventArgs e)
            {
               
            }

     


     


    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (3 items)