In this article I will explain how to implement cascading dropdownlist in gridview using asp.net.
In Previous posts I explained clearly how to implement ajax cascading dropdownlist using asp.net and lot of articles regarding Gridview. Now I will explain how to implement cascading dropdown list in gridview using asp.net. Here I am using previous implemented article ajax cascading dropdownlist in asp.net to bind data to dropdownlists.
ColumnName | DataType |
UserId | Int(set identity property=true) |
UserName | varchar(50) |
LastName | varchar(50) |
Location | varchar(50) |
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %> <!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>Cascading Dropdownlist Sample</title> </head> <body> <form id="form1" runat="server"> <ajax:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server"/> <div> <asp:GridView runat="server" ID="gvdetails" DataSourceID="dsdetails" AllowPaging="true" PageSize="10" AutoGenerateColumns="false" ShowFooter="true"> <RowStyle BackColor="#EFF3FB" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:TemplateField HeaderText="UserId"> <ItemTemplate> <asp:Label ID="lblUserId" runat="server" Text='<%#Eval("UserId") %>'/> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="UserName"> <ItemTemplate> <asp:Label ID="lblUserName" runat="server" Text='<%#Eval("UserName") %>'/> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="ddlCountry" runat="server"></asp:DropDownList> <ajax:CascadingDropDown ID="CountryCascading" runat="server" Category="Country" TargetControlID="ddlCountry" LoadingText="Loading Countries..." PromptText="Select Country" ServiceMethod="BindCountrydropdown" ServicePath="DropdownWebService.asmx"> </ajax:CascadingDropDown> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="LastName"> <ItemTemplate> <asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName") %>'/> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="ddlState" runat="server"></asp:DropDownList> <ajax:CascadingDropDown ID="StateCascading" runat="server" Category="State" TargetControlID="ddlState" ParentControlID="ddlCountry" LoadingText="Loading States..." PromptText="Select State" ServiceMethod="BindStatedropdown" ServicePath="DropdownWebService.asmx"> </ajax:CascadingDropDown> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Location"> <ItemTemplate> <asp:Label ID="lblLocation" runat="server" Text='<%#Eval("Location") %>'/> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="ddlregion" runat="server"></asp:DropDownList> <ajax:CascadingDropDown ID="RegionCascading" runat="server" Category="Region" TargetControlID="ddlregion" ParentControlID="ddlState" LoadingText="Loading Cities..." PromptText="select" ServiceMethod="BindRegiondropdown" ServicePath="DropdownWebService.asmx"> </ajax:CascadingDropDown> </FooterTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="dsdetails" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>" SelectCommand="select * from UserInformation"/> </div> </form> </body> </html> |
<connectionStrings> <add name="dbconnection" connectionString="Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"/> </connectionStrings> |
![]() |
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
Subscribe by RSS
Subscribe by Email
|
|||
|
|
|


Subscribe by RSS
Subscribe by Email
6 comments :
Hi Suresh,
If the gridview initially does not have any data or is not binded to datasource but if we need some dropdownlists in such gridview. Should we have empty data template? I am confused what to do. I also have to populate the dropdownlist
Initially the gridview will have just 1 row with 4 dropdownlists which are populated. How to do this?
hi,
i think that feature available in 4.0 version except that version we don't have any option like empty data template. we need to write custom code to display gridview even if that doesn't contain any data check below post
http://www.aspdotnet-suresh.com/2010/12/v-behaviorurldefaultvmlo.html
fine
Hello sir,
I am getting method error 500 rply
hi friend i need 1 textbox1 ajax autocomplete, on select suggestion my textbox2 and textbox3 automatically display corresponding field the database table,,,,
c#.net microsoft sql server, ajax(asmx web service)
please provide your database backup