Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am going to implement a cascading dropdownlist inside a gridview. Like given below

My .aspx Page


ASP.NET
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="available_services.aspx.cs" MasterPageFile="~/Forms/Home.Master"  Inherits="HospitalManagementSystem.Forms.available_services" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
 <script type="text/javascript" src="scripts/jquery-1.10.1.min.js"></script>

<div>

    <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1"  runat="server">
    </ajaxToolkit:ToolkitScriptManager>

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
            <asp:BoundField HeaderText="Name" DataField="ContactName" />
            <asp:TemplateField HeaderText="Country">
                <ItemTemplate>
 <asp:DropDownList ID="ddlCountries" runat="server" Width="150">
                    </asp:DropDownList>
                    <cc1:CascadingDropDown ID="cdlCountries" TargetControlID="ddlCountries" PromptText="Select Country" PromptValue=""
                        ServicePath="ServiceCS.asmx" ServiceMethod="GetCountries"
                         runat="server" Category="Country" />
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City">
                <ItemTemplate>
                    <asp:DropDownList ID="ddlCities" runat="server" Width="150">
                    </asp:DropDownList>
                    <cc1:CascadingDropDown ID="cdlCities" TargetControlID="ddlCities" PromptText="Select City" PromptValue=""
                        ServicePath="ServiceCS.asmx" ServiceMethod="GetCities"  runat="server"
                        Category="City" ParentControlID="ddlCountries" />
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
 </div>
</asp:Content>



C# Code Behind

C#
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.Configuration;
using System.Data.SqlClient;
using System.Web.Services;
using AjaxControlToolkit;


namespace Testing.Forms
{

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

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) //Only do this first time it's loaded
        {
            BindMajorHeadDropdown();
            GridView1.DataSource = GetData("SELECT TOP 10 ContactName, Country FROM Customers");
            GridView1.DataBind();                              
        }
    }

private DataSet GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        using (SqlConnection con = new SqlConnection(conString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;

                sda.SelectCommand = cmd;
                using (DataSet ds = new DataSet())
                {
                    sda.Fill(ds);
                    return ds;
                }
            }
        }
    }
}



ServiceCS.asmx (Web Service)

C#
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using AjaxControlToolkit;



namespace Testing
{

/// <summary>
/// Summary description for ServiceCS
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

// 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 ServiceCS : System.Web.Services.WebService
{

    [WebMethod]
    public CascadingDropDownNameValue[] GetCountries(string knownCategoryValues)
    {
        string query = "SELECT DISTINCT Country FROM Customers";
        List<CascadingDropDownNameValue> countries = GetData(query);
        return countries.ToArray();
    }

    [WebMethod]
    public CascadingDropDownNameValue[] GetCities(string knownCategoryValues)
    {
        string country = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)["Country"];
        string query = string.Format("SELECT DISTINCT City FROM Customers WHERE Country = '{0}'", country);
        List<CascadingDropDownNameValue> cities = GetData(query);
        return cities.ToArray();
    }

    private List<CascadingDropDownNameValue> GetData(string query)
    {
        string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlCommand cmd = new SqlCommand(query);
        List<CascadingDropDownNameValue> values = new List<CascadingDropDownNameValue>();
        using (SqlConnection con = new SqlConnection(conString))
        {
            con.Open();
            cmd.Connection = con;
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    values.Add(new CascadingDropDownNameValue
                    {
                        name = reader[0].ToString(),
                        value = reader[0].ToString()
                    });
                }
                reader.Close();
                con.Close();
                return values;
            }
        }
    }
}
}



When i try to run the program i get nothing in the dropdownlist instead i get [Method error 500]. Please help me what should i do.
Posted
Comments
JoCodes 24-Dec-13 23:09pm    
Any details on the error?

 
Share this answer
 
It can be one of multiple reasons.
First try the service call url used for cascading dropdowns like

ServicePath="ServiceCS.asmx" modify to a full qualified path.

check the web service, make sure it working properly
Please try to browser it so that you can get the full url like http://localhost/path

Then next, it can be caused by too much data being returned in the SQL query behind the web service call.For that,increase the size limit of data returned in web.config .

Hope this helps...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900