2016-10-03 62 views
0

如果我有一个表,包括列Id,Name,Attack,SortOfWeapon。并在下拉列表中输入Text =“Name”和Value =“Id”。当我点击按钮,我可以采取SelectedItem(国家和邮编)的其他属性?SelectedItem的dropdownlist属性ASP.NET

protected void Page_Load(object sender, EventArgs e) 
    { 
     foreach (Weapon weapon in GetWeapons()) 
     { 
      DropDownListOfWeapon.Items.Add(weapon.Name); 
     } 

    } 
protected IEnumerable<Weapon> GetWeapons() 
    { 
     return weaponRepository.Weapons 
      .OrderBy(a => a.Name); 

    } 

当用户选择项目并单击按钮时,必须在该函数中提供所选项目的属性值。

+1

请访问http:/ /stackoverflow.com/questions/38150214/asp-net-how-to-create-cascading-dropdownlists-boxes-using-single-data-table/39230066#39230066?你的问题看起来相似 – VDWWD

+0

你的意思是采取其他的值是什么?您是否在寻找级联下拉菜单(因此您先选择一个项目,然后用相关数据填充另一项目),或者您是否希望选择下拉列表中的项目,然后访问所有属性选择的项目? 如果代码示例是关于武器的,为什么要写地址问题呢?让任何人都难以给你一个相关的例子! – Matt

+0

@VDWWD对不起,但我不明白你的例子。我怎么能适用于selectedItem的属性?在webforms中,如果我使用List <>作为DataSourse,我可以应用于SelectedItem的所有属性。 – cruim

回答

1

我认为你正在寻找(确保虽然不是100%),这样的事情

在aspx页面:

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

在后面的代码:

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

相关问题