C# 填充asp:dropdownlist-VS 2008

C# 填充asp:dropdownlist-VS 2008,c#,asp.net,visual-studio-2008,C#,Asp.net,Visual Studio 2008,我有一个名为DDL的表单 州和城市 声明: <asp:UpdatePanel ID="States" runat="server" UpdateMode="Conditional"> <Triggers> <asp:AsyncPostBackTrigger ControlID="States"EventName="SelectedIndexChanged" /> </Triggers>

我有一个名为DDL的表单

州和城市

声明:

<asp:UpdatePanel ID="States" runat="server" UpdateMode="Conditional">
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="States"EventName="SelectedIndexChanged" />
        </Triggers>
        <ContentTemplate>
            <asp:DropDownList ID="States" runat="server"
            AutoPostBack="True" DataSourceID="StatesObjectDataSource" 
            AppendDataBoundItems="true" 
                onselectedindexchanged="States_SelectedIndexChanged">
            <asp:ListItem Value="-1" Text="- None -"/>    
            </asp:DropDownList>
            <asp:ObjectDataSource ID="StatesObjectDataSource" runat="server" 
                onselecting="StatesObjectDataSource_Selecting" 
                SelectMethod="GetStates" 
                TypeName="Something">
            </asp:ObjectDataSource>
        </ContentTemplate>
    </asp:UpdatePanel>
我试着用这个来填充城市DDL

Cities.Items.Add(new ListItem(city,city));

但是,我看不到我的Cities DDL已填充

我建议在ViewState中创建一个包含物理对象集合的私有属性。然后将对象添加到该列表中,然后将对象列表数据绑定到下拉列表中

落后

<asp:DropDownList runat="server" ID="ddlCity" DataValueField="Key" DataTextField="Value">
</asp:DropDownList>
代码隐藏

private List<KeyValuePair<string, string>> ListData
{
    get { return (List<KeyValuePair<string, string>>) (ViewState["ListData"] ??     
                 (ViewState["ListData"] = new List<KeyValuePair<string, string>>())); }
    set { ViewState["ListData"] = value; }
}

protected void States_SelectedIndexChanged_SelectedIndexChanged(object sender, EventArgs e)
{
  ListData.Add(new KeyValuePair<string, string>(ddlCitys.SelectedValue, ddlCitys.SelectedValue));
  ddlCitys.DataSource = ListData;
  ddlCitys.DataBind();
}

get语句还对ListData属性采用延迟加载,因此在访问列表时不会遇到空引用异常。

我建议在ViewState中创建一个私有属性,用于保存物理对象的集合。然后将对象添加到该列表中,然后将对象列表数据绑定到下拉列表中

落后

<asp:DropDownList runat="server" ID="ddlCity" DataValueField="Key" DataTextField="Value">
</asp:DropDownList>
代码隐藏

private List<KeyValuePair<string, string>> ListData
{
    get { return (List<KeyValuePair<string, string>>) (ViewState["ListData"] ??     
                 (ViewState["ListData"] = new List<KeyValuePair<string, string>>())); }
    set { ViewState["ListData"] = value; }
}

protected void States_SelectedIndexChanged_SelectedIndexChanged(object sender, EventArgs e)
{
  ListData.Add(new KeyValuePair<string, string>(ddlCitys.SelectedValue, ddlCitys.SelectedValue));
  ddlCitys.DataSource = ListData;
  ddlCitys.DataBind();
}

get语句还对ListData属性采用延迟加载,因此在访问列表时不会遇到空引用异常。

如果可能,我建议使用而不是UpdatePanel。重新发明这个轮子是没有用的,Toolkit控件使用web服务而不是部分回发要快得多。

如果可能的话,我建议使用,而不是UpdatePanel。重新发明这个轮子是没有用的,工具箱控件使用web服务而不是部分回发要快得多。

将您的城市下拉列表放在更新面板中。

将您的城市下拉列表放在更新面板中。

Dave:我使用亚音速数据访问层获取数据,没有web服务。CascadingDropDown extender是否仍能与列表对象一起工作。另外,您能否解释一下为什么在更新面板上推荐此选项?您可以在创建其服务方法时将CascadingDropDown绑定到任何数据源。尽可能避免使用UpdatePanel,因为它在过度使用时会导致性能问题。只有在您确实必须查看状态重要信息等时才使用它们。戴夫:我使用亚音速数据访问层获取数据,没有webservice。CascadingDropDown extender是否仍能与列表对象一起工作。另外,您能否解释一下为什么在更新面板上推荐此选项?您可以在创建其服务方法时将CascadingDropDown绑定到任何数据源。尽可能避免使用UpdatePanel,因为它在过度使用时会导致性能问题。只有在您确实必须查看、声明重要信息等时才使用它们。