Monday 17 October 2011

JQuery + AJAX example



In Demo.aspx page
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" 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>Untitled Page</title>
<script src="/JsFiles/jquery1.4.js" type="text/javascript"></script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$('#button1').click(function() {
var a = $('#Label1').html();
var b = $('#TextBox1').val();
var c = $('#Button1').val();
alert($('#CheckBox1').is(':checked'));
var result;
$('.rbl').each(function() {
var checked = $(this).find('input:radio:checked');
alert(checked.val());
if (checked.length == 0) {
result = false;
return;
}
});
alert(result);
$('#display').hide();
var l = "a=" + a;
$.ajax({
url: "ajaxrequest.aspx",
type: "POST",
data: l,
success: function(a) { if (a == "success") { alert("successed"); } else { alert(a); }; },
error: function(a) { alert(a); }

});

});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="ramlabel-ram"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<input type ="button" id='button1' runat = "server" value = "button" />
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
<asp:DropDownList ID="DropDownList1" runat="server">
</asp:DropDownList>
<asp:RadioButtonList ID="RadioButtonList1" class = "rbl" runat="server" RepeatDirection="Horizontal">
<asp:ListItem Text = "sum1" Value = "1"></asp:ListItem>
<asp:ListItem Text = "sum3" Value = "3"></asp:ListItem>
<asp:ListItem Text = "sum2" Value = "2"></asp:ListItem>
</asp:RadioButtonList>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:CheckBox ID="CheckBox2" runat="server" />
<div id ="display" >
hai this is for testing purpose by ram.
</div>
</div>
</form>
</body>
</html>

in    ajaxrequest page

<%@ page Language ="VB" ValidateRequest = "false" %>
<%@ Import Namespace ="system" %>
<%@ Import Namespace = "system.data" %>
<%@ Import Namespace ="system.data.sqlclient" %>
<%@ Import Namespace = "system.web.ui.page" %>
<%@ Import Namespace = "system.configuration" %>
<% Response.AddHeader("Pragma", "no-cache") %>
<% Response.AddHeader("cache-control","no-cache,must-revalidate") %>

<script language ="vb" type = "text/VB" runat ="server" >
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

Response.Write("success")
'write your code
End Sub
</script>