Friday, July 4, 2014

Two Dropdowns in GridView Country and State

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

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:abc %>"
            SelectCommand="SELECT * FROM Country"></asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Countryid"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="Countryid" HeaderText="Countryid" InsertVisible="False"
                    ReadOnly="True" SortExpression="Countryid" />
                <asp:TemplateField HeaderText="Categories">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlCategories" AutoPostBack="true" DataTextField="CountryName"
                            DataValueField="Countryid" DataSourceID="SqlDataSource1" runat="server" AppendDataBoundItems="true"
                            SelectedValue='<%# Bind("Countryid") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Products">
                    <ItemTemplate>
                        <asp:DropDownList ID="ddlProducts" DataTextField="StateName" DataValueField="Stateid"
                            DataSourceID="SqlDataSource2" runat="server" />
                        <asp:SqlDataSource runat="server" ID="sqlDataSource2" ConnectionString="<%$ ConnectionStrings:abc %>"
                            SelectCommand="SELECT * FROM State"
                            FilterExpression="Countryid = '{0}'">
                            <FilterParameters>
                                <asp:ControlParameter Name="categoryParam" ControlID="ddlCategories" PropertyName="SelectedValue" />
                            </FilterParameters>
                        </asp:SqlDataSource>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>

 -----------------------------------------------------------------------------------
WEB.CONFIG
<?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
       <connectionStrings>
              <add name="abc" connectionString="Data Source=KCLINK-PC\SQLEXPRESS;Initial Catalog=kclink;Integrated Security=True" providerName="System.Data.SqlClient"/>
       </connectionStrings>
       <system.web>
              <compilation debug="true" targetFramework="4.0"/>
       </system.web>
</configuration>