简体   繁体   中英

DropDownList SelectedItem attribute count always returns 0

In a DropDownList I've added attribute ie abcd while adding list items.

it is appearing after render in html page, when i try to access it in .cs file after postback it is not there.

ddl.SelectedItem.Attribute's count always returns 0.

How can I achieve this.

The attribute value should be Boolean (1,0 will be fine)

Following is the code to bind data to DDL

Private Sub LoadSetTypes()
    SetTypes.Items.Clear()

    Dim setTypesList As List(Of Entities.Sets.SetType) = Config.SetMetaData.GetSetTypes(setInformation)

    SetTypes.DataTextField = "SetTypeName"
    SetTypes.DataValueField = "SetTypeID"
    SetTypes.DataSource = setTypesList
    SetTypes.DataBind()
    SetTypesUpdatePanel.Update()

    Dim i As Int32 = 0
    'For Each setType As Entities.Sets.SetType In setTypesList
    For Each lm As ListItem In SetTypes.Items
        Dim setType As Entities.Sets.SetType = setTypesList(i)
        i = i + 1
        'Dim listItem As ListItem = New ListItem(setType.SetTypeName, setType.SetTypeID)
        If (setType.IsProfiled) Then
            lm.Attributes.Add("IsProfiled", "1")
        End If
        'SetTypes.Items.Add(listItem)
    Next

    SetTypes.SelectedValue = Nothing

    SetTypes.Items.Insert(0, "Please Choose")
    SetTypes.SelectedValue = 0
    SetTypesUpdatePanel.Update()
End Sub

custom attributes will not be sent back to server...you may use a HiddenFeild control instead to hold your data and change it with javascript and on server you will read the updated value

Edit :

On ItemDataBound( for the ddl) do this :

int i // will represent the index of the listitem.

var d = new Dicionary<int,string>();

  e.Item.Attributes["myAttr"] ="lalala"+i;// something dynamic
  d[i.ToString()]="lalala"+i; ( dictionary is serializable)
  viewState["myAttributes"]=d;

now when you want to read it :

var  myFirstIndexAttribute  = ((Dicionary<int,string>())viewState["myAttributes"])["0"];

you should use as instead.... well you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM