2012-05-04 161 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> 

一个标签和其他下拉列表

<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

嗨,谢谢它的作品! :) –