Retrieving value of a custom tag

R

RSH

I am testing a few concepts in preparation of a project. One of the
concepts revolves around a custom dropdownlist class. Basically I am
overriding the RenderContents and writing a custom value called
"CustomTestValue" in each option tag. I am wiring up the
OnSelectedIndexChanged to a Generic OnSelectedIndexChanged event handler.
My question is...Is it possible to get at the value of my custom value
during postback? And if so how?

Thanks!
Ron
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

'Put user code to initialize the page here

Dim liCol As ListItemCollection

Dim item As ListItem

liCol = New ListItemCollection

For i As Integer = 1 To 23

item = New ListItem

item.Text = i

item.Value = i

liCol.Add(item)

Next

Dim DDL As New CustomDDL(liCol)

DDL.AutoPostBack = True

AddHandler DDL.SelectedIndexChanged, AddressOf DDL_SelectedIndexChanged

PlaceHolder1.Controls.Add(DDL)

End Sub



Private Sub DDL_SelectedIndexChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs)

Label1.Text = sender.SelectedItem.Text

sender.selecteditem.text = sender.selecteditem.text

End Sub

End Class



Public Class CustomDDL

Inherits DropDownList

Sub New(ByVal liCol As ListItemCollection)

For Each item As ListItem In liCol

Dim li As New ListItem

li.Text = item.Text

li.Value = item.Value

Me.Items.Add(li)

Next

End Sub

Protected Overrides Sub RenderContents(ByVal writer As
System.Web.UI.HtmlTextWriter)

Me.Style.Add("Border", "5px")

For Each li As ListItem In Me.Items

If InStr(li.Text, "1") > 0 Then

writer.WriteLine("<option class='gray' ")

Else

writer.WriteLine("<option class='black' ")

End If

writer.WriteAttribute("value", li.Value.ToString())

writer.WriteAttribute("CustomTestValue", "TestValue" & li.Value.ToString())

writer.Write(HtmlTextWriter.TagRightChar)

writer.Write(li.Text & " - Modified")

writer.WriteLine("</option>")

writer.WriteLine()

Next

End Sub

End Class
 
R

RSH

How would I go about implementing that?

I have a few situations where it would be handly to store several different
values with the DropDownList options. Previously I was just creating a
listItem Value that comprised of a delimited string
(value='DivisionID--DepartmentID--LocationID--EmployeeID') and I would
perform a split on the returned value. But if I could include this type of
functionality in a class then i figured this would be a cleaner
implementation.

The other thing I was attempting to do was create a modified ListItem Class
but the ListItem class is un-inheritable.

Any suggestions would be greatly appreciated.

Thanks,
Ron
 

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

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top