2012-05-04 6 views
0

asp.netで動的ドロップダウンリストを作成するには?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> 

ラベルおよびその他のDropDownListの

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

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

私はFirstParameterドロップダウンリストの選択を変更し、テキスト上の

は私が ドロップダウンリストを持っています SecondParameter_Labelは選択に基づいて変更する必要があります。 SecondParameterのドロップダウンistは、最初のドロップダウンリストで選択した値に基づいてデータフィルタにバインドする必要があります。

私はこのコードを試してみましたが、それは動作しません:

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テキストを変更しようとしましたが、まだ動作しません。どうやってやるの?

答えて

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> 
+0

こんにちは、ありがとうございます! :) –

関連する問題