简体   繁体   中英

How to highlight page number of selected page for a linkbutton inside a repeater using pagedatasource?

I want to highlight the current page number selected.

ASPX code:

   <asp:Repeater ID="rptPaging" runat="server" onitemcommand="rptPaging_ItemCommand">
        <ItemTemplate>                             
             <asp:LinkButton ID="btnPage"  CommandName="Page" CommandArgument="<%# Container.DataItem %>" runat="server" ><%# Container.DataItem %>
              </asp:LinkButton>
       </ItemTemplate>
    </asp:Repeater>

Backend code:

protected void rptPaging_ItemCommand(object source, RepeaterCommandEventArgs e)
{


    if (e.CommandName == "Page")
    {    

         int index = e.Item.ItemIndex;

        for (int i = 0; i < rptPaging.Items.Count; i++)
        {
            LinkButton btnlnk = rptPaging.Items[i].FindControl("btnPage") as LinkButton;
            if (btnlnk != null)
            {
                btnlnk.CssClass = index == i ? "page_enabled" : string.Empty;
            }
        }

        PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
        DisplayData();
    }
}

Add an attribute.. onclick="OnClickPageBtnLink(this)" the LinkButton. Use the javascript function:

function OnClickPageBtnLink(sender)
{
   sender.class = "YourHighlightClass";

   var pageLinks = document.getElementsByTagName("a");

   for (var i = 0; i < pageLinks.length; i++)
   {
     if (sender.id != pageLinks[i].id)
     {
       if (pageLinks[i].id.indexOf('rptPaging_btnPage_') != 0)
       {
          pageLinks[i].class = "UnHighlightClass";
       }
     }           
   }
}

You may also use document.queryFor.. (I am not sure of the syntax) to get the client generated anchor elements. This is definitely not a standard answer, but it would work.

Move the following code:

PageNumber = Convert.ToInt32(e.CommandArgument) - 1;
DisplayData();

Above from:

if (e.CommandName == "Page")

It will definitely work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM