Reputation: 49
Having one control ID in a string,can I know if its a text box or Rad Combo Box or RadDatePicker or any such property? Is there any function to get it? Can i use reflection? If not please suggest me some way to do it . I have a user control page named invoice.ascx and the controls are in its parent page ie invoice.aspx. I get to know only the ID. I have tried the following code in invoice.ascx.cs
string ID=txtPartyName;
TextBox txt = this.Parent.FindControl(ID) as TextBox;
but here i must know that it is a TextBox.I am passing ID to a function which will fill the controls.If passing any other property helps, please suggest.Thanks in advance!
Upvotes: 0
Views: 137
Reputation: 615
FindControl only searches controls under current naming container. In other words, it does not do find recursively. You might need that, depending on your control tree structure. See http://blog.codinghorror.com/recursive-pagefindcontrol/
In you line above: TextBox txt = this.Parent.FindControl(ID) as TextBox
txt would be null if the control found is not of type textbox. You can leverage this point to find if the control is a textbox or not
Upvotes: 0
Reputation: 56
Try it this way:
Control ctrl = this.Parent.FindControl(ID);
if(ctrl is TextBox){
TextBox txt = (TextBox)ctrl;
//Do anything Textbox specific
}
if(ctrl is RadDatePicker){
RadDatePicker rad = (RadDatePicker)ctrl;
//Do anything RadDatePicker specific
}
If you have several possible types, use switch(typeof(ctrl)) to check for the Type (so dont have to write so many if clauses.
If you write your specific aim, maybe there would be an even better solution.
Upvotes: 1