2016-10-03 14 views
0

テーブルwhith列にID、名前、攻撃、SortOfWeaponがある場合。ドロップダウンリストのText = "Name"とValue = "Id"。ボタンをクリックすると、SelectedItem(国と郵便番号)の他のプロパティを取ることができますか?SelectedItem ASP.NETのドロップダウンリストのプロパティ

ユーザがアイテムを選択してボタンをクリックすると、選択されたアイテムのプロパティ値を関数内で指定する必要があります。背後にあるコードでは

<asp:DropDownList ID="DropDownListOfWeapon" runat="server" OnSelectedIndexChanged="DropDownListOfWeapon_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> 
    <br /><br /> 
    <asp:Literal ID="Literal1" runat="server"></asp:Literal> 

+1

参照してくださいます。http:/ /stackoverflow.com/questions/38150214/asp-net-how-to-create-cascading-dropdownlists-boxes-using-single-data-table/39230066#39230066?あなたの質問はよく似ています – VDWWD

+0

他の値をとってどういう意味ですか?カスケードするドロップダウンを探していますか(最初の項目を選択し、関連するデータを2番目に入れるために使用されます)、またはドロップダウンリストで項目を選択して、すべてのプロパティ選択したアイテムの コードサンプルが武器に関するものであれば、なぜあなたの質問について書いていますか?誰もがあなたに関連する例を与えるのが難しくなります! – Matt

+0

@VDWWD申し訳ありませんが、私はあなたの例を理解していません。 selectedItemのプロパティにどのように適用できますか? Webformsでは、DataSourseとしてList <>を使用すると、SelectedItemのすべてのプロパティに適用できます。 – cruim

答えて

1

は、私はあなたがaspxページで(ただし、わからない100%)このような何か

を探していると思う

protected void Page_Load(object sender, EventArgs e) 
    { 
     //check for postback 
     if (!IsPostBack) 
     { 
      //fill the dropdown 
      DropDownListOfWeapon.DataSource = GetWeapons(); 
      DropDownListOfWeapon.DataTextField = "Name"; 
      DropDownListOfWeapon.DataValueField = "Id"; 
      DropDownListOfWeapon.DataBind(); 

      //add a select text at the first position 
      DropDownListOfWeapon.Items.Insert(0, new ListItem("Select a weapon", "-1", true)); 
     } 
    } 

    private List<Weapon> GetWeapons() 
    { 
     //create a new list 
     List<Weapon> weaponList = new List<Weapon>(); 

     //fill the list with dummy weapons 
     //this probly would come from a database or other external source 
     for (int i = 0; i < 10; i++) 
     { 
      Weapon weapon = new Weapon(); 
      weapon.Id = i; 
      weapon.Name = "Weapon " + i; 
      weapon.Attack = "Attack " + i; 
      weapon.SortOfWeapon = "Sort of weapon " + i; 
      weaponList.Add(weapon); 
     } 

     //sort the list alphabetically 
     weaponList = weaponList.OrderBy(x => x.Name).ToList(); 

     return weaponList; 
    } 

    protected void DropDownListOfWeapon_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     //get the id from the dropdown 
     int Id = Convert.ToInt32(DropDownListOfWeapon.SelectedValue); 

     if (Id < 0) 
     { 
      //clear the literal when no selection is made 
      Literal1.Text = ""; 
     } 
     else 
     { 
      //get the correct weapon from the list based on it's id using Linq 
      List<Weapon> weaponList = GetWeapons().Where(x => x.Id == Id).ToList(); 

      //show the properties in the literal 
      Weapon weapon = weaponList[0]; 
      Literal1.Text = weapon.Id + "<br />"; 
      Literal1.Text += weapon.Name + "<br />"; 
      Literal1.Text += weapon.Attack + "<br />"; 
      Literal1.Text += weapon.SortOfWeapon + "<br />"; 
     } 
    } 

    //the weapon class 
    class Weapon 
    { 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Attack { get; set; } 
     public string SortOfWeapon { get; set; } 
    } 
+0

私は何を探しています、ありがとう!(私はあなたの答えに投票できませんでした - ごめんなさい - 低レポ) – cruim

関連する問題