ASP.NET - Access Dropdownlist item attribute in Code Behind

Asked

Viewed 545 times

1

I have a DropDownList in which I add items as below:

var item= new ListItem("Texto do Item", "Valor do Item");
item.Attributes.Add("NomeAtributo", "ValorAtributo");

The DropDownList is rendered on the screen with the attribute, until then cool. When I select an item in the DropDownList fires an event on code behind, where I want to get the attribute value again, however when I run the code below comes null:

var meuAtributo = meuDropDownList.SelectedItem.Attributes["NomeAtributo"];

In the documentation of Microsoft is written in the description of WebControl.Attributes:

Gets the collection of arbitrary attributes (for rendering only)


Translation:

Pega a coleção de atributos arbitrários (somente para renderização).

How do I get the value of this attribute DropDownList at the event OnSelectedIndexChanged?

  • is Webforms? has the complete code of this?

  • Yes, it’s webforms, but the full code I can’t post, malz ai...?

  • you use values only in server-side or client-side as well? It is information only in programming?

  • in the Onload (server-side) event I create the ddl with the values and attributes. In the Onselectedindexchanged (server-side) event I want to retrieve the attribute from the ddl item. So I create and use only on the server-side. The only thing that happens on the front end is to show the attribute in the rendered select options, but it has no change or processing or anything.

  • Here’s the thing, that way there’s no way, Attributes are only really to re-experience, which can be done to store the values in a ViewState page itself. I ask for each item are many settings?

  • It is an attribute for each item in the list, loading from the database the information and making a loop will be good...thanks for the solution!!! I will test!

  • You used the answer?

  • Thanks @Virgilio, in the rush I ended up using a solution not so elegant, but it was very useful in another situation.

Show 3 more comments

1 answer

1


Actually the only server attributes to render the page, in recovering the information these attributes are lost, but, there are ways to bypass this, one would be to save the information in the Viewstate

Minimal example:

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

    protected void Load_Drop()
    {
        var l1 = new ListItem("v1", "1");
        l1.Attributes.Add("a1", "r1");
        ViewState.Add("v1", "a1;r1");

        var l2 = new ListItem("v2", "2");
        l2.Attributes.Add("a2", "r2");
        ViewState.Add("v2", "a2;r2");

        DropDownList1.Items.Add(l1);
        DropDownList1.Items.Add(l2);            
        DropDownList1.DataBind();    

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        var drop = DropDownList1;
        int index = drop.SelectedIndex;
        if (index > -1)
        {
            ListItem item = drop.SelectedItem;
            string values = (string)ViewState[item.Text];                    
        }
    }
}

References

Browser other questions tagged

You are not signed in. Login or sign up in order to post.