Thursday, 30 January 2014

Instead of using Cascading Dropdown list in AJAX use LINQ


I've seen many of them are using Ajax "Cascading DropdownList" for selecting countries and states and other all .....
But the far better way is to use LINQ(Language Integrated Query) ....
Today i am going to share with you the linq query for selecting state on selection of Country dropdown....:)

Front end Image:


Developing :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    DataClassesDataContext data = new DataClassesDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var fetch = from m in data.CountryTabs select new { m.C_ID, m.CountryName };
            DropDownList1.DataTextField = "CountryName";
            DropDownList1.DataValueField = "C_ID";
            DropDownList1.DataSource = fetch;
            DropDownList1.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
      
        
        var fetch = from m in data.StateTabs where m.Cntry_ID==Convert.ToInt32(DropDownList1.SelectedItem.Value) select new {m.StateName};
        DropDownList2.DataTextField = "StateName";
        DropDownList2.DataValueField = "StateName";
        DropDownList2.DataSource = fetch;
        DropDownList2.DataBind();



    }
}

No comments:

Post a Comment

How To Pass An Array As A Parameter While Calling an ASP.NET Web API C#

Please visit my C# Corner Blog for this, where I have provided better efforts to make this concept more understandable - www.c-sharpcorner....