2011-09-23 61 views

回答

4

添加到您的代碼:

DropDownList changesList = new DropDownList(); 

ListItem item; 
item = new ListItem(); 
item.Text = "go to google.com"; 
item.Value = "http://www.google.com"; 

changesList.Items.Add(item); 
changesList.Attributes.Add("onChange", "location.href = this.options[this.selectedIndex].value;"); 
+0

謝謝〜它的工作 – draw

0

首先聲明的下拉列表

<asp:DropDownList ID="ddlDestination" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged"> 
    <asp:ListItem Text="Select Destination" Selected="True" /> 
    <asp:ListItem Text="Go To Google.com" Value="http://www.google.com" /> 
    <asp:ListItem Text="Go To Yahoo.com" Value="http://www.yahoo.com" /> 
    <asp:ListItem Text="Go To stackoverflow.com" Value="http://www.stackoverflow.com" /> 
</asp:DropDownList> 

其次,在背後把這段代碼的代碼

protected void ddl_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    if (ddlDestination.SelectedIndex > 0) 
    { 
     string goToWebsite = ddlDestination.SelectedValue; 
     Response.Redirect(goToWebsite); 
    } 
} 

希望這有助於

+1

這可能有所幫助。但我的問題是'DropDownList的ListItem是動態添加的?''謝謝你。 – draw