tags:

views:

53

answers:

3
+3  Q: 

C# Page postback

Is it possible to stop the page postback on any click event based on some validation

A: 

if you mean doing it client-side (i.e. make it so that the page doesn't postback), you'll probably have to do it in Javascript.

just attach a click event JS function on whatever it is you want validated, and have it return false; when you don't want it to postback to server-side ASP.NET.

Richard Neil Ilagan
+1  A: 

You can use client side validation of different kinds. With ASP.NET WebForms, the easiest way would probably be the built in validators (e.g: RequiredFieldValidator).

SirDemon
A: 

Answers above are correct. Here is another way to block postback.

Page.ClientScript.RegisterOnSubmitStatement(string key, string script)

We can prevent pages from postback at onsubmit event because:

function __doPostBack(eventTarget, eventArgument) {
     if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
     }
}
Danny Chen