I created a folder in my solution named “UserControls”. That is where the following .ascx files are located:

StateFilter.ascx

<%@ Control Language=”C#” AutoEventWireup=”true” CodeBehind=”StateFilter.ascx.cs” Inherits=”Sandbox2010.UserControls.StateFilter” %>

<asp:DropDownList ID=”DdlState” runat=”server” DataSourceID=”DsState”
DataTextField=”Name” DataValueField=”StateProvinceID” AutoPostBack=”True”
onselectedindexchanged=”DdlState_SelectedIndexChanged”>
</asp:DropDownList>

<asp:SqlDataSource ID=”DsState” runat=”server”
ConnectionString=”<%$ ConnectionStrings:AdventureWorks2012_ConnectionString %>”
SelectCommand=”
select ” as StateProvinceID, ‘(Select a State)’ as Name
UNION ALL
SELECT DISTINCT StateProvinceID, Name
FROM Person.StateProvince
WHERE ISNULL(@CountryRegionCode, ”) = ” or CountryRegionCode = @CountryRegionCode ORDER BY Name”
onselecting=”DsState_Selecting”>
<SelectParameters>
<asp:Parameter Name=”CountryRegionCode” Type=”String” />
</SelectParameters>
</asp:SqlDataSource>

StateFilter.ascx.cs

using System;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Sandbox2010.UserControls
{
public partial class StateFilter : System.Web.UI.UserControl
{
public event SelectedIndexChangedEventHandler SelectedIndexChanged;
public delegate void SelectedIndexChangedEventHandler(object sender, SelectedIndexChangedEventArgs args);

public string CountryRegionCode { get; set; }

public ListItem SelectedItem
{
get { return DdlState.SelectedItem; }
}

public string SelectedValue
{
get { return DdlState.SelectedValue; }
}

[Description(“CSS Class”), Category(“Configuration”)]
public string CssClass { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
DdlState.CssClass = CssClass;
}

protected void DdlState_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.SelectedIndexChanged != null)
{
this.SelectedIndexChanged(this, new SelectedIndexChangedEventArgs(DdlState.SelectedItem));
}
}

protected void DsState_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.Parameters[“@CountryRegionCode”].Value = string.IsNullOrEmpty(CountryRegionCode) ? string.Empty : CountryRegionCode;
}
}
}

CountryFilter.ascx

<%@ Control Language=”C#” AutoEventWireup=”true” CodeBehind=”CountryFilter.ascx.cs” Inherits=”Sandbox2010.UserControls.CountryFilter” %>

<asp:DropDownList ID=”DdlCountry” runat=”server” DataSourceID=”DsCountry”
DataTextField=”Name” DataValueField=”CountryRegionCode” AutoPostBack=”True”
onselectedindexchanged=”DdlCountry_SelectedIndexChanged”>
</asp:DropDownList>

<asp:SqlDataSource ID=”DsCountry” runat=”server”
ConnectionString=”<%$ ConnectionStrings:AdventureWorks2012_ConnectionString %>”
SelectCommand=”
select ” as CountryRegionCode, ‘(Select a Country)’ as Name
UNION ALL
select distinct c.CountryRegionCode, c.Name
from
Person.CountryRegion as c
join Person.StateProvince as s
on c.CountryRegionCode = s.CountryRegionCode
order by Name
“>
</asp:SqlDataSource>

CountryFilter.ascx.cs

using System;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace Sandbox2010.UserControls
{
public partial class CountryFilter : System.Web.UI.UserControl
{
public event SelectedIndexChangedEventHandler SelectedIndexChanged;
public delegate void SelectedIndexChangedEventHandler(object sender, SelectedIndexChangedEventArgs args);

public string CountryRegionCode { get; set; }

public ListItem SelectedItem
{
get { return DdlCountry.SelectedItem; }
}

public string SelectedValue
{
get { return DdlCountry.SelectedValue; }
}

[Description(“CSS Class”), Category(“Configuration”)]
public string CssClass { get; set; }

protected void Page_Load(object sender, EventArgs e)
{
DdlCountry.CssClass = CssClass;
}

protected void DdlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.SelectedIndexChanged != null)
{
this.SelectedIndexChanged(this, new SelectedIndexChangedEventArgs(DdlCountry.SelectedItem));
}
}
}
}

I created a web form that contains my user controls and a text box that displays the final selected drop down list option:

WebForm1.aspx

<%@ Page Language=”C#” AutoEventWireup=”true” CodeBehind=”WebForm1.aspx.cs” Inherits=”Sandbox2010.WebForm1″ %>
<%@ Register Src=”UserControls/StateFilter.ascx” TagPrefix=”uc” TagName=”StateFilter”%>
<%@ Register Src=”UserControls/CountryFilter.ascx” TagPrefix=”uc” TagName=”CountryFilter”%>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head runat=”server”>
<title></title>
<link rel=”Stylesheet” href=”Styles/Rubrik.css” />
</head>
<body>
<form id=”form1″ runat=”server”>
<br /><br />
<uc:CountryFilter id=”CountryFilter1″ runat=”server” onselectedindexchanged=”CountryFilter1_SelectedIndexChanged” />
<br /><br />
<uc:StateFilter id=”StateFilter1″ runat=”server” onselectedindexchanged=”StateFilter1_SelectedIndexChanged” />
<br /><br />
You selected:<br />
<asp:TextBox ID=”MySelection” runat=”server”></asp:TextBox>
</form>
</body>
</html>

WebForm1.aspx.cs

using System;
using Sandbox2010.UserControls;

namespace Sandbox2010
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}

protected void CountryFilter1_SelectedIndexChanged(object sender, SelectedIndexChangedEventArgs e)
{
if (e.SelectedItem != null)
{
StateFilter1.CountryRegionCode = e.SelectedItem.Value;
StateFilter1.DataBind();
}
}

protected void StateFilter1_SelectedIndexChanged(object sender, SelectedIndexChangedEventArgs e)
{
if (e.SelectedItem != null)
{
MySelection.Text = e.SelectedItem.Text;
}
}
}
}

 

 

Advertisements