Cascading dropdownlist in gridview using asp.net

Introduction:
This article explains how to implement cascading dropdownlist in gridview using asp.net.


 After completion table creation add ajax reference to your application because here I am using ajax cascading dropdownlists bind to gridview and design your aspx page like this

--------------------------------------------------------------------------------------------------------------------------
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" 
TagPrefix="ajax" %>
<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>

--------------------------------------------------------------------------------------------------------------------------
  If you observe above code I used ajax cascading dropdownlist to bind in gridview. To bind data to dropdownlist and data tables design check this post ajax cascading dropdownlist using asp.net .After completion of bind data to dropdownlists use above code to place dropdownlist in gridview and bind data to gridview if you have any doubts just download the attached code to and check the details.

Here don’t forgot to set the connection string in web.config file here I am getting database connection from web.config file for that reason you need to set the connectionstring in web.config file like this
-------------------------------------------------------------------------------------------------------------------------
<connectionStrings> <add name="dbconnection" connectionString="Data Source=vepsh;Integrated Security=true;Initial Catalog=SampleDB"/> </connectionStrings>
---------------------------------------------------------------------------------- 
Ashwani
Ashwani

This is a short biography of the post author. Maecenas nec odio et ante tincidunt tempus donec vitae sapien ut libero venenatis faucibus nullam quis ante maecenas nec odio et ante tincidunt tempus donec.

No comments:

Post a Comment