how do I add an custom attribute to a listitem inside a dynamically created listbox?

  • Thread starter Thread starter Rick Spiewak
  • Start date Start date
R

Rick Spiewak

Adding attributes to the listitems doesn't actually do anything, even though
you can write the code that seems to do it. I would suggest just retrieving
the value from the table as needed.
 
Hi everyone, I have a asp.net listbox (lstAvailable) that is populated via
as ADO DataSet.

[vbcode]

lstAvailable.DataSource = oDs

lstAvailable.DataTextField = oDs.Tables(0).Columns(0).ToString

lstAvailable.DataBind()

[/vbcode]

This works fine, but what I would like to do is for each item in the listbox
associated a new attribute to it. Given that each item in the list box has a
Text attribute and a Value attribute, I would like to add another attribute
called PersonID.

I tried this

lstAvailable.Attributes.Add("ContributorType",
oDs.Tables(0).Columns("PersonID").ToString)

But this adds the attribute to the listbox not to each individual item in
the list..

Thanks for any help in advance

Mark
 
Hi Rick, thanks for your help, yep, gettring the info from the database was
my second option but I was just trying to save a dbase call
Cheers
Mark

Rick Spiewak said:
Adding attributes to the listitems doesn't actually do anything, even though
you can write the code that seems to do it. I would suggest just retrieving
the value from the table as needed.

Hi everyone, I have a asp.net listbox (lstAvailable) that is populated via
as ADO DataSet.

[vbcode]

lstAvailable.DataSource = oDs

lstAvailable.DataTextField = oDs.Tables(0).Columns(0).ToString

lstAvailable.DataBind()

[/vbcode]

This works fine, but what I would like to do is for each item in the listbox
associated a new attribute to it. Given that each item in the list box
has
a
Text attribute and a Value attribute, I would like to add another attribute
called PersonID.

I tried this

lstAvailable.Attributes.Add("ContributorType",
oDs.Tables(0).Columns("PersonID").ToString)

But this adds the attribute to the listbox not to each individual item in
the list..

Thanks for any help in advance

Mark
 
Instead of using -- asp:listbox, use HTML SELECT.

<SELECT size="10" id="nGroup" name="nGroup" runat="server">
</SELECT>


Declare the control in code behind page --
Protected WithEvents nGroup As System.Web.UI.HtmlControls.HtmlSelect

Then add attributes to the listitems as
ls.Attributes.Add("key", .....)


Hope that helps
Kiran
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top