javascriptasp.netpaginationrepeater

Highlighting Page Number Using Repeater


I have created one program in which i have used repeater control. Now in repeater control i have also used paging.When i click on the page number that number is highlighted with other color . And it works fine. Now my problem is i am also having View all button. When i click on that button . I want to change the color of view all only. Not number. And whatever i am doing is when i click on view all button both Page number 1 and Vie wall are highlighted. Can u please give me the suggestions? I have took one link button for view all and a repeater which is having link button for paging. Now what should i do for that?? Should i use Javascript for that? Or it will be done by back end coding? aspx file code

 <div style="overflow: hidden;" class="inner_bold_txt">
  <asp:LinkButton ID="lnkviewall" runat="server" Text="View All" 
          onclick="lnkviewall_Click"></asp:LinkButton> |
          <asp:LinkButton ID="lnkviewpaging" runat="server" Text="Back to Paging" 
          onclick="lnkviewpaging_Click" Visible="false"></asp:LinkButton> 
    <asp:Repeater ID="rptPaging" runat="server" OnItemDataBound="rptPaging_ItemDataBound" onitemcommand="rptPaging_ItemCommand">

        <ItemTemplate>
         <asp:LinkButton ID="btnPage" style="padding:8px; margin:2px; background:#ffa100; border:solid 1px #666; font:8pt tahoma;"
            CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" OnClick="btnPage_Click" ForeColor="White" Font-Bold="True" >
            <%# Container.DataItem %>
          </asp:LinkButton>
       </ItemTemplate>
    </asp:Repeater>
    </div>

aspx.cs file

 protected void BindRepeter()
 {
        DataTable dt = new DataTable();
        dt = (DataTable)Session["news"];

        PagedDataSource pds = new PagedDataSource();
        DataView dv = new DataView(dt);
        pds.DataSource = dv;
            pds.AllowPaging = true;
            pds.PageSize = Convert.ToInt32(drpItems.SelectedValue.ToString());

            pds.CurrentPageIndex = PageNumber;
            if (PageNumber == 1110)
            {

               // rptPaging.DataBind();
                repeator.DataSource = dt;
                repeator.DataBind();
            }
            else{
        if (pds.PageCount > 1)
        {
            rptPaging.Visible = true;
            ArrayList pages = new ArrayList();
            pages.Add("View all".ToString());
            for (int i = 0; i < pds.PageCount; i++)
                pages.Add((i + 1).ToString());
            rptPaging.DataSource = pages;
            rptPaging.DataBind();
        }
        else
        {
            rptPaging.Visible = false;
        }

        repeator.DataSource = pds;
        repeator.DataBind();
            }
        Session["news"] = dt;
//}
}
public int PageNumber
{
    get
    {
        if (ViewState["PageNumber"] != null)
            return Convert.ToInt32(ViewState["PageNumber"]);
        else
            return 0;
    }
    set
    {
        ViewState["PageNumber"] = value;
    }
}
protected void rptPaging_ItemDataBound(object source, RepeaterItemEventArgs e)
{

    LinkButton lnk = (LinkButton)e.Item.FindControl("btnPage");
//    if (ViewState["viewall"] != "1")

        if (lnk.CommandArgument.ToString() == (PageNumber + 1).ToString())
        {

            lnk.ForeColor = System.Drawing.Color.Black;

        }

        else
        {
            //if (lnk.CommandArgument.ToString() == "View all")
            //{
            //    lnk.ForeColor = System.Drawing.Color.Black;
            //}
            //else
            {
                lnk.ForeColor = System.Drawing.Color.White;
            }
        }
}
protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{


    PageNumber = Convert.ToInt32(e.CommandArgument.ToString().Replace("View all","1111")) - 1;
    //if (PageNumber == 1110)
    //{
    //    ViewState["viewall"] = "1";

    //}
    //else
    //{
    //    ViewState["viewall"] = null;
    //}
    //((LinkButton)e.CommandSource).ApplyStyle(mystyle);
    //((LinkButton)e.CommandSource).MergeStyle(mystyle);
    //((LinkButton)e.CommandSource).ForeColor=System.Drawing.Color.MediumVioletRed;

    BindRepeter();



}


protected void lnkreadmore_Click(object sender, EventArgs e)
{
    LinkButton btn = (sender as LinkButton);
    DataTable dt = new DataTable();
    if (Session["news"] != null)
    {
        Session["news"] = null;
    }
    dt.Columns.Add("news_id", typeof(int));
    DataRow dr;
    dr = dt.NewRow();
    dr["news_id"] = btn.CommandArgument;
    dt.Rows.Add(dr);
    Session["news"] = dt;
    Response.Redirect("Default2.aspx");


}
protected void lblSort_OnSelectedIndexChanged(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
    con.Open();
    string sql;

    if(drpSort.SelectedIndex==1)
    {
        sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) order by news_subject asc";
    }
    else if (drpSort.SelectedIndex == 2)
    {
        sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103)  order by news_subject desc";
    }
    else if (drpSort.SelectedIndex == 3)
    {
        sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) ORDER BY CONVERT(DateTime, news_start_date,101) asc";
    }
    else if (drpSort.SelectedIndex == 4)
    {
        sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) ORDER BY CONVERT(DateTime, news_start_date,101) DESC";
    }
    else
    {
        sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) order by news_position_id";
    }
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    sda.Fill(dt);
    Session["news"] = dt;  
    BindRepeter();
    con.Close();
}
protected void drpItems_SelectedIndexChanged(object sender, EventArgs e)
{
    BindRepeter();
}

Solution

  • I got the solution ....

     protected void BindRepeter()
     {
            DataTable dt = new DataTable();
            dt = (DataTable)Session["news"];
    
            PagedDataSource pds = new PagedDataSource();
            DataView dv = new DataView(dt);
            pds.DataSource = dv;
                pds.AllowPaging = true;
                pds.PageSize = Convert.ToInt32(drpItems.SelectedValue.ToString());
    
                pds.CurrentPageIndex = PageNumber;
                if (PageNumber == 1110)
                {
                    repeator.DataSource = dt;
                    repeator.DataBind();
                    rptPaging.Visible = true;
                    ArrayList pages = new ArrayList();
                    pages.Add("View all".ToString());
                    for (int i = 0; i < pds.PageCount; i++)
                        pages.Add((i + 1).ToString());
                    rptPaging.DataSource = pages;
                    rptPaging.DataBind();
                }
                else
                {
                    if (pds.PageCount > 1)
                     {
                        rptPaging.Visible = true;
                        ArrayList pages = new ArrayList();
                        pages.Add("View all".ToString());
                        for (int i = 0; i < pds.PageCount; i++)
                            pages.Add((i + 1).ToString());
                        rptPaging.DataSource = pages;
                        rptPaging.DataBind();
                    }
                    else
                    {
                        rptPaging.Visible = false;
                     }
                     repeator.DataSource = pds;
                    repeator.DataBind();
            }
            Session["news"] = dt;
    //}
    }
    public int PageNumber
    {
        get
        {
            if (ViewState["PageNumber"] != null)
                return Convert.ToInt32(ViewState["PageNumber"]);
            else
                return 0;
        }
        set
        {
            ViewState["PageNumber"] = value;
        }
    }
    protected void rptPaging_ItemDataBound(object source, RepeaterItemEventArgs e)
    {
        LinkButton lnk = (LinkButton)e.Item.FindControl("btnPage");
    
        if (viewall_ == false)
        {
    
            //    if (ViewState["viewall"] != "1")
    
            if (lnk.CommandArgument.ToString() == (PageNumber + 1).ToString())
            {
    
                lnk.ForeColor = System.Drawing.Color.Black;
    
            }
    
            else
            {
                {
                    lnk.ForeColor = System.Drawing.Color.White;
                }
            }
        }
        else
        {
            if (lnk.CommandArgument.ToString() == "View all".ToString())
            {
                lnk.ForeColor = System.Drawing.Color.Black;
            } 
        }
    }
    protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
    
    
        PageNumber = Convert.ToInt32(e.CommandArgument.ToString().Replace("View all","1111")) - 1;
        if (PageNumber == 1110)
        {
            viewall_ = true;
        }
        BindRepeter();
    
    
    
    }
    
    
    protected void lnkreadmore_Click(object sender, EventArgs e)
    {
        LinkButton btn = (sender as LinkButton);
        DataTable dt = new DataTable();
        if (Session["news"] != null)
        {
            Session["news"] = null;
        }
        dt.Columns.Add("news_id", typeof(int));
        DataRow dr;
        dr = dt.NewRow();
        dr["news_id"] = btn.CommandArgument;
        dt.Rows.Add(dr);
        Session["news"] = dt;
        Response.Redirect("Default2.aspx");
    
    
    }
    protected void lblSort_OnSelectedIndexChanged(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        con.Open();
        string sql;
    
        if(drpSort.SelectedIndex==1)
        {
            sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) order by news_subject asc";
        }
        else if (drpSort.SelectedIndex == 2)
        {
            sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103)  order by news_subject desc";
        }
        else if (drpSort.SelectedIndex == 3)
        {
            sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) ORDER BY CONVERT(DateTime, news_start_date,101) asc";
        }
        else if (drpSort.SelectedIndex == 4)
        {
            sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) ORDER BY CONVERT(DateTime, news_start_date,101) DESC";
        }
        else
        {
            sql = "SELECT news_id,DATENAME(MONTH, news_start_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_start_date), 2) + ', ' + DATENAME(YEAR, news_start_date) AS news_start_date,DATENAME(MONTH, news_end_date)+ ' ' + RIGHT('0' + DATENAME(DAY, news_end_date), 2) + ', ' + DATENAME(YEAR, news_end_date) AS news_end_date,news_subject,news_short_desc,news_position_id,news  from dbo.news where convert(nvarchar(15),news_start_date,103) >= convert(nvarchar(15),getdate(),103) and convert(nvarchar(15),news_end_date,103) >= convert(nvarchar(15),getdate(),103) order by news_position_id";
        }
        SqlCommand cmd = new SqlCommand(sql, con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        sda.Fill(dt);
        Session["news"] = dt;  
        BindRepeter();
        con.Close();
    }
    protected void drpItems_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindRepeter();
    }