locked
css class not applying for linkbutton from code behind RRS feed

  • Question

  • User1804579801 posted

    I have repeater as below

    <asp:Repeater ID="rptPager" runat="server">
    <ItemTemplate>
    <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
    Enabled='<%# Eval("Enabled") %>' CssClass="GridPager" OnCommand="Page_Changed"></asp:LinkButton>
    </ItemTemplate>
    </asp:Repeater>.

    I want to change the css from code behind after clicking 

    protected void Page_Changed(object sender, CommandEventArgs e)
    {
    int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
    FillSearchGrid(pageIndex);

    LinkButton lnkPage = (LinkButton)sender;
    if (lnkPage != null)
    {
    lnkPage.CssClass = "aspNetDisabled GridPager";
    }
    }

    does not apply css after clicking on nos  I tried 

    protected void rptPager_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
     LinkButton my_btn = (LinkButton)e.Item.FindControl("lnkPage");
     if (my_btn != null) my_btn.CssClass = "aspNetDisabled GridPager";
    }

    but this is applying css with out clicking on page load i want after page plese suggest 

    Wednesday, April 24, 2019 12:41 PM

All replies

  • User288213138 posted

    Hi  nagapavanich,
     
      I tried to reproduce your problem, but my code works fine, Is your Enabled property set to false?
     
    The code:

    Aspx.cs:
    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Value"), new DataColumn("Text"), new DataColumn("Enabled", typeof(bool)) });
                    dt.Rows.Add(1, "Sam1", true);
                    dt.Rows.Add(2, "Sam2", true);
                    dt.Rows.Add(3, "Sam3", true);
                    dt.Rows.Add(4, "Sam4", true);
                    rptPager.DataSource = dt;
                    rptPager.DataBind();
                }
            }
    
           
    
            protected void Page_Changed(object sender, CommandEventArgs e)
            {
                LinkButton lnkPage = (LinkButton)sender;
                if (lnkPage != null)
                {
                    lnkPage.CssClass = "bb";
                }
    
            }
    Aspx:
    <asp:Repeater ID="rptPager" runat="server">
                    <ItemTemplate>
                        <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>' 
                            CssClass="aa" OnCommand="Page_Changed" Enabled='<%# Eval("Enabled") %>'></asp:LinkButton>
                    </ItemTemplate>
                </asp:Repeater>
    Css:
    .aa {
              font-size:small
            }
            .bb{
               font-size:x-large
            }
    

    The Result:

    Best Regards,

    Sam

    Thursday, April 25, 2019 8:21 AM
  • User1804579801 posted

    Thank you for your replay 

    Iam using the below link custom paging 

    URL:https://www.aspsnippets.com/Articles/ASPNet-GridView-Custom-Paging-with-PageSize-Change-Dropdown.aspx

    I have a gridview with 8000+ records in Fillgrid method iam calling 

    protected void FillSearchGrid(int pageIndex)
    {

    PopulatePager(TotalRecord, 1);

    }

    private void PopulatePager(int recordCount, int currentPage)
    {
    double dblPageCount = (double)((decimal)recordCount / decimal.Parse(ddlPageSize.SelectedValue));
    int pageCount = (int)Math.Ceiling(dblPageCount);
    List<ListItem> pages = new List<ListItem>();
    if (pageCount > 0)
    {
    pages.Add(new ListItem("First", "1", currentPage > 1));
    for (int i = 1; i <= pageCount; i++)
    {
    pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
    }
    pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
    //pages.Attributes.Add("class", "3");
    }
    rptPager.DataSource = pages;
    rptPager.DataBind();
    }
    protected void PageSize_Changed(object sender, EventArgs e)
    {
    //this.GetCustomersPageWise(1);
    FillSearchGrid(1);
    }

    protected void Page_Changed(object sender, CommandEventArgs e)
    {
    int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
    FillSearchGrid(pageIndex);
    ViewState["pageIndex"] = pageIndex;

    //LinkButton lnkPage=new LinkButton();
    //rptPager.FindControl(lnkPage)


    // LinkButton lb = sender as LinkButton;
    //GridViewRow grow = (GridViewRow)lb.NamingContainer;
    //string EpId = gv_products.DataKeys[grow.RowIndex].Values[0].ToString();//EpId

    LinkButton lnkPage = (LinkButton)sender;
    if (lnkPage != null)
    {
    lnkPage.CssClass = "aspNetDisabled GridPager";
    }
    // RepeaterItem item = (sender as LinkButton).NamingContainer as RepeaterItem;

    //LinkButton lb=item.FindControl("lnkPage") as LinkButton;
    ////lb.CssClass = "aspNetDisabled";
    ////lb.Attributes.Remove(
    //if (lb != null)
    //{
    //lb.Attributes.Add("class", "aspNetDisabled GridPager");
    //}

    //lb.Attributes["class"] = lb.Attributes["class"].Replace("GridPager", "aspNetDisabled GridPager");
    }

    aspx code

    <style type="text/css">
    a.GridPager
    {
    text-align: center !Important;
    text-decoration: none !Important;
    background-color: #f5f5f5 !Important;
    color: #969696 !Important;
    border: 1px solid #969696 !Important;
    padding: 0px 3px;
    font-size: 12px;
    }
    a.aspNetDisabled
    {
    background:#006699 !important;
    color:#ffffff !important;
    }
    .GridPager span
    {
    background-color: #A1DCF2 !Important;
    color: #000 !Important;
    border: 1px solid #3AC0F2 !Important;
    }

    </style>

    <asp:Repeater ID="rptPager" runat="server">
    <ItemTemplate>
    <asp:LinkButton ID="lnkPage" runat="server" Text='<%#Eval("Text") %>' CommandArgument='<%# Eval("Value") %>'
    Enabled='<%# Eval("Enabled") %>' CssClass="GridPager" OnCommand="Page_Changed"></asp:LinkButton>
    </ItemTemplate>
    </asp:Repeater>

    Enabled is true when i click on 2 

    Please check my code thank you in advance 

    Thursday, April 25, 2019 9:02 AM
  • User288213138 posted

    Hi  nagapavanich,
     
    Based on your code, I tried to reproduce your problem and found that CSS can be changed in the rptPager_ItemDataBound() method. You can refer to my code below.
     
    The Code:

    protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Value"), new DataColumn("Text"), new DataColumn("Enabled", typeof(bool)) });
                    dt.Rows.Add(1, "Sam1", true);
                    dt.Rows.Add(2, "Sam2", true);
                    dt.Rows.Add(3, "Sam3", true);
                    dt.Rows.Add(4, "Sam4", true);
                    rptPager.DataSource = dt;
                    rptPager.DataBind();
                }
            }
            private void GetCustomersPageWise(int pageIndex)
            {
               
                     int recordCount = 3000;
                     this.PopulatePager(recordCount, pageIndex);
               
            }
            private void PopulatePager(int recordCount, int currentPage)
            {
                double dblPageCount = (double)((decimal)recordCount /500);
                int pageCount = (int)Math.Ceiling(dblPageCount);
                List<ListItem> pages = new List<ListItem>();
                if (pageCount > 0)
                {
                    pages.Add(new ListItem("First", "1", currentPage > 1));
                    for (int i = 1; i <= pageCount; i++)
                    {
                        pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
                    }
                    pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
                }
                rptPager.DataSource = pages;
                rptPager.DataBind();
            }
    
            protected void Page_Changed(object sender, EventArgs e)
            {
                int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
                this.GetCustomersPageWise(pageIndex);
    
                //LinkButton lnkPage = (LinkButton)sender;
                //if (lnkPage != null)
                //{
                //    lnkPage.CssClass = "bb";
                //}
            }
    
            protected void rptPager_ItemDataBound(object sender, RepeaterItemEventArgs e)
            {
    
                LinkButton my_btn = (LinkButton)e.Item.FindControl("lnkPage");
                if (my_btn != null)
                {
                    my_btn.CssClass = "aspNetDisabled";
                }
    
            }
    

    The Result:

    Best Regards,

    Sam

    Friday, April 26, 2019 7:25 AM
  • User-943250815 posted

    You should make your changes in Repeater.ItemDataBound event.
    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.repeater.itemdatabound?view=netframework-4.8

    protected void rptPager_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        LinkButton zLnk = (LinkButton)e.Item.FindControl("lnkPage");
        if (zLnk.Enabled == false)
            zLnk.CssClass = "aspNetDisabled";
    }
    

    Friday, April 26, 2019 12:01 PM
  • User1804579801 posted

    Thanks for your replays I have tired the same in another default page works as required something is blocking in my page.I need to check mean while I have come up with alternate solution of displaying the current page when ever user clicks on the page no .Thanks once again for your replies 

    Monday, April 29, 2019 5:17 AM