如何为 asp.net 中的多项选择创建 SelectIndexChanged 事件

How to create SelectIndexChanged event for multiple selection in asp.net

我想 select 复选框列表中的多个项目,然后在 select 索引更改第一个复选框列表的事件上填充另一个复选框列表,但在一个 selection 之后它不是由于 "Auto Post Back = true" 而正在处理另一个 selected 项目,在调试时我可以看到指针从页面加载事件开始。我想在所有项目的 selection 之后它应该是 fire "ddlregion_SelectedIndexChanged" 这样我就可以在另一个复选框列表中看到所有 selected 项目相关的值。

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindRegion();
            }
        }

因此它无法为另一个 selected 项目获取数据如何解决这个问题请帮助我。

ddlRegion代码绑定:

public void BindRegion()
        {
            OracleCommand Cmd = new OracleCommand("select * from regions", con);
            Cmd.CommandType = CommandType.Text;
            OracleDataAdapter da = new OracleDataAdapter();
            DataSet ds = new DataSet();
            da.SelectCommand = Cmd;
            da.Fill(ds);
            ddlregion.DataSource = ds;
            ddlregion.DataTextField = "REGION_DESC";
            ddlregion.DataValueField = "REGION_CODE";
            ddlregion.DataBind();

        }

Select 索引更改事件

 protected void ddlregion_SelectedIndexChanged(object sender, EventArgs e)
 {
  ddlDepot.Items.Clear();
  ddlDepot.Items.Add(new ListItem("--Select Depot--", ""));
  foreach (ListItem item in ddlregion.Items)
   {

   if (item.Selected == true)
   {
string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr, depots d where r.region_code = sr.region_code and sr.sub_region_code = d.sub_region_code and active = 'Y' and r.region_code in " + ddlregion.SelectedValue + "";
                    OracleCommand Cmd = new OracleCommand(str, con);
                    Cmd.CommandType = CommandType.Text;
                    OracleDataAdapter da = new OracleDataAdapter();
                    DataSet ds = new DataSet();
                    da.SelectCommand = Cmd;
                    da.Fill(ds);
                    ddlDepot.DataSource = ds;
                    ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
                    ddlDepot.DataValueField = "DEPOT_CODE";
                    ddlDepot.DataBind();
                }
            }
        }

您需要为 SQL 建立区域列表,然后在完成后调用您的 SQL 和绑定。

 string regions = "";
 bool regionSelected = false;
  foreach (ListItem item in ddlregion.Items)
  {  
   if (item.Selected == true)
    {
        regions += item.SelectedValue + ",";
        regionSelected = true;
    }
 
  } 
     // looping done so clean up string for SQL
     regions = "(" + regions.TrimEnd(',') + " )";

 //only run if a region is selected
 if(regionSelected)
 {
 string str = "select d.depot_code, d.depot_description from regions r, sub_regions sr, 
 depots d where r.region_code = sr.region_code and      
 sr.sub_region_code = d.sub_region_code and active = 'Y' and  r.region_code    
 in " + regions;
                OracleCommand Cmd = new OracleCommand(str, con);
                Cmd.CommandType = CommandType.Text;
                OracleDataAdapter da = new OracleDataAdapter();
                DataSet ds = new DataSet();
                da.SelectCommand = Cmd;
                da.Fill(ds);
                ddlDepot.DataSource = ds;
                ddlDepot.DataTextField = "DEPOT_DESCRIPTION";
                ddlDepot.DataValueField = "DEPOT_CODE";
                ddlDepot.DataBind();
 }