ASP.NET 繰り返しコントロール内で2つのDropDownListを連携する。

繰り返しコントロール(GridView,DataList,Repeater)などのItemTemplateにDropDownListを二つ配置し
一つ目のDropDownListの選択値によって二つ目のDropDownListのDataSourceを変更する方法です。

DataListのItemTemplateにDropDownListを二つ配置します。

<asp:DataList ID="DataList1" runat="server">
    <ItemTemplate>
        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" />
        <asp:DropDownList ID="DropDownList2" runat="server" />
    </ItemTemplate>
</asp:DataList>

DataListのItemCreatedイベントで一つ目のDropDownListにデータを設定し
DropDownListのSelectedIndexChangedイベントを関連付けます。
Private Sub DataList1_ItemCreated(sender As Object, e As DataListItemEventArgs) Handles DataList1.ItemCreated
    Dim ddl1 As DropDownList = DirectCast(e.Item.FindControl("DropDownList1"), DropDownList)
    With ddl1
        .DataValueField = "Cd"
        .DataTextField = "Name"
        .DataSource = GetDropDownList1Data()
        .DataBind()
    End With

    AddHandler ddl1.SelectedIndexChanged, AddressOf ddl1_SelectedIndexChanged
End Sub

一つ目のDropDownListのSelectedIndexChangedイベントでは
一つ目のDropDownListのParentプロパティからDropDownListItemを取得します。
取得したDropDownListItemからFindControlで二つ目のDropDownListはオブジェクトを探します。
Private Sub ddl1_SelectedIndexChanged(sender As Object, e As EventArgs)
    Dim ddl1 As DropDownList = DirectCast(sender, DropDownList)
    Dim ddl1Cd As Integer = CInt(ddl1.SelectedValue)

    Dim item As DataListItem = DirectCast(ddl1.Parent, DataListItem)

    Dim ddl2 As DropDownList = DirectCast(item.FindControl("DropDownList2"), DropDownList)
    With ddl2
        .DataValueField = "Cd"
        .DataTextField = "Name"
        .DataSource = GetDropDownList2Data(ddl1Cd)
        .DataBind()
    End With
End Sub

ASP.NET CustomValidatorでTextが空のTextBoxを検証する

昨晩からドハマりした。
空のテキストボックスをCustomValidatorで検証したい場合はValidateEmptyTextをtrueにしないといけないのね!
知らなかったよー
<asp:TextBox ID="TextBox1" runat="server" />
<asp:CustomValidator ID="CustomValidator1" runat="server" Display="Dynamic" ValidateEmptyText="true"
 ControlToValidate="TextBox1" ErrorMessage="エラーメッセージ"/>