0

私はこれに取り組んできました、そしてそれは働きます。ただし、オプションを制御ドロップダウンリストから変更して、動的リストを表示するはずのドロップダウン内の別のリストを表示しようとしても、何も起こりません。それでも前のリストが表示されます。私はこれでいくつかの助けを得ることができますか?:

これが私のフロントサイドです:

         <label for="ddlGift1">Gift #1</label>
         <asp:DropDownList runat="server" ID="ddlGift1" class="gift" AutoPostBack="true"  AppendDataBoundItems="True" ClientIDMode="Static" OnSelectedIndexChanged="ddlGift1_SelectedIndexChanged"> 
             <asp:ListItem Value="--" Text="Please Select" />
         </asp:DropDownList>  

         <label for="ddlFName1">Fund Name</label>
         <asp:DropDownList runat="server" ID="ddlFName1" AutoPostBack="true"  AppendDataBoundItems="True" ClientIDMode="Static"  > 
             <asp:ListItem Value="--" Text="Please Select" />
         </asp:DropDownList>


         <label for="ddlGift2">Gift #2</label>
         <asp:DropDownList runat="server" ID="ddlGift2" class="gift" AutoPostBack="true"  AppendDataBoundItems="True" ClientIDMode="Static" OnSelectedIndexChanged="ddlGift2_SelectedIndexChanged" > 
             <asp:ListItem Value="--" Text="Please Select" />
         </asp:DropDownList>   

         <label for="ddlFName2">Fund Name</label>
         <asp:DropDownList runat="server" ID="ddlFName2" AutoPostBack="true"  AppendDataBoundItems="True" ClientIDMode="Static" > 

         <label for="txtPAmount2">Pledge Amount</label>
         <asp:TextBox runat="server" ID="txtPAmount2" CssClass="narrow"  text="0.00" onClick="ComputeCosts();" ClientIDMode="Static" class="PAmount" />             

         <label for="ddlGift3">Gift #3</label>
         <asp:DropDownList runat="server" ID="ddlGift3" class="gift" AutoPostBack="true"  AppendDataBoundItems="True" ClientIDMode="Static" OnSelectedIndexChanged="ddlGift3_SelectedIndexChanged"> 
             <asp:ListItem Value="--" Text="Please Select" />
         </asp:DropDownList>  

         <label for="ddlFName3">Fund Name</label>
         <asp:DropDownList runat="server" ID="ddlFName3" AutoPostBack="true"  AppendDataBoundItems="True" ClientIDMode="Static" > 
             <asp:ListItem Value="--" Text="Please Select" />
         </asp:DropDownList>

裏側:1つの関数の例:

private void SetCOB(DropDownList name)
      {
           var _db = (from a in _foundation.COB
                     orderby a.id
                     select new { a.id, a.name });

        name.DataSource = _db;
        name.DataTextField = "name";
        name.DataValueField = "id";
        name.DataBind();
     }
 protected void ddlGift1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if(ddlGift1.SelectedValue == "41")
        {
            SetCOB(ddlFName1);
        }
        else if (ddlGift1.SelectedValue == "42")
        {
            SetCOLA(ddlFName1);
        }
        else if (ddlGift1.SelectedValue == "43")
        {
            SetCONHP(ddlFName1);
        }
        else if (ddlGift1.SelectedValue == "44")
        {
            SetOE(ddlFName1);
        }
        else if (ddlGift1.SelectedValue == "45")
        {
            SetCSEE(ddlFName1);
        }
        else if (ddlGift1.SelectedValue == "52")
        {
            SetA(ddlFName1);
        }
        else if (ddlGift1.SelectedValue == "54")
        {
            SetS(ddlFName1);
        }
    }

    protected void ddlGift2_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlGift2.SelectedValue == "41")
        {
            SetCOB(ddlFName2);
        }
        else if (ddlGift2.SelectedValue == "42")
        {
            SetCOLA(ddlFName2);
        }
        else if (ddlGift2.SelectedValue == "43")
        {
            SetCONHP(ddlFName2);
        }
        else if (ddlGift2.SelectedValue == "44")
        {
            SetOE(ddlFName2);
        }
        else if (ddlGift2.SelectedValue == "45")
        {
            SetCSEE(ddlFName2);
        }
        else if (ddlGift2.SelectedValue == "52")
        {
            SetA(ddlFName2);
        }
        else if (ddlGift2.SelectedValue == "54")
        {
            SetS(ddlFName2);
        }
    }

    protected void ddlGift3_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlGift3.SelectedValue == "41")
        {
            SetCOB(ddlFName3);
        }
        else if (ddlGift3.SelectedValue == "42")
        {
            SetCOLA(ddlFName3);
        }
        else if (ddlGift3.SelectedValue == "43")
        {
            SetCONHP(ddlFName3);
        }
        else if (ddlGift3.SelectedValue == "44")
        {
            SetOE(ddlFName3);
        }
        else if (ddlGift3.SelectedValue == "45")
        {
            SetCSEE(ddlFName3);
        }
        else if (ddlGift3.SelectedValue == "52")
        {
            SetA(ddlFName3);
        }
        else if (ddlGift3.SelectedValue == "54")
        {
            SetS(ddlFName3);
        }
    }
4

2 に答える 2

0

メソッドSetCOBは、ドロップダウンリストの選択された値に基づいてクエリを実行する必要があります。現在、すべてをクエリしています。

このようなものでなければなりません-

protected void ddlGift1_SelectedIndexChanged(object sender, EventArgs e)
{
    if(ddlGift1.SelectedValue == "41")
    {
       SetCOB(41, ddlFName1);
    }
    .....
}

private void SetCOB(int id, DropDownList name)
{
    var _db = (from a in _foundation.COB
       where a.id == id 
      orderby a.id
      select new { a.id, a.name });
    .....
}
于 2013-03-26T17:20:26.607 に答える
0

LINQを使用すると、Linqクエリを使用して選択した国に基づいて都市を表示するこの例のように、2番目のドロップダウンリストデータソースを条件付きで選択できます。

HTML ASP.Net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style>
        .required:after {
            content: " * ";
            font-weight: bold;
            color: red;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table>
                <tr>
                    <td>Country:</td>
                    <td>
                        <asp:DropDownList ID="ddlCountries" runat="server" AutoPostBack="true" 
                            OnSelectedIndexChanged="ddlCountries_SelectedIndexChanged">
                        </asp:DropDownList>
                    </td>
                </tr>
                <tr>
                    <td>City:</td>
                    <td>
                        <asp:DropDownList ID="ddlCities" runat="server">
                        </asp:DropDownList>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

C#の背後にあるコード

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication2.Models;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetData();

            }
        }

        private List<City> SetCityData()
        {
            List<City> cities = new List<City>()
            {
                new City() { Id = 1, CountryId = 2, CityName = "Amman"  },
                new City() { Id = 1, CountryId = 2, CityName = "Zarqa"  },
                new City() { Id = 1, CountryId = 4, CityName = "Istanbul"  },
                new City() { Id = 1, CountryId = 4, CityName = "Ankara"  },
                new City() { Id = 1, CountryId = 4, CityName = "Mersin"  },
                new City() { Id = 1, CountryId = 4, CityName = "Trabzon"  },
                new City() { Id = 1, CountryId = 1, CityName = "Rio de Janeiro"  },
                new City() { Id = 1, CountryId = 3, CityName = "Los Angeles"  },
                new City() { Id = 1, CountryId = 3, CityName = "Chicago"  },
            };
            return cities;
        }

        protected void ddlCountries_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedCountry = Convert.ToInt32(ddlCountries.SelectedValue);
            var cityByCountry = from c in SetCityData()
                                where c.CountryId == selectedCountry
                                select c;
            ddlCities.DataSource = cityByCountry.ToList();
            ddlCities.DataTextField = "CityName";
            ddlCities.DataValueField = "Id";
            ddlCities.DataBind();
        }

        private List<Country> SetCountries()
        {
            List<Country> countries = new List<Country>()
            {
                new Country() { Id = 1, CountryName = "Brazil"  },
                new Country() { Id = 2, CountryName = "Jordan" },
                new Country() { Id = 3, CountryName = "USA" },
                new Country() { Id = 4, CountryName = "Turkey" }
            };
            return countries;
        }

        private void GetData()
        {
            ddlCountries.DataSource = SetCountries();
            ddlCountries.DataTextField = "CountryName";
            ddlCountries.DataValueField = "Id";
            ddlCountries.DataBind();
        }
    }
}

国と都市のクラス

using System;

namespace WebApplication2.Models
{
    public class Country
    {
        public int Id { get; set; }
        public string CountryName { get; set; }
    }
}

using System;

namespace WebApplication2.Models
{
    public class City
    {
        public int Id { get; set; }
        public int CountryId { get; set; }
        public string CityName { get; set; }

    }
}
于 2017-01-10T18:29:24.977 に答える