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?
– novic
Yes, it’s webforms, but the full code I can’t post, malz ai...?
– Mr. Mister
you use values only in server-side or client-side as well? It is information only in programming?
– novic
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.
– Mr. Mister
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 aViewState
page itself. I ask for each item are many settings?– novic
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!
– Mr. Mister
You used the answer?
– novic
Thanks @Virgilio, in the rush I ended up using a solution not so elegant, but it was very useful in another situation.
– Mr. Mister