Thursday, 19 February 2015

CALL AJAX METHODS FROM JAVASCRIPT

Add Ajax dll 
Import the AJAX namespace from Ajax.DLL.

File->New->WebSite





Add Ajax dll (Add Reference of Ajax.dll)








Design the Form as Follows

 <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <table>
                <tr>
                    <td>Country </td>
                    <td><asp:DropDownList ID="ddlCountry" runat="server" ></asp:DropDownList></td>
                </tr>
                <tr>
                    <td>City </td>
                    <td> <asp:DropDownList ID="ddlCity" runat="server"></asp:DropDownList></td>
                </tr>
                <tr>
                    <td> <asp:Button ID="btnSave" runat="server" Text="save" OnClientClick="save()" /> </td>
                </tr>
            </table>
    </form>




Javascript Code

<script src="http://code.jquery.com/jquery-1.8.3.min.js">  </script>
    <script src="jquery-1.7.min.js"></script>
    <script type="text/javascript">
        $('#ddlCountry').live('change', function () {
            var rst = Default2.BindCities($(this).val()).value;
            $('#ddlCity').html(rst);
        })
    </script>







web.config

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <httpHandlers>
      <add verb="POST,GET" path="ajax/*.ashx" type="Ajax.PageHandlerFactory, Ajax"/>
    </httpHandlers>
    <httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
    </httpModules>
  </system.web>

</configuration>





.cs Page

Namespaces

using Ajax;

using System.Data.SqlClient;


protected void Page_Load(object sender, EventArgs e)
    {
        Ajax.Utility.RegisterTypeForAjax(typeof(Default2));
        SqlConnection con = new SqlConnection("------------");
        SqlCommand cmd = new SqlCommand("------", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        ddlCountry.DataSource = ds;
        ddlCountry.DataTextField = "Country";
        ddlCountry.DataValueField = "ID";
        ddlCountry.DataBind();
        ddlCountry.Items.Insert(0, new ListItem("SelectItem", "0"));
    }



[Ajax.AjaxMethod(HttpSessionStateRequirement.ReadWrite)]
    public string BindCities(int countryid)
    {
        StringBuilder _stringBuilder = new StringBuilder();
        try
        {
            SqlConnection con = new SqlConnection("----");
            SqlCommand cmd = new SqlCommand("", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            _stringBuilder.Append("<option value='0'> -SELECT-</option>");
                if (ds != null && ds.Tables != null && ds.Tables[0] != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    _stringBuilder.Append("<option value='" + ds.Tables[0].Rows[i]["ID"].ToString() + "'>" + ds.Tables[0].Rows[i]["CityCode"].ToString() + "</option>");
                }
            }
            return _stringBuilder.ToString();
        }
        catch (Exception Ex)
        {
            return Ex.Message.ToString();
        }

    }

Output:


No comments:

Post a Comment