how to prevent page refresh in link button click event using asp.net C#
asp.code :
<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> 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> No
</span>
</asp:LinkButton>
</p>
</div>
c# code :
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 :
<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>