下拉菜单相互依赖

时间:2022-02-25 01:22:59

I have three drop down menus that are using a store Procedure to populate my gridview. I need the first facility DropDown to give different options in the second source type dropdown depending on what facility is selected. Some facility's need to see all options and some only 1 or 2. I am unsure how to go about this.

我有三个下拉菜单,使用存储过程来填充我的gridview。我需要第一个工具DropDown在第二个源类型下拉列表中提供不同的选项,具体取决于所选的工具。有些设施需要查看所有选项,有些只需要1或2.我不确定如何解决这个问题。

Here is the code for the dropdowns and the store precedure.

这是下拉菜单和商店菜单的代码。

Any help would be greatly appreciated

任何帮助将不胜感激

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);
    }
}

1 个解决方案

#1


1  

A very straightforward, though not exactly elegant, way would be to handle the DropDownList's OnSelectedIndexChanged event. For that to work, you'd have to make sure that each DDL whose selection will affect the contents of another DDL has the AutoPostBack property set to true (this ensures when something in the DDL changes, the page will cause a postback, allowing you to handle the change in server-side code).

一种非常简单但不完全优雅的方法是处理DropDownList的OnSelectedIndexChanged事件。为了实现这一点,您必须确保选择将影响另一个DDL内容的每个DDL都将AutoPostBack属性设置为true(这可以确保当DDL中的某些内容发生更改时,该页面将导致回发,允许您处理服务器端代码的变化)。

In your first DropDownList's OnSelectedIndexChanged event handler (in the code-behind), you'll have the following code:

在你的第一个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();
}

You'd have similar logic for DDL2's SelectedIndexChanged event handler to then bind the items of DDL3, and so on.

您对DDL2的SelectedIndexChanged事件处理程序有类似的逻辑,然后绑定DDL3的项目,依此类推。

For something that works nicely out of the box, check out the ASP.NET AJAX AjaxControlToolkit's CascadingDropDown: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx

对于可以很好地开箱即用的东西,请查看ASP.NET AJAX AjaxControlToolkit的CascadingDropDown:http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx

#1


1  

A very straightforward, though not exactly elegant, way would be to handle the DropDownList's OnSelectedIndexChanged event. For that to work, you'd have to make sure that each DDL whose selection will affect the contents of another DDL has the AutoPostBack property set to true (this ensures when something in the DDL changes, the page will cause a postback, allowing you to handle the change in server-side code).

一种非常简单但不完全优雅的方法是处理DropDownList的OnSelectedIndexChanged事件。为了实现这一点,您必须确保选择将影响另一个DDL内容的每个DDL都将AutoPostBack属性设置为true(这可以确保当DDL中的某些内容发生更改时,该页面将导致回发,允许您处理服务器端代码的变化)。

In your first DropDownList's OnSelectedIndexChanged event handler (in the code-behind), you'll have the following code:

在你的第一个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();
}

You'd have similar logic for DDL2's SelectedIndexChanged event handler to then bind the items of DDL3, and so on.

您对DDL2的SelectedIndexChanged事件处理程序有类似的逻辑,然后绑定DDL3的项目,依此类推。

For something that works nicely out of the box, check out the ASP.NET AJAX AjaxControlToolkit's CascadingDropDown: http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx

对于可以很好地开箱即用的东西,请查看ASP.NET AJAX AjaxControlToolkit的CascadingDropDown:http://www.asp.net/ajaxLibrary/AjaxControlToolkitSampleSite/CascadingDropDown/CascadingDropDown.aspx