How can I type cast an string as an htmlTable ?
-
Hi I have a problem with type casting . I have the string which is the name of one control(HtmlTable) . I know the control I want to type cast the name as the control. I tried the following and I get "Invalid Casting ! " string TableName = "Table1"; (HtmlTable)(TableName) and (HtmlTable)((object)(TableName)) Thanks
-
Hi I have a problem with type casting . I have the string which is the name of one control(HtmlTable) . I know the control I want to type cast the name as the control. I tried the following and I get "Invalid Casting ! " string TableName = "Table1"; (HtmlTable)(TableName) and (HtmlTable)((object)(TableName)) Thanks
Do you know the container of the control? Then you can find the name in the Controls colleciton: HtmlTable myTable = null; foreach(Control ctl in container.Controls){ if(ctl.Name == tableName){ myTable = (HtmlTable)ctl; break; } } If you don't know, where the control is, you can find it "by name": String tableName = "Table1"; Object ctl = null; System.Reflection.FieldInfo info = this.GetType().GetField(tableName); if(info != null){ ctl = info.GetValue(this); } HtmlTable myTable = (HtmlTable)ctl;
-
Do you know the container of the control? Then you can find the name in the Controls colleciton: HtmlTable myTable = null; foreach(Control ctl in container.Controls){ if(ctl.Name == tableName){ myTable = (HtmlTable)ctl; break; } } If you don't know, where the control is, you can find it "by name": String tableName = "Table1"; Object ctl = null; System.Reflection.FieldInfo info = this.GetType().GetField(tableName); if(info != null){ ctl = info.GetValue(this); } HtmlTable myTable = (HtmlTable)ctl;
Or by referencing the container control, you can use
FindControl
, passing the name. Cast the return (after checking fornull
) to anHtmlTable
then.Microsoft MVP, Visual C# My Articles
-
Hi I have a problem with type casting . I have the string which is the name of one control(HtmlTable) . I know the control I want to type cast the name as the control. I tried the following and I get "Invalid Casting ! " string TableName = "Table1"; (HtmlTable)(TableName) and (HtmlTable)((object)(TableName)) Thanks