views:

201

answers:

2

I have several DropDownLists on a form which are dynamically populated as they move down the form pulling data from a DB. The data is all HTMLEncoded so I need to HTMLDecode the data to display the text.

I created a method to do this and trigger it 'ondatabound' for each DDL

ondatabound="SortHTMLModel"

BUT whats annoying I have the same method just changing the DDL name on each one. I want a generic single method each DDL could call. Here is the one for the DDL called ddlfuel

protected void SortHTML(object sender, EventArgs e)
{
    foreach (ListItem item in ddlFuel.Items)
    {
        item.Text = Server.HtmlDecode(item.Text);
    }
}

And one for the DDL called ddlModel

protected void SortHTMLModel(object sender, EventArgs e)
{
    foreach (ListItem item in ddlModel.Items)
    {
        item.Text = Server.HtmlDecode(item.Text);
    }
}

You see my predicament! So annoying I just can't figure out the syntax for one method

A: 

Why can you not subclass the DropDownList control to do that before it renders the control? Then instead of using the stock DropDownList, you use your subclassed dropdownlist and the functionality happens automatically.

hova
Could you provide any examples? I'm still an intermediate .NET'er
leen3o
A: 

IIRC, the sender of an event is the actual control, so you could also say

protected void SortHTML(object sender, EventArgs e)
{
    foreach (ListItem item in ((DropDownList)sender).Items)
    {
        item.Text = Server.HtmlDecode(item.Text);
    }
}

and bind each DropDownList's DataBound event to SortHTML

Ruben
I'll give that a try... I thought the same, and tried sender.Items but it didn't work - I'll cast it as a dropdownlist and see if it works :)
leen3o