Click here to Skip to main content
14,934,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to do paging in repeater control in asp.net. I can do custom paging with repeater with ado.net. But with linq i never did it. I try it but its not work Here is the code for binding my repeater with xml using linq


C#
var doc = XDocument.Load(Server.MapPath("~/Data/BlogContent.xml"));
        var result = doc.Descendants("post").Where(x => x.Element("id") != null).Select(x => new
        {
            id = x.Element("id").Value,
            title = x.Element("title").Value,
            Description = x.Element("Discription").Value,
            dt = x.Element("dt").Value,
            mnt = x.Element("mnt").Value,
            yr = x.Element("yr").Value,
            postdate=x.Element("PostDate").Value
        }).OrderByDescending(x => x.id).Take(5);


        Repeater1.DataSource = result;
        Repeater1.DataBind();


I am doing pagging like this here but its changing the previous next button no effect on data

C#
public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection();
    public int PgNum
    {
        get
        {
            if (ViewState["pgNum"] != null)
                return Convert.ToInt32(ViewState["pgNum"]);
            else
                return 0;
        }
        set
        {
            ViewState["pgNum"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        
       BindRep1();
       // Paging();
        BindCureentMonth();
        if (!IsPostBack)
        {
            DropDownList1.SelectedValue = DateTime.Now.Month.ToString();
            
        }

       
        
    }
    private void Paging()
    {
        int cnt;
        String File = Server.MapPath("~/Data/BlogContent.xml");
        DataSet ds = new DataSet();
        ds.ReadXml(File);
        cnt = ds.Tables[0].Rows.Count;
        PagedDataSource pagedata = new PagedDataSource();
        pagedata.DataSource = ds.Tables[0].DefaultView;
        pagedata.AllowPaging = true;
        pagedata.PageSize = 5;
        pagedata.CurrentPageIndex = PgNum;
        int vcnt = cnt / pagedata.PageSize;
        if (PgNum < 1)
            LinkButton1.Visible = false;
        else if(PgNum>0)
            LinkButton1.Visible=true;
        if (PgNum == vcnt)
            LinkButton2.Visible = false;
        else if (PgNum < vcnt)
            LinkButton2.Visible = true;
        Repeater1.DataSource = pagedata;
        Repeater1.DataBind();
           

    }

     
    private void BindRep()
    {
        String File = Server.MapPath("~/Data/BlogContent.xml");
        DataSet ds = new DataSet();
        ds.ReadXml(File);
        DataView dv = new DataView(ds.Tables[0]);
        DataTable dt = dv.Table;
        dv.Sort = dt.Columns["id"].ColumnName;
        dv.Sort += " Desc";
        Repeater1.DataSource = dv;
        Repeater1.DataBind();


    }
    void BindRep1()
    {
        int cnt1;
        var doc = XDocument.Load(Server.MapPath("~/Data/BlogContent.xml"));
        var result = doc.Descendants("post").Where(x => x.Element("id") != null).Skip(5).Take(5).Select(x => new
        {
            id = x.Element("id").Value,
            title = x.Element("title").Value,
            Description = x.Element("Discription").Value,
            dt = x.Element("dt").Value,
            mnt = x.Element("mnt").Value,
            yr = x.Element("yr").Value,
            postdate=x.Element("PostDate").Value
        }).OrderByDescending(x => x.id);
        cnt1=result.Count();
        PagedDataSource datasource = new PagedDataSource();
        datasource.AllowPaging = true;
        datasource.AllowCustomPaging = true;
        datasource.DataSource = result;
        datasource.PageSize = 2;
        datasource.CurrentPageIndex = PgNum;
        int vcnt = cnt1/ datasource.PageSize;
        if (PgNum < 1)
            LinkButton1.Visible = false;
        else if (PgNum > 0)
            LinkButton1.Visible = true;
        if (PgNum == vcnt)
            LinkButton2.Visible = false;
        else if (PgNum < vcnt)
            LinkButton2.Visible = true;
        Repeater1.DataSource = datasource;
        Repeater1.DataBind();
     
    }

   public int  CountComment(string xid)
    {
     
        var doc = XDocument.Load(Server.MapPath("~/Data/BlogContent.xml"));
        var result = doc.Descendants("post")
                      .Where(x => x.Element("id").Value == xid).Count();
        int count = Convert.ToInt32(result);
        return count;
    }


    public static DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
    {

        DataTable dt = new DataTable();

        int TempColumn = 0;



        foreach (XmlNode node in xnl.Item(0).ChildNodes)
        {

            TempColumn++;

            DataColumn dc = new DataColumn(node.Name, System.Type.GetType("System.String"));

            if (dt.Columns.Contains(node.Name))
            {

                dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());

            }

            else
            {

                dt.Columns.Add(dc);

            }

        }

        int ColumnsCount = dt.Columns.Count;
        for (int i = 0; i < xnl.Count; i++)
        {

            DataRow dr = dt.NewRow();

            for (int j = 0; j < ColumnsCount; j++)
            {

                dr[j] = xnl.Item(i).ChildNodes[j].InnerText;

            }

            dt.Rows.Add(dr);

        }

        return dt;

    }
    protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
    }
    protected bool SetVisibility(object Desc, int length)
    {
        return Desc.ToString().Length > length;
    }

    protected void ReadMoreLinkButton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/PostComment.aspx?id={0}");

            
    }
  
    protected string Limit(object Desc, int length)
{
    StringBuilder strDesc = new StringBuilder();
    strDesc.Insert(0, Desc.ToString());

    if (strDesc.Length > length)
    {
        return strDesc.ToString().Substring(0, length) + "..." ;
    }
    else
    {
        
        return strDesc.ToString();
    }
}
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int month = DateTime.Now.Month;
        var doc = XDocument.Load(Server.MapPath("~/Data/BlogContent.xml"));
        var result = doc.Descendants("post").Where(x => x.Element("mnt").Value ==DropDownList1.SelectedValue && x.Element("yr").Value==DateTime.Now.Year.ToString()).Select(x => new
        {
            id = x.Element("id").Value,
            title = x.Element("title").Value,
            
            
        }).OrderByDescending(x => x.id);
        Repeater2.DataSource = result;
        Repeater2.DataBind();
      

    }
    private void BindCureentMonth()
    {
        int month = DateTime.Now.Month;
        var doc = XDocument.Load(Server.MapPath("~/Data/BlogContent.xml"));
        var result = doc.Descendants("post").Where(x => x.Element("mnt").Value == month.ToString()).Select(x => new
        {
            id = x.Element("id").Value,
            title = x.Element("title").Value
            //postdate = x.Element("PostDate").Value

        }).OrderByDescending(x => x.id);
        Repeater2.DataSource = result;
        Repeater2.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        //string id = string.Empty;
        //Label lblid = (Label)(e.Item.FindControl("Label1"));
        //Label lblcount = (Label)(e.Item.FindControl("Label4"));

        //id = lblid.Text;
        //var doc = XDocument.Load(Server.MapPath("~/Data/comments.xml"));
        //var result = doc.Descendants("comments")
        //              .Where(x => x.Element("postid").Value == id).Count();
        //lblcount.Text = result.ToString() ;
         
    }
    protected void Repeater1_ItemDataBound1(object sender, RepeaterItemEventArgs e)
    {
        string id = string.Empty;
        Label lblid = (Label)(e.Item.FindControl("Label1"));
        Label lblcount = (Label)(e.Item.FindControl("Label4"));

        id = lblid.Text;
        var doc = XDocument.Load(Server.MapPath("~/Data/comments.xml"));
        var result = doc.Descendants("comments")
            .Where(x => x.Element("postid").Value == id && x.Element("status").Value=="Y").Count();
        lblcount.Text = result.ToString();
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        PgNum -= 1;
        BindRep1();
    }
    protected void LinkButton2_Click(object sender, EventArgs e)
    {
        PgNum += 1;
        BindRep1();

    }
}


Please tell me where i am wrong
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900