Compact code

A

anonymous

I have mycontrol as custom control and I need to get some
data from a control
on a different page. My code is below. It is working
perfectly fine, but since
I have 10 more label to check is it some how I can make
this code is more
compact?
If CType(myconrtol.FindControl("lblCompany"),
Label).Visible = True Then
dr(0) = CType(myconrtol.FindControl
("lblCompany"), Label).Text()
dr(1) = myconrtol.Company
dt.Rows.Add(dr)
dr = dt.NewRow()
End If

If CType(myconrtol.FindControl("lblPlanName"),
Label).Visible = True Then
dr(0) = CType(myconrtol.FindControl
("lblPlanName"), Label).Text()
dr(1) = myconrtol.PlanName
dt.Rows.Add(dr)
dr = dt.NewRow()

End If
 
J

Jason

Hello,
Assuming these controls are all part of a page, try looping through all
of the controls on the page...

For Each ctl In Me.Controls(1).Controls
Try
...
Catch
End Try
Next

Jason
 
A

anonymous

But I do I get to this code?
dr(0) = CType(mycontrol.FindControl("lblCompany"),
Label).Text()
dr(1) = mycontrol.Company
dt.Rows.Add(dr)
 
A

anonymous

Here is what I did based on your post, however I am not
getting back the right controls name in custom mycontrols.
For Each ctl In mycontrols.Controls 'Me.Controls
(1).Controls
Dim tb As Label
Dim tx As TextBox
If TypeOf ctl Is Label Then
tb = CType(ctl, Label)
dr(0) = tb.Text
ElseIf TypeOf ctl Is TextBox Then
tx = CType(ctl, TextBox)
dr(0) = tx.Text
Else
End If
dt.Rows.Add(dr)
dr = dt.NewRow()
Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top