I have a System.Collections.Generic.Dictionary<System.Web.UI.Control, object> where all keys can be either type of System.Web.UI.WebControls.HyperLink or type of System.Web.UI.WebControls.Label.
I want to change Text property of each control. Because HyperLink doesn't implement (why?!) ITextControl, I need to cast Label or HyperLink explicitly:
Dictionary<Control,object> dic = ..
dic
.Where(p => p.Key is HyperLink)
.ForEach(c => ((HyperLink)c).Text = "something")
dic
.Where(p => p.Key is Label)
.ForEach(c => ((Label)c).Text = "something")
Are there ways to workaround such approach?
Slightly more elegant, but retaining the problem:
foreach (HyperLink c in dic.Keys.OfType<HyperLink>())
{
c.Text = "something";
}
foreach (Label c in dic.Keys.OfType<Label>())
{
c.Text = "something";
}
You could create a class that derives from HyperLink and let your class inherit from ITextControl. Should be clear where to go from there...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With