Adding an data bindable attribute to option tag in ListBox

  • Thread starter Thread starter Chuck Dascalos
  • Start date Start date
C

Chuck Dascalos

Say for instance I had data like this...

BandID BestYear BandName
------ -------- --------
1 1991 Depeche Mode
2 1989 New Order
3 1985 The Smiths
etc...

I would like to use the asp:listbox control to display this data in its
generated html. The BandID would be the value of the row and BandName
would be what is displayed. I would however also like to add an attribute
to the <option> tag, so the final generated html would look something like
this...

<select name="GreatBandsListBox" id="GreatBandsListBox">
<option value="1" BestYear="1991">Depeche Mode</option>
<option value="2" BestYear="1989">New Order</option>
<option value="3" BestYear="1985">The Smiths</option>
</select>

I realize this is going to probably entail creating a custom control. I
see ways to add static attributes to the select tag. I want to add a data
bindable attribute to the option tag.

Any help would be greatly appreciated.
 
Hi Chuck,

Rather than getting into non-standard HTML attributes, you might want to
consider including all of your data in the value attribute, separated by a
pipe (|) character. You can retrieve the separate values using the Split()
function.

Here's an example:

<form id="Form1" method="post" runat="server">
<P>
<asp:ListBox id="GreatBandsListBox" runat="server" AutoPostBack="True">
<asp:ListItem Value="1|1991">Depeche Mode</asp:ListItem>
<asp:ListItem Value="2|1989">New Order</asp:ListItem>
<asp:ListItem Value="3|1985">The Smiths</asp:ListItem>
</asp:ListBox></P>
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>
<P>
<asp:Label id="Label2" runat="server"></asp:Label></P>
</form>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If IsPostBack Then
Dim strYear() As String = _
Split(GreatBandsListBox.SelectedValue.ToString, "|")
Label1.Text = UBound(strYear).ToString
Label1.Text = strYear(0).ToString
Label2.Text = strYear(1).ToString
End If
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]
 
Thanks Ken. That is a good idea I didn't think of. I will give that a
try. Just starting my first asp.net project so I will probably have
more questions in the future.

Thanks again!

Hi Chuck,

Rather than getting into non-standard HTML attributes, you might want
to consider including all of your data in the value attribute,
separated by a pipe (|) character. You can retrieve the separate
values using the Split() function.

Here's an example:

<form id="Form1" method="post" runat="server">
<P>
<asp:ListBox id="GreatBandsListBox" runat="server"
AutoPostBack="True">
<asp:ListItem Value="1|1991">Depeche Mode</asp:ListItem>
<asp:ListItem Value="2|1989">New Order</asp:ListItem>
<asp:ListItem Value="3|1985">The Smiths</asp:ListItem>
</asp:ListBox></P>
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>
<P>
<asp:Label id="Label2" runat="server"></asp:Label></P>
</form>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If IsPostBack Then
Dim strYear() As String = _
Split(GreatBandsListBox.SelectedValue.ToString, "|")
Label1.Text = UBound(strYear).ToString
Label1.Text = strYear(0).ToString
Label2.Text = strYear(1).ToString
End If
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]


Chuck Dascalos said:
Say for instance I had data like this...

BandID BestYear BandName
------ -------- --------
1 1991 Depeche Mode
2 1989 New Order
3 1985 The Smiths
etc...

I would like to use the asp:listbox control to display this data in
its generated html. The BandID would be the value of the row and
BandName would be what is displayed. I would however also like to
add an attribute to the <option> tag, so the final generated html
would look something like this...

<select name="GreatBandsListBox" id="GreatBandsListBox">
<option value="1" BestYear="1991">Depeche Mode</option>
<option value="2" BestYear="1989">New Order</option>
<option value="3" BestYear="1985">The Smiths</option>
</select>

I realize this is going to probably entail creating a custom control.
I see ways to add static attributes to the select tag. I want to
add a data bindable attribute to the option tag.

Any help would be greatly appreciated.
 
Hi Chuck,

Glad to help. Drop by any time! We've got a very friendly group here.

Ken
MVP [ASP.NET]

Chuck Dascalos said:
Thanks Ken. That is a good idea I didn't think of. I will give that a
try. Just starting my first asp.net project so I will probably have
more questions in the future.

Thanks again!

Hi Chuck,

Rather than getting into non-standard HTML attributes, you might want
to consider including all of your data in the value attribute,
separated by a pipe (|) character. You can retrieve the separate
values using the Split() function.

Here's an example:

<form id="Form1" method="post" runat="server">
<P>
<asp:ListBox id="GreatBandsListBox" runat="server"
AutoPostBack="True">
<asp:ListItem Value="1|1991">Depeche Mode</asp:ListItem>
<asp:ListItem Value="2|1989">New Order</asp:ListItem>
<asp:ListItem Value="3|1985">The Smiths</asp:ListItem>
</asp:ListBox></P>
<P>
<asp:Label id="Label1" runat="server"></asp:Label></P>
<P>
<asp:Label id="Label2" runat="server"></asp:Label></P>
</form>

Private Sub Page_Load _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
If IsPostBack Then
Dim strYear() As String = _
Split(GreatBandsListBox.SelectedValue.ToString, "|")
Label1.Text = UBound(strYear).ToString
Label1.Text = strYear(0).ToString
Label2.Text = strYear(1).ToString
End If
End Sub

Does this help?

Ken
Microsoft MVP [ASP.NET]


Chuck Dascalos said:
Say for instance I had data like this...

BandID BestYear BandName
------ -------- --------
1 1991 Depeche Mode
2 1989 New Order
3 1985 The Smiths
etc...

I would like to use the asp:listbox control to display this data in
its generated html. The BandID would be the value of the row and
BandName would be what is displayed. I would however also like to
add an attribute to the <option> tag, so the final generated html
would look something like this...

<select name="GreatBandsListBox" id="GreatBandsListBox">
<option value="1" BestYear="1991">Depeche Mode</option>
<option value="2" BestYear="1989">New Order</option>
<option value="3" BestYear="1985">The Smiths</option>
</select>

I realize this is going to probably entail creating a custom control.
I see ways to add static attributes to the select tag. I want to
add a data bindable attribute to the option tag.

Any help would be greatly appreciated.
 
Back
Top