News



Locations of visitors to this page


Alright, as much as I try to avoid using FindControl() sometimes I need to get a control by its ID.  I came across what seems like a good idea here : http://www.thescripts.com/forum/thread269592.html

It's in C#, but here is the VB.NET adaptation...

  549     Private Sub BuildControlHashTable()

  550         Dim cName As String = String.Empty

  551         For i As Int16 = 0 To Me.Controls.Count - 1

  552             cName = Me.Controls(i).ID

  553             If Not (cName Is Nothing) Then

  554                 cntrlHashTbl.Add(cName, Me.Controls(i))

  555             End If

  556         Next

  557     End Sub

  558     Private Function GetControlByName(ByVal cntrlName As String) As Control

  559         Return CType(Me.cntrlHashTbl(cntrlName), Control)

  560     End Function


Good idea? Bad idea? Thoughts?



posted @ Thursday, January 24, 2008 12:55 PM | Filed Under [ Developing & Best Practices VB.NET ]

Comments

Gravatar # re: Alternatives to FindControl()?
Posted by Hassan Gareeb allah on 9/1/2008 6:03 AM
try this code, it worked for me,
the first code to fine a calenar and the second one is to find a textbox

regards

----------
Dim cal As Calendar = DirectCast(GridView1.Rows(GridView1.EditIndex).FindControl("Calendar1"), Calendar)
Dim txt As TextBox = DirectCast(GridView1.Rows(GridView1.EditIndex).FindControl("txt_date"), TextBox)
'cal.SelectedDate = Format(cal.SelectedDate, "dd-MMM-yyyy")
txt.Text = Format(cal.SelectedDate, "dd-MMM-yyyy")

---------------------------------------------------------
Dim cal As Calendar = DirectCast(FormView1.FindControl("Calendar1"), Calendar)
Dim txt As TextBox = DirectCast(FormView1.FindControl("txt_date"), TextBox)
txt.Text = Format(cal.SelectedDate, "dd-MMM-yyyy")

--------------------------
Post a comment





 

Please add 3 and 8 and type the answer here: