Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

reference a control by a variable 1

Status
Not open for further replies.

drctx

IS-IT--Management
May 20, 2003
226
US
in vb i could use code like this to get the field name from a database table and then hide the textbox with the same name.
Code:
if rs("FieldName") = "hidden" then
  me("txt" & rs("FieldName")).visible = false
end if

but in asp.net using vb.net i can figure it out. the code below gives the error "Compiler Error Message: BC30367: Class 'ASP.srpages_sr_aspx' cannot be indexed because it has no default property."

Code:
    Dim MyDataReader As System.Data.SqlClient.SqlDataReader
    Dim MyCommand As System.Data.SqlClient.SqlCommand
    Dim MyConnection As System.Data.SqlClient.SqlConnection

    MyConnection = New System.Data.SqlClient.SqlConnection("server=sfx02ra04; initial catalog=SFXServiceRequestDB;uid=username;pwd=password")
    MyConnection.Open()
    MyCommand = New System.Data.SqlClient.SqlCommand
    MyCommand.Connection = MyConnection
    MyCommand.CommandText = "SELECT StageScreenFields.FieldName, StageScreenFields.FieldType FROM StageScreenFields WHERE (((StageScreenFields.FieldType) Is Not Null) AND ((StageScreenFields.StageID)=" & vstageID & "));"
    MyDataReader = MyCommand.ExecuteReader()

    'MyDataReader.Read()
    While MyDataReader.Read
            if MyDataReader.Item("FieldType") = "Hidden" then
                me("txt" & MyDataReader.Item("FieldName")).visible = false
            end if
    End While

    MyDataReader = Nothing
    MyCommand = Nothing
    MyConnection.Close()
    MyConnection = Nothing

does anybody know a way to do this?
 
I'm a C# guy so I don't know what the "Me" keyword does in that context, but the generic ASP.NET syntax would be something like:

Code:
Page.FindControl("txt" + MyDataReader.Item("FieldName")).visible = false

That's assuming, however, that the control is on the top layer of the control tree. Otherwise you'd have to either reference:

Code:
parentControl.FindControl()

or use recursion.
 
Page.FindControl worked. thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top