相互依赖的下拉菜单

本文关键字:下拉菜单 依赖 | 更新日期: 2023-09-27 18:16:22

我有三个下拉菜单,使用存储过程来填充我的gridview。我需要第一个设施下拉菜单,根据选择的设施,在第二个源类型下拉菜单中提供不同的选项。有些设施需要看到所有选项,有些只需要1或2个。我不知道该怎么做。

下面是下拉列表和存储过程的代码。

如有任何帮助,不胜感激

private void BindGrid()
{
    //set up arguments for the stored proc
    int? FacilityID = (ddlFacility.SelectedValue.Equals("-1")) ? (int?)null : int.Parse(ddlFacility.SelectedValue);
    int? SourceTypeID = int.Parse(ddlSource.SelectedValue);
    int? StatusTypeID = int.Parse(ddlStatusType.SelectedValue);
    //bind
    ObjectResult<models.MS_spGetMatchCross_Result> ds = this.DataLayer.model.MS_spGetMatchCross(FacilityID, SourceTypeID, StatusTypeID);
    gvResults.DataSource = ds;
    gvResults.DataBind();
}
private void ResetForm()
{
    try
    {
        //facility dropdown
        ddlFacility.Items.Clear();
        ddlFacility.DataSource = this.DataLayer.model.MS_spGetFacilityInfo(null).OrderBy(x => x.FacilityName);
        ddlFacility.DataTextField = "FacilityName";
        ddlFacility.DataValueField = "FacilityID";
        ddlFacility.DataBind();
        ddlFacility.Items.Insert(0, new ListItem("Select a facility...", "-1"));
        //SourceType dropdown
        ddlSource.Items.Clear();
        ddlSource.DataSource = this.DataLayer.model.SourceTypes;
        ddlSource.DataTextField = "Description";
        ddlSource.DataValueField = "SourcetypeID";
        ddlSource.DataBind();
        //Match Status dropdown
        ddlStatusType.Items.Clear();
        ddlStatusType.DataSource = this.DataLayer.model.StatusTypes;
        ddlStatusType.DataTextField = "Description";
        ddlStatusType.DataValueField = "StatusTypeID";
        ddlStatusType.DataBind();
        BindGrid();
    }
    catch (Exception ex)
    {
        this.SetMessage(ex.ToString(), PageMessageType.Error);
        AISLogger.WriteException(ex);
    }
}

相互依赖的下拉菜单

处理DropDownList的OnSelectedIndexChanged事件是一种非常直接的方法,虽然不是很优雅。要做到这一点,您必须确保其选择将影响另一个DDL的内容的每个DDL都将AutoPostBack属性设置为true(这确保当DDL中的某些内容更改时,该页将导致回发,从而允许您在服务器端代码中处理更改)。

在你的第一个DropDownList的OnSelectedIndexChanged事件处理程序中(在代码后面),你将有以下代码:

// Handle selected item or selected index
if(ddl1.SelectedItem.Text=="Selection1")
{
   // Get items for DDL2
   var items=GetDataForDdl2();
   // Bind the data
   DDL2.DataSource=items;
   DDL2.DataBind();
}

对于DDL2的SelectedIndexChanged事件处理程序,可以使用类似的逻辑来绑定DDL3的项,等等。

要获得开箱即用的功能,请查看ASP。. NET AJAX AjaxControlToolkit的CascadingDropDown: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx