当<a href=""> object is clicked in c#

时间:2019-01-09 21:20:58

标签: c# html asp.net href asp.net-4.5

I have 2 hrefs like below in asp.net 4.5:

<a href="/Admin/<%= prevPage %>" role="button" ><i class="fa fa-left"></i></a>
<a href="/Admin/<%= nextPage %>" role="button" ><i class="fa fa-right"></i></a>

where I have some process to do in page_load in c# like

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
       //loading data from db;
    }
}

When I click on this a href, the page_load event is triggered and IsPostBack comes as false. Therefore, it is loading the data again and again whenever someone clicks previous or next page hrefs, causing a performance issue. I need to prevent this happening, meaning that I want IsPostBack = true after an href link is clicked. What is the best way to handle this issue?

I have tried to use onclientlick="return false;", it did not work. I have tried to use asp:button and asp:linkbutton like below, did not work.

<asp:Button ID="btnPrev" runat="server" Text="left" OnClick="RedirectPreviousPage" />
protected void RedirectPreviousPage(object sender, EventArgs e)
{
   var prevPage = CurrentPage == 1 ? -1 : CurrentPage - 1;
   Response.Redirect("/Admin/" + prevPage);
}

What is the best solution? Any help or advise would be appreciated. I've checked lots of previous topics with related issue, but couldn't find a proper solution for my case. Apologies if I am causing a duplication. Regards.

Edit: also did not work.

<script>
     function RedirectPreviousPage()
     {
          window.location.href = "/Admin/<%= prevPage %>";
     }
</script>
<a href="#" role="button" onclick="RedirectPreviousPage(); return false;" ><i class="fa fa-chevron-left"></i></a>

1 个答案:

答案 0 :(得分:0)

Changing hrefs into "asp:LinkButton" and setting AutoPostBack property to true for those linkbuttons fixed my issue.