【问题标题】:dropdownlist properties of SelectedItem ASP.NETSelectedItem ASP.NET 的下拉列表属性
【发布时间】:2016-10-03 13:08:01
【问题描述】:

如果我有一个包含 Id、Name、Attack、SortOfWeapon 列的表。在下拉列表中 Text="Name" 和 Value="Id"。当我点击按钮时,我可以获取 SelectedItem(Country and 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);

    }

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

【问题讨论】:

  • 看到stackoverflow.com/questions/38150214/…?您的问题看起来很相似
  • 取其他值是什么意思?您是在寻找级联下拉列表,(因此您在第一个中选择一个项目,它用于用相关数据填充第二个项目),或者您是在寻找下拉列表中的一个项目,然后访问所有属性所选项目的?如果代码示例是关于武器的,为什么还要写关于地址的问题?让任何人都很难给你一个相关的例子!
  • @VDWWD 对不起,但我不明白你的例子。我如何适用于 selectedItem 的属性?在网络表单中,如果我使用 List 作为 DataSourse,我可以应用到 SelectedItem 的所有属性。
  • @Matt 对不起代码示例。是的,我想在下拉列表中选择一个项目,然后访问所选项目的所有属性。
  • 在这种情况下,您有 3 个选项(广义而言)。1)将所有对象数据添加到页面中(例如在 javascript 数组中,或作为一系列隐藏面板),然后当选项被选中,使用一些 javascript 来显示相关数据。 2)使用某种 ajax 解决方案,所以当一个项目被选中时,你会回调到服务器并要求它给你相关的对象 3)只需回发到服务器,然后用所选数据重新加载页面(甚至直接到包含信息的新页面。)很难说哪个最适合您。也许根据您的意图更新您的问题?

标签: c# asp.net


【解决方案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; }
    }

【讨论】:

  • 看来这就是我要找的东西,谢谢!(对不起,我不能为你的答案低回购投票)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-08
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 2011-10-29
相关资源
最近更新 更多