Friday, March 25, 2011

Bind Drop Down List Using JSON




<%@ 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 src="JS/jquery-1.4.2.min.js" type="text/javascript"></script>
    <script language="javascript" >

        $(function() {
            $(window).load(function() {

                BindDDL();

            });
        });
        function AddItems(ddlName, textField, ValueField) {
            var opt = document.createElement("option");
            opt.text = textField;
            opt.value = ValueField;
            ddlName.options.add(opt);
        }
        function BindDDL() {
            var ddlList = document.getElementById('<%=ddlList.ClientID %>');
            $.ajax(
                {
                    type: "POST",
                    url: "Default.aspx/BindData",
                    data: "{}",
                    contentType: "application/json",
                    dataType: "json",
                    success: function(data) {

                        for (var i = 0; i < data.d.length; i++) {

                            AddItems(ddlList, data.d[i].InvoiceNumber, data.d[i].invoiceid);
                        }


                    }
                });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlList" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{


    protected void Page_Load(object sender, EventArgs e)
    {

        BindData();
    }

    [System.Web.Services.WebMethod]
    public static MyClass[] BindData()
    {
        string ConnectionString = ConfigurationManager.ConnectionStrings["appConnectionString"].ToString();
        SqlConnection con = new SqlConnection(ConnectionString);
        SqlCommand cmd = new SqlCommand();
        DataTable dt = new DataTable();
        cmd.CommandText = "     Select invoiceid,InvoiceNumber from Payable_InvoiceMaster  ";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        da.Fill(dt);
        System.Collections.Generic.List<MyClass> mListMyClass = new System.Collections.Generic.List<MyClass>(); //Create Generic List
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            MyClass mClass = new MyClass();
            mClass.invoiceid = Convert.ToInt32(dt.Rows[i][0]);
            mClass.InvoiceNumber = dt.Rows[i][0].ToString();

            mListMyClass.Add(mClass);
        }

        return (mListMyClass.ToArray());

    }

    public class MyClass    //Create a Class
    {
        public int invoiceid;
        public string InvoiceNumber;

    }

}
 here download Jquey File....
http://api.jquery.com/category/version/1.4.2/


No comments:

Post a Comment