Reputation: 9485
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
Upvotes: 1
Views: 1246
Reputation: 15545
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
Upvotes: 1
Reputation: 2841
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.
Upvotes: 1