Showing posts with label sample code. Show all posts
Showing posts with label sample code. Show all posts

Wednesday, May 8, 2013

Sample code for ajax update panel in asp.net

Here we call ajax in asp.net using script manager update panel. by using update panel we can update particular portion of page.
Sample code as below :
Default.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <br />
        <div>
            <asp:Label ID="Label1" runat="server" Text="Label" Width="249px"></asp:Label><br />
            <br />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    &nbsp;
            <hr />       
            <asp:Label ID="Label2" runat="server" Text="Label" Width="249px"></asp:Label>
            <hr />
            </ContentTemplate>
            <Triggers>
            <asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
            <asp:PostBackTrigger ControlID="Button2"/>
            </Triggers>
            </asp:UpdatePanel>
            <br />
            <asp:Label ID="Label3" runat="server" Text="Label" Width="249px"></asp:Label><br />
            <br />
            &nbsp;<asp:Button ID="Button1" runat="server" Text="Refresh single label only" Width="233px" OnClick="Button1_Click" />
            <asp:Button ID="Button2" runat="server" Text="Refresh all" Width="233px" OnClick="Button2_Click" />
            </div>
    </form>
</body>
</html>

Default.aspx.cs

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Label1.Text = DateTime.Now.ToString();
        Label2.Text = DateTime.Now.ToString();
        Label3.Text = DateTime.Now.ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
    }
}

Ouptut :

Source code download : Download Source code

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website

sample code for call webservice using ajax in asp.net

Here we call webservice using ajax update panal and javascript .
We see also how an ASP.NET AJAX-enabled web service generates the JavaScript needed to call the web service.
: sample code as below:
Webservice.asmx
using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService {
    public WebService () {
        //Uncomment the following line if using designed components
        //InitializeComponent();
    }
    [WebMethod]
    public string SayHello(int a,int b)
    {
        return Convert.ToString(a + b);
    }
  
   
}
Default.aspx
<%@ 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">
    <title>Untitled Page</title>
    <script language="javascript" type="text/javascript">
<!--
function Button1_onclick() {
ret = WebService.SayHello(document.getElementById('Text1').value,document.getElementById('Text2').value,  OnComplete, OnTimeOut, OnError);
return(true);
}
function OnComplete(arg) {
alert(arg);
}
function OnTimeOut(arg) {
alert("TimeOut encountered when calling Say Hello.");
}
function OnError(arg) {
alert("Error encountered when calling Say Hello.");
}
// -->
</script>
</head>
<body>
      <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
        <asp:ServiceReference Path="~/WebService.asmx" />
        </Services>
        </asp:ScriptManager>
        <br />
        <div>
            <input id="Text1" type="text" /><br />
                       <input id="Text2" type="text" /><br />
            <input id="Button1" style="width: 158px" type="button" value="button" language="javascript" onclick="return Button1_onclick()" />&nbsp;</div>
    </form>
</body>
</html>

Source code download : Source code

If you are searching life partner. your searching end with kpmarriage.com. now kpmarriage.com offer free matrimonial website which offer free message, free chat, free view contact information. so register here : kpmarriage.com- Free matrimonial website