Adding additional properties to list items in a dropDownlist through inheritance

R

RSH

Hi,

I have a situation where I need to add several "Hidden" properties to list
items in a dropdownlist. By default the DropDownList item has two
properties with regards to the listitems collection, Text and Value. I need
to add a DivisionID, and DepartmentID.

I assumed i could simply override the ListItem class and add the additional
properties. is this in fact the case? If so what would the code look like?

Thanks in advance for any information you can provide.

Ron
 
M

Mark Rae

I have a situation where I need to add several "Hidden" properties to list
items in a dropdownlist. By default the DropDownList item has two
properties with regards to the listitems collection, Text and Value.

That's right.
I need to add a DivisionID, and DepartmentID.
OK.

I assumed i could simply override the ListItem class and add the
additional properties. is this in fact the case? If so what would the
code look like?

Run your web app and navigate to the page with the DropDownList - do a View
Source. The DropDownList webcontrol simply creates the <select> HTML tag,
and the various <option> elements in it. Whereas you could override the
ListItem class in the way you describe by adding additional properties,
ASP.NET would simply ignore them when creating the HTML markup to stream
down to the client.
Thanks in advance for any information you can provide.

Fortunately, this is quite easy to work around.

<asp:DropDownList ID="cmbTest" runat="server" />
<asp:Button ID="cmdTest" runat="server" Text="Test" OnClick=cmdTest_Click />

protected void Page_Load(object sender, EventArgs e)
{
ListItem objItem = null;

objItem = new ListItem();
objItem.Value = "1¬2¬3";
objItem.Text = "One";
cmbTest.Items.Add(objItem);

objItem = new ListItem();
objItem.Value = "4¬5¬6";
objItem.Text = "Two";
cmbTest.Items.Add(objItem);
}

protected void cmdTest_Click(object sender, EventArgs e)
{
string strValue = cmbTest.SelectedValue.Split('¬')[0];
string strDivision = cmbTest.SelectedValue.Split('¬')[1];
string strDepartment = cmbTest.SelectedValue.Split('¬')[2];
}
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top