Bala
Bala

Reputation: 1476

C# Page postback

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

Upvotes: 3

Views: 508

Answers (3)

Cheng Chen
Cheng Chen

Reputation: 43531

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();
     }
}

Upvotes: 0

SirDemon
SirDemon

Reputation: 1758

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).

Upvotes: 1

Richard Neil Ilagan
Richard Neil Ilagan

Reputation: 14737

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.

Upvotes: 0

Related Questions