Reputation: 466
I need to set text property for any type controls(Ex. Textbox,Label,HyperLink..etc) from my web page dynamically. Here is my code
foreach (string id in List<IdCollection>)
{
Control ctrl = (this.Page.FindControl(id)); // Control should be HtmlGenericControl or WebControl.
ctrl.Text=???(This property is not available for Control Class)..
}
I dont need to check each and every control type for set text property as like as following codes
if(ctrl is TextBox)
{
((TextBox)ctrl).Text="test";
}
or
if(ctrl.GetType()==typeof(TextBox))
{
((TextBox)ctrl).Text="test";
}
Is there any other way to set text property as simple, just like as the following code
WebControl wbCntrl=(WebControl)ctrl;
wbCntrl.Tooltip="tooltip"; //// This is possible
wbCntrl.Text="test" ??? //// But this is not possible
Thanks
Upvotes: 0
Views: 1316
Reputation: 226
If you are using C# 4.0 (VS 2010) then you could use the "dynamic" keyword:
foreach (string id in List<IdCollection>)
{
dynamic ctrl = (this.Page.FindControl(id));
ctrl.Text = "test";
}
Obviously you would get a runtime exception if you try to do this on a control that's missing the Text property.
Upvotes: 1