How to Stop PostBack of Asp Button
-
How do I stop the postback from a asp button? I want to do some validation on the client side when a button is pressed and decide whether to post back to the server or not. Thanks Hari
-
How do I stop the postback from a asp button? I want to do some validation on the client side when a button is pressed and decide whether to post back to the server or not. Thanks Hari
Alternatively, if you're using 2.0, the buttons expose an OnClientClick attribute which fires (on the client side obviusly, so you'll need to give it a javascript function) before OnClick...to stop OnClick, return false from OnClientClick
"Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox
-
Hi Hari you can do this by adding small attribute(JavaScript function) to Button. on form load event of code behind file .Attributes.Add("OnClick", "return confirm('Continue?');"); or hope this Help Thanks..
Hi TRK@UK, Thanks for the response, this is working fine as expected. I tried something more...
In Page Load
Object objAjax = Request.QueryString["from"];
if (objAjax != null)
{
Response.Clear();
Response.Write("1");
Response.End();
}//Button1.Attributes.Add("OnClick", " if(TextBox1.value=='hari'){return true}else{return false};"); Button1.Attributes.Add("OnClick", " var finalval = chkText(); if(finalval == false){return false;}");
var XmlHttp; function chkText() { try { XmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { XmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch(e1) { XmlHttp = null; } } //Creating object of XMLHTTP in Mozilla and Safari if(!XmlHttp && typeof XMLHttpRequest != "undefined") { XmlHttp = new XMLHttpRequest(); } if(XmlHttp) { try { XmlHttp.onreadystatechange = HandleResponse; XmlHttp.open("POST", "CallingServerFunctionUsingAjax.aspx?from=Ajax",true); XmlHttp.send(null); if(XmlHttp.readyState==1) { return false; } } catch(e2) { alert(e2); } }
}
function HandleResponse()
{
try
{
if(XmlHttp.readyState == 4)
{
if(XmlHttp.status == 200)
{
var retval=XmlHttp.responseText;
switch(retval)
{
case "1":
return false;
break;default: break; } } else </x-turndown>
-
Alternatively, if you're using 2.0, the buttons expose an OnClientClick attribute which fires (on the client side obviusly, so you'll need to give it a javascript function) before OnClick...to stop OnClick, return false from OnClientClick
"Now I guess I'll sit back and watch people misinterpret what I just said......" Christian Graus At The Soapbox