<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
< html xmlns="http://www.w3.org/1999/xhtml" >
< head runat="server">
< title >Untitled Page< /title >
< script language="javascript" type="text/javascript" >
function GetServerString()
{
PageMethods.getString(OnGetProductsComplete);
}
function OnGetProductsComplete(result)
{
alert(result);
}
function GetCity()
{
var txtCityId = document.getElementById('txtCityId');
var id = txtCityId.value;
PageMethods.getCityById(id,OnGetProductsComplete);
}
< /script >
< /head >
< body >
< form id="form1" runat="server" >
< /form >
< /body >
< /html >
2. .aspx.cs file
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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string getString()
{
return ("Hi Mahmad !");
}
[System.Web.Services.WebMethod()]
[System.Web.Script.Services.ScriptMethod()]
public static string getCityById(int CityId)
{
SqlConnection cn = new SqlConnection("Database=Emp;Server=mahmad\\sqlexpress;User Id=sa;password=sql;");
SqlCommand cmd = new SqlCommand("select City_Name from cities Where City_ID=" + CityId);
cmd.Connection = cn;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
cn.Close();
cmd.Dispose();
return ds.Tables[0].Rows[0]["City_Name"].ToString();
}
cn.Close();
cmd.Dispose();
return "Sorry City Not Found !";
}
}
Visit Below Link To Download Full Example
http://mahmad-khoja.googlegroups.com/web/WebMethodExample.rar
No comments:
Post a Comment