I use a button (of type button not submit) and on the click the data is submitted to server and i use jquery ajax for that,but at that time the asp.net validations are not called,how my asp.net validation execute when user click on this button before executing
the j query ajax
You may either have to move ur validation to the web method u r calling or to add an another call to the server for doing the validation before calling the actual web method.
Nothing is impossible ramesh if there are some java script functions which runs when the user submit the forms,then i think there is also a some way by which we call this function manually and i am searching out that way...
I believe Page_IsValid is only available after a postback, but vish02chouhan wants to do it without a postback. Please read the below excerpt
If the request is a postback, control event handlers are called. After that, the
Validate method of all validator controls is called, which sets the
IsValid property of individual validator controls and of the page.
If i use asp button then also i have to do preventDefault(); and call the ajax function before that, and asp.net validation at the starting point,is their is any way to do this
I believe Page_IsValid is only available after a postback
what makes u think that...
vish02chouhan
If i use asp button then also i have to do preventDefault(); and call the ajax function before that, and asp.net validation at the starting point,is their is any way to do this
u can do this...
OnClientClick event of asp:button control, call Page_ClientValidate() method... which will validate all validation controls are true...
if it is valid, call jquery ajax.... else, return false
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod]
public static string Test(string name)
{
return "This is called from server side : " + name;
}
}
the jquery post is only called if requiredfieldvalidator is valid else, validator shown message
hope this helps...
Cheers!
KK
Please mark as Answer if post helps in resolving your issue
My Site
This excerpt of msdn page 'http://msdn.microsoft.com/en-us/library/ms178472.aspx'
If the request is a postback, control event handlers are called. After that, the
Validate method of all validator controls is called, which sets the
IsValid property of individual validator controls and of the page.
This excerpt of msdn page 'http://msdn.microsoft.com/en-us/library/ms178472.aspx'
If the request is a postback, control event handlers are called. After that, the
Validate method of all validator controls is called, which sets the
IsValid property of individual validator controls and of the page.
u should understand that this line in the link... is talking about server side validation and server side proprty IsValid
we are here dealing with the clien side validation... asp.net validation framework provides support from both client as well as server side validation mechanism.... the context of question is based on client side validation... so it does not require postback
also, in u r comment, u missed to mention complete excerpt..... it continues
(There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
hope this is clear for you now
Cheers!
KK
Please mark as Answer if post helps in resolving your issue
My Site
vish02chouha...
Member
303 Points
251 Posts
Run .net validations before jquery ajax
Feb 24, 2012 09:40 AM|LINK
I use a button (of type button not submit) and on the click the data is submitted to server and i use jquery ajax for that,but at that time the asp.net validations are not called,how my asp.net validation execute when user click on this button before executing the j query ajax
Ramesh T
Contributor
5111 Points
827 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 09:42 AM|LINK
I'm afraid u can't do this.
You may either have to move ur validation to the web method u r calling or to add an another call to the server for doing the validation before calling the actual web method.
vish02chouha...
Member
303 Points
251 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 09:46 AM|LINK
Nothing is impossible ramesh if there are some java script functions which runs when the user submit the forms,then i think there is also a some way by which we call this function manually and i am searching out that way...
kedarrkulkar...
All-Star
34305 Points
5520 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 10:06 AM|LINK
check this before jquery post
if (Page_IsValid) {
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
Ramesh T
Contributor
5111 Points
827 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 10:15 AM|LINK
@kedarrkulkarni
I believe Page_IsValid is only available after a postback, but vish02chouhan wants to do it without a postback. Please read the below excerpt
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
@vish02chouhan
I agree, but u have to put lot of time in it, why not u just use the asp button or one of the ways I suggessted earlier?
vish02chouha...
Member
303 Points
251 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 10:58 AM|LINK
If i use asp button then also i have to do preventDefault(); and call the ajax function before that, and asp.net validation at the starting point,is their is any way to do this
p.o.d.
Member
325 Points
110 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 01:04 PM|LINK
I think this is exactly what you need.
http://www.aspdotnetfaq.com/Faq/How-to-control-ASP-NET-Validator-Controls-Client-Side-validation-from-JavaScript.aspx
http://msguy.net/
kedarrkulkar...
All-Star
34305 Points
5520 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 01:08 PM|LINK
what makes u think that...
u can do this...
OnClientClick event of asp:button control, call Page_ClientValidate() method... which will validate all validation controls are true...
if it is valid, call jquery ajax.... else, return false
test this code.... add this in .aspx file
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <title></title> <script type="text/javascript"> function CallAjax() { if (Page_ClientValidate()) { $.ajax({ type: "POST", url: "Default2.aspx/Test", data: '{name: "' + $("#TextBox1").val() + '" }', contentType: "application/json; charset=utf-8", dataType: "json", success: function (msg) { // Replace the div's content with the page method's return. alert(msg.d); } }); return false; } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <br /> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="return CallAjax()"/> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Value required in textbox" ControlToValidate="TextBox1"></asp:RequiredFieldValidator> </div> </form> </body> </html>add this in codebehind
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } [System.Web.Services.WebMethod] public static string Test(string name) { return "This is called from server side : " + name; } }the jquery post is only called if requiredfieldvalidator is valid else, validator shown message
hope this helps...
KK
Please mark as Answer if post helps in resolving your issue
My Site
Ramesh T
Contributor
5111 Points
827 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 01:12 PM|LINK
@kedarrkulkarni
This excerpt of msdn page 'http://msdn.microsoft.com/en-us/library/ms178472.aspx'
If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.
kedarrkulkar...
All-Star
34305 Points
5520 Posts
Re: Run .net validations before jquery ajax
Feb 24, 2012 01:20 PM|LINK
u should understand that this line in the link... is talking about server side validation and server side proprty IsValid
we are here dealing with the clien side validation... asp.net validation framework provides support from both client as well as server side validation mechanism.... the context of question is based on client side validation... so it does not require postback
also, in u r comment, u missed to mention complete excerpt..... it continues
(There is an exception to this sequence: the handler for the event that caused validation is called after validation.)
hope this is clear for you now
KK
Please mark as Answer if post helps in resolving your issue
My Site