.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
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
Member
5 Points
77 Posts
css class not applying for linkbutton from code behind
Apr 24, 2019 12:41 PM|nagapavanich|LINK
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
Contributor
3370 Points
1409 Posts
Re: css class not applying for linkbutton from code behind
Apr 25, 2019 08:21 AM|samwu|LINK
Hi nagapavanich,
I tried to reproduce your problem, but my code works fine, Is your Enabled property set to false?
The code:
The Result:
Best Regards,
Sam
Member
5 Points
77 Posts
Re: css class not applying for linkbutton from code behind
Apr 25, 2019 09:02 AM|nagapavanich|LINK
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
Contributor
3370 Points
1409 Posts
Re: css class not applying for linkbutton from code behind
Apr 26, 2019 07:25 AM|samwu|LINK
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:
The Result:
Best Regards,
Sam
Participant
1061 Points
667 Posts
Re: css class not applying for linkbutton from code behind
Apr 26, 2019 12:01 PM|jzero|LINK
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
Member
5 Points
77 Posts
Re: css class not applying for linkbutton from code behind
Apr 29, 2019 05:17 AM|nagapavanich|LINK
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