views:

56

answers:

4

Hi, I am trying to add an attribute to a web page control.

TextBox txt=new TextBox();
txt.Attributes["Foo"]="Bar"

I need to put this in a method, but my problems is that I do not know what kind of element I will be sending to it - maybe an input maybe a select. In essence I need this method below, but what is oControl? In VB I used to just call it an object.

protected void SetAttrib(oControl){
    oControl.Attributes["Foo"]="Bar"
}

Thanks

+2  A: 

WebControl

mgroves
Control doesn't have an Attributes property.
tvanfosson
Control class doesn't have Attributes property
Kamarey
Don't make me open Reflection! I'm trying to be lazy here :)WebControl has an Attributes property.
mgroves
Reflector, I mean.
mgroves
A: 

Make the parameter of a type that your webcontrols all inherit from. Otherwise you could make it of type object and cast.

Bryan Rowe
A: 

Use the WebControl class.

protected void SetAttrib( WebControl oControl, string bar )
{
     oControl.Attributes["Foo"] = bar;
}
tvanfosson
+6  A: 

I think you want WebControl:

protected void SetAttrib(WebControl oControl){
    oControl.Attributes["Foo"]="Bar"
}
Jon Skeet
Beaten by two minutes. Again.
John Saunders
awesome thanks!
Praesagus