Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to prevent page refresh in link button click event using asp.net C#


asp.code :

XML
<div class="review-footer-new">

<p class="review-feedbackQuestion">
    Was this review helpful?
    <asp:HiddenField ID="comment_like" runat="server" Value='<%#Eval("comment_id") %>' />

    <asp:HiddenField ID="comment_product_id" runat="server" Value='<%#Eval("id") %>' />

    <asp:LinkButton ID="cmt_like" runat="server" OnClick="cmt_like_Click">

        <span class="review-feedbackLink login-required" data-rating="1">
            <span class="review-feedback-isHelpful"></span>&nbsp;Yes
        </span>
    </asp:LinkButton>
    <asp:LinkButton ID="cmt_dislike" runat="server" OnClick="cmt_dislike_Click" onClientClick="return false">

        <span class="review-feedbackLink login-required" data-rating="0">
            <span class="review-feedback-isNotHelpful"></span>&nbsp;No
        </span>
    </asp:LinkButton>
</p>
                    </div>



c# code :

C#
protected void cmt_like_Click(object sender, EventArgs e)
    {
        LinkButton btn = (LinkButton)sender;
        RepeaterItem item = (RepeaterItem)btn.NamingContainer;
        HiddenField comment_like = (HiddenField)item.FindControl("comment_like");

        if(Request.QueryString["id"]!=null)
        {
        int id;
        id = Convert.ToInt32(Request.QueryString["id"].ToString());
        con = new SqlConnection(str);
        con.Open();
        cmd = new SqlCommand("sp_command_like_dislike", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Action", "Y");
        cmd.Parameters.AddWithValue("@comment_id", comment_like.Value);
        cmd.Parameters.AddWithValue("@eid", id);
        cmd.ExecuteNonQuery();
        con.Close();
        }
        
    }

   
    protected void cmt_dislike_Click(object sender, EventArgs e)
    {

        LinkButton btn = (LinkButton)sender;
        RepeaterItem item = (RepeaterItem)btn.NamingContainer;
        HiddenField comment_like = (HiddenField)item.FindControl("comment_like");

        if (Request.QueryString["id"] != null)
        {
            int id;
            id = Convert.ToInt32(Request.QueryString["id"].ToString());
            con = new SqlConnection(str);
            con.Open();
            cmd = new SqlCommand("sp_command_like_dislike", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@Action", "N");
            cmd.Parameters.AddWithValue("@comment_id", comment_like.Value);
            cmd.Parameters.AddWithValue("@eid", id);
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }


jquery :


XML
<script type="text/javascript">
     jQuery(function ($) {
         $("a[disabled]").live("click", function () {
             e.preventDefault();
         });

         $(".review-feedbackLink").live("click", function () {
             if ($(this).closest("a").is("[disabled]")) { return false; }
             $(this).addClass("selected");
             $(this).parent().after("<span class='thanks'> Thanks for your vote! </span>");
             $(this)
                 .closest(".review-feedbackQuestion")
                 .find(".review-feedbackLink")
                 .closest("a")
                 .attr("disabled", "disabled");
         });
     })
       </script>
Posted
Updated 25-Feb-15 19:10pm
v2

LinkButton always Posts Back. So, the page will be loaded again. You can use HTML HyperLink, which does not Post Back.
 
Share this answer
 
Use asp update panel for prevent post back on link button
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900