0

asp.netで動的ドロップダウンリストを作成するにはどうすればよいですか?

ドロップダウンリストがあります:

<asp:DropDownList ID="FirstParameter" runat="server">
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
    <asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>  

ラベルとその他のドロップダウンリスト

<asp:Label ID="SecondParameter_Label" runat="server" Text=""></asp:Label>

<asp:DropDownList ID="SecondParameter" runat="server">
</asp:DropDownList> 

ドロップダウンリストの選択を変更すると、選択に基づいて上FirstParameterのテキストが変更されるはずです。したがって、ドロップダウンリストの場合、最初のドロップダウンリストで選択した値に基づいてデータフィルターにバインドする必要があります。SecondParameter_LabelSecondParameter

このコードを試しましたが、機能しません。

Protected Sub FirstParameter_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles FirstParameter.SelectedIndexChanged
    'Change the label text
    SecondParameter_Label.Text = ReportType_Dropdownlist.SelectedItem.Value

    'Bind value to the SecondParameter dropdownlist
    SecondParameter.DataSource = dataTableName '--> Assumed that I already have the DataTable called from a class
    SecondParameter.DataTextField = "NameofDatabaseColumn"
    SecondParameter.DataValueField = "NameofDatabaseColumn"
    SecondParameter.DataBind()
End Sub

最初にテキストを変更しようとしましたSecondParameter_Labelが、それでも機能しません。どうやってやるの?

4

1 に答える 1

1

ドロップダウンにAutoPostBackプロパティを追加してみてください。また、サーバー側でそれらを処理する必要があります。最初のドロップダウンは次のようになります。

<asp:DropDownList ID="FirstParameter" runat="server" AutoPostBack="true">
    <asp:ListItem Value="1">1</asp:ListItem>
    <asp:ListItem Value="2">2</asp:ListItem>
    <asp:ListItem Value="3">3</asp:ListItem>
</asp:DropDownList>  
于 2012-05-04T06:15:13.763 に答える