Click here to Skip to main content
15,893,381 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Quote:
I want to get cascading dropdown list with in gridview , I have seen many artilces, none of them are working. So many used webservices and Ajax, but they did not clearly explain. plz plz someone clear this doubt
Posted

1 solution

First of all, Please describe where you stuck at and point out your doubt about the thing where you stuck
further you can see these articles that describes cascading dropdown clearly
Cascading DropDownList on GridView[^]
Implement AJAX Cascading DropDown with DropDownList in GridView in ASP.Net[^]
Ajax Cascading DropDownList With GridView[^]

and Google[^] is your friend.
 
Share this answer
 
Comments
Member 11785128 23-Jun-15 2:50am    
Thank you very much for the quick response... I have tried the code in the following link
http://www.aspsnippets.com/Articles/Implement-AJAX-Cascading-DropDown-with-DropDownList-in-GridView-in-ASPNet.aspx
On page load only Contact Name is binding to gridview, not binded the dropdown lists.. and one more doubt is after creating .asax file,do we have to add it to the project using add service reference??
Abhishek Pant 23-Jun-15 4:28am    
That's your choice to bind fields on page load you may opt to add or my not, if you do not need this you can also remove that or add other fields too.

In the above link(in comments) the Author added the .asmx(service) file in the the same project and uses CascadingDropDown component from ajax toolkit and call the service path and its ServiceMethod for loading the data.

Further if you require any pre-operation regarding the application startup and other ones.. you can add .asax file(Global.asax). But I guess you wanted to write about .asmx(service) file ,its not required to add that as its in the same project.
Member 11785128 23-Jun-15 2:55am    
I will be very thankful, If any of you gives me the solution... struggling alot
Member 11785128 23-Jun-15 3:05am    
I will send the 3 pages which I used here..plz plzz check it...
1 . Ajaxcheck.aspx
2. Ajaxcheck.aspx.cs
3. Service.cs

Ajaxcheck.aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajaxcheck.aspx.cs" Inherits="Ajaxcheck" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" EnablePageMethods="true">

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<columns>
<asp:BoundField HeaderText="ContactName" DataField="ContactName" />
<asp:TemplateField HeaderText="Country">
<itemtemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Bind("Country") %>' Visible = "false" />
<asp:DropDownList ID="ddlCountries" runat="server" Width = "150" >

<cc1:CascadingDropDown ID="cdlCountries" TargetControlID="ddlCountries" PromptText = "Select Country" PromptValue = "af"
ServicePath = "Service.asmx" ServiceMethod="GetCountries"
runat="server" Category="Country" />


<asp:TemplateField HeaderText="City">
<itemtemplate>
<asp:DropDownList ID="ddlCities" runat="server" Width = "150">

<cc1:CascadingDropDown ID = "cdlCities" TargetControlID="ddlCities" PromptText = "Select City" PromptValue = ""
ServicePath = "Service.asmx" ServiceMethod="GetCities" runat="server"
Category="City" ParentControlID = "ddlCountries" />




</div>
</form>
</body>
</html>
Member 11785128 23-Jun-15 3:06am    
2 . Ajaxcheck.aspx.cs

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;

public partial class Ajaxcheck : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("SELECT TOP 10 ContactName,Country,City FROM Customers");
GridView1.DataBind();
}
}
private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["ProdrepConnectionString"].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;
}
}
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
string value = e.Row.Cells[0].Text;
ddlCountries.DataSource = GetData("SELECT DISTINCT Country FROM Customers ");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
//Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, new ListItem("Please select"));
//Select the Country of Customer in DropDownList
string country1 = (e.Row.FindControl("lblCountry") as Label).Text;
ddlCountries.Items.FindByValue(country1).Selected = true;
}
}

}

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