SelectedItem ASP.NET的dropdownlist属性

时间:2016-10-03 13:08:01

标签: c# asp.net

如果我有一个表,包括列Id,Name,Attack,SortOfWeapon。在下拉列表中Text =" Name"和值=" Id"。当我点击按钮时,我可以使用SelectedItem(Country和Zip)的其他属性吗?

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 个答案:

答案 0 :(得分: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; }
    }