We help IT Professionals succeed at work.

custom paging/normal paging on Datalist

leap29
leap29 asked
on
Medium Priority
502 Views
Last Modified: 2013-11-26
Hi,
I have created a datalist for displaying the results of my searches for a new job board site i am creating.
I have all results returning into  a Datalist. Now i understand that a datalist does not have the inbuilt ability to add paging onto the list.
Please could someone guide me though how i would go about adding paging onto my datalist.

Many thanks
<asp:DataList ID="DataList1" runat="server" BackColor="White" 
            BorderColor="#336666" BorderStyle="Double" BorderWidth="3px" CellPadding="4" 
            GridLines="Horizontal" RepeatLayout="Flow" Width="600px" 
            onselectedindexchanged="DataList1_SelectedIndexChanged" 
            UseAccessibleHeader="True" DataKeyField="jobid" Font-Bold="False" Font-Italic="False" 
                                Font-Names="Arial" Font-Overline="False" Font-Size="Smaller" 
                                Font-Strikeout="False" Font-Underline="False" 
            style="text-align: center">
            <FooterStyle BackColor="White" ForeColor="#333333" />
            <ItemTemplate>
              
              
                <br />  <b>Job Title</b>:
                <asp:Label ID="jobtitleLabel" runat="server" Text='<%# Bind("jobtitle") %>' />
                <b>Job Location</b>:
                <asp:Label ID="Label1" runat="server" 
                            Text='<%# Bind("location") %>' />
                <br />
                                <br />
                                
                <b>Job Description</b>:<br />
                <asp:Label ID="publishedjobdescriptionLabel" runat="server" 
                            Text='<%# Bind("description") %>' />
               
                   
                   
                    <asp:LinkButton ID="LinkButton1" CommandName="DetailsViewRedirect" runat="server" CommandArgument='<%# Bind("jobid") %>' oncommand="LinkButton1_Command">more</asp:LinkButton>
 
 
                <br />
                <br />
              
                <hr />
            </ItemTemplate>
            <ItemStyle BackColor="White" ForeColor="#333333" />
            <SeparatorStyle Font-Bold="False" Font-Italic="False" Font-Overline="False" 
                Font-Strikeout="False" Font-Underline="False" ForeColor="Aqua" />
            <SelectedItemStyle BackColor="#339966" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#336666" Font-Bold="True" ForeColor="White" />
        </asp:DataList>

Open in new window

Comment
Watch Question

David H.H.LeeDigital Marketing (Hospitality)
CERTIFIED EXPERT

Commented:

Author

Commented:
hi, thanks for the links but none are really relevant,
I am coding in C# and have already built my datalist. have you any other examples or can you help with the code?
 
thanks

Author

Commented:
hi, i have the following code after following another tutorial.
 
when i run the page it just crashes my internet explorer

   
        PagedDataSource objPage = new PagedDataSource();
        try
        {
            DataSet ds = new DataSet();
            objPage.AllowPaging = true;
            objPage.DataSource = objPage;
            objPage.PageSize = 100;
            objPage.CurrentPageIndex = CurrentPage;
            DataList2.DataSource = objPage;
            DataList2.DataKeyField = "Jobid";
            DataList2.DataBind();
        }
        catch (Exception ex)
        {
            throw ex;
        }
       
David H.H.LeeDigital Marketing (Hospitality)
CERTIFIED EXPERT

Commented:
Hi leap29,
>>..have you any other examples or can you help with the code?
It depend how you control the paging features that required. You can refer the articles that posted for the idea how to implement paging functionality inside your application.
>>...when i run the page it just crashes my internet explorer
what is the estimated results that return from your datasource? Lastly, what is the errors details that returned from screen?

Author

Commented:
Hi,
Attached is a sample of my code behind file. I am returning anywhere between 10 - 500 records. I think my problem with using this code is that it expects a single table to be giving it the data.
I am using a stored procedure which fills a datatable and returns the datatable to a datalist.
not sure if what i am doing is 100% correct.
 
thanks for looking

  public int CurrentPage
    {
        get
        {
            // look for current page in ViewState
            object o = this.ViewState["_CurrentPage"];
            if (o == null)
                return 0; // default page index of 0
            else
                return (int)o;
        }
 
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    } 
    
    protected void Button3_Click(object sender, EventArgs e)
    {
        string strConn = "";
        string keyword = TextBox1.Text;
        string location = TextBox2.Text;
        strConn = ConfigurationManager.ConnectionStrings["SQL03"].ConnectionString;
        SqlConnection cnn = new SqlConnection(strConn);
        cnn.Open();
 
        System.Data.SqlClient.SqlCommand objCmd = new System.Data.SqlClient.SqlCommand("sp_leap29_jobdetails_quicksearch", cnn);
 
        SqlDataAdapter da = new SqlDataAdapter(objCmd);
        objCmd.CommandType = System.Data.CommandType.StoredProcedure;
        objCmd.Parameters.Add(new SqlParameter("@keyword", keyword));
        objCmd.Parameters.Add(new SqlParameter("@location", location));
 
        DataTable dt = new DataTable();
        da.Fill(dt);
 
        //DataList2.DataSource = dt;
        //DataList2.DataBind();
        
       
        
        
        
        
        PagedDataSource objPage = new PagedDataSource();
 
        try
        {
            DataSet ds = new DataSet();
 
            objPage.AllowPaging = true;
            objPage.DataSource = ds.Tables["jobs"].DefaultView;
            objPage.PageSize = 8;
 
            objPage.CurrentPageIndex = CurrentPage;
 
            DataList2.DataSource = objPage;
            DataList2.DataKeyField = "jobid";
            DataList2.DataBind();
 
        }
 
        catch (Exception ex)
        {
            throw ex;
        }
       
    }

Open in new window

Digital Marketing (Hospitality)
CERTIFIED EXPERT
Commented:
Hi leap29,
You can refer this page for more details:
http://www.aspdotnetcodes.com/DataList_Dynamic_Paging_PagedDataSource.aspx

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
it was working for the past 24hrs it was me being an idiot and forgetting to tell my datatable to fill..... ooops :)

thanks for your help
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.