Sep 22, 2011

How to call server side function using JQuery in Asp.net

Click Here for Download Source Code

Below is Default.aspx page code
-------------------------------

<%@ 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 id="Head1" runat="server">
<title>Call server side method using Jquery</title>

<!-- Add the jQuery Reference Library-->
<script src="Script/jquery-1.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$('#btnok').click(function() {
$.ajax({
type: "POST",
url: "Default.aspx/Test1",
data: "{'args1':'Kalpesh','args2':'Patel'}",
contentType: "application/json",
dataType: "json",
success: function(msg) {
// Replace the div's content with the page method's return.
$("#newresult").text(msg.d);
}
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="Button1" type="button" value="button" />
<span id="status"></span>
</div>
</form>
</body>
</html>


Code Behide file code (Default.aspx.cs)
-----------------------------------------


using System;
using System.Web.Services;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
public void test()
{


}

[WebMethod(EnableSession = false)]
public static string Test1(string args1, string args2)
{
return "Hello: " + args1 + " " + args2;
}
[WebMethod]
public static string Test2()
{


return "Hello: " + DateTime.Now.Millisecond + " -- Kalpesh Patel";
}
}

Sep 14, 2011

How to split a string with multi-character delimiter in Asp.Net

string [] words = myStr.Split(new String[] { "##" }, StringSplitOptions.None);

Sep 3, 2011

Disable Validation Control From JavaScript.

Just simply call the inbuilt Ajax function from Javascript.

<script type="text/javascript">
function HasPageValidators() {
var hasValidators = false;

try {
if (Page_Validators.length > 0) {
hasValidators = true;
}
}
catch (error) {
}

return hasValidators;
}

function ValidationGroupEnable(validationGroupName, isEnable) {
if (HasPageValidators()) {
for (i = 0; i < Page_Validators.length; i++) {
if (Page_Validators[i].validationGroup == validationGroupName) {
ValidatorEnable(Page_Validators[i], isEnable);
}
}
}
}
</script&ft;

Call Function :

ValidationGroupEnable(validationgroupname, false);
Note : It Will Disable validation of Control for "validationgroupname"
there EnableClientScript true For Control