How to prevent postback from asp.net linkbutton
Solution 1
try
OnClientClick="return false;"
UPDATE:
OnClientClick="showDiffUser();return false;"
showDiffUser();
calls the JS-Method
return false;
prevents postback
Solution 2
You only add your jQuery function call to the page after button is clicked. Of course if you suppress the click postback the function call never gets to the page and never gets executed.
What you want though to is to call this function and then suppress, right? Therefore do this:
OnClientClick="showDiffUser(); return false;"
And you do not need the server-side click handler anymore.
Solution 3
If you don't want to cause a postback don't use a .Net LinkButton, use a standard HTML hyperlink instead and use jQuery to capture the event:
<a id="my-link">Save</a>
<script>
$(function () {
$("#my-link").click(function () {
showDiffUser();
});
})
</script>
In the same call, you could do an AJAX call to raise your server-side validation:
<script>
$(function () {
$("#my-link").click(function () {
// AJAX Call
$.ajax({
type: "post",
url: "your-url",
success: function (returnData) {
showDiffUser();
}
});
});
})
</script>
SearchForKnowledge
I am just a programming enthusiast, learning about new things.
Updated on June 28, 2022Comments
-
SearchForKnowledge almost 2 years
ASP:
<asp:LinkButton ID="lbSave" OnClick="lbSave_Click" runat="server">Save</asp:LinkButton>
HTML:
<a id="ContentMain_lbSave" href="javascript:__doPostBack('ctl00$ContentMain$lbSave','')">Save</a>
Code-behind:
public void lbSave_Click(object sender, EventArgs e) { if (strP != "") { ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "DiffUser", "showDiffUser();", true); } else { //do something else... } }
JQuery:
function showDiffUser() { $("#dvHide").fadeIn("slow"); $("#backOverlay").fadeIn("slow"); }
When I click the button, I want the page to not do a postback and run the JQuery function. Instead, when I click the link it refreshes the page and then executes the JQuery function.
How can I prevent the postback.
I tried the following:
onClientClick='javascript:void(0);'
but that doesn't execute the JQuery function. -
SearchForKnowledge about 9 yearsI tried that but when I click the link, nothing happens.
-
SearchForKnowledge about 9 yearsThank you for the response. I need to have the server side call as shown in the updated question. Please advise.
-
SearchForKnowledge about 9 yearsThank you. I left out some information and updated my question. I have to call the function through the code-behind because I am doing if/else checking.
-
fubo about 9 yearswhen you want to do something serverside you need a postback or ajax
-
Andrei about 9 years@SearchForKnowledge, emm, a bit confused now. So do you want to disable postback completely or not? As it currently stands you want it to happen sometimes, but not always.
-
SearchForKnowledge about 9 yearsSo when the page loads,
strP
is assigned a value. if it is blank then execute JQuery otherwise do not execute. thestrP
is a server side variable. I know I am confusing the question. -
SearchForKnowledge about 9 yearsThe server side needs to be execute to retrieve a variable value. That is my issue unfortunately :/
-
SearchForKnowledge about 9 yearsThat sounds like a good idea. Please give me some info to start with :) Thank you.
-
Matt about 9 yearsI've edited to include an AJAX call where you can pass your variable back. Failing that you could always wrap the LinkButton in an UpdatePanel to supress the postback
-
SearchForKnowledge about 9 yearsSo how do I change the linkbutton code and the code-behind? Thank you btw for the clarification.
-
fubo about 9 yearsthis post seem to fit your needs stackoverflow.com/questions/18236634/…