Monday, March 26, 2012

Trouble referencing controls within list controls

I have a DataGrid containing a TextBox control and a CustomValidator
in each row. The CustomValidator fires a function that compares all
TextBoxes for equality. The algorithm for comparison is
straightforward:
*PSEUDOCODE*
for i=1 thru ( Container.Length-1 )
for j=i+1 thru ( Container.Length-1 )
if ( TextBox[i]==TextBox[j] )
TRUE
It would be even faster if instead of starting with 1 everytime, I set
i to get the index of the current Container row.
Problem is I'm having trouble referencing the TextBoxes within the
DataGrid. I've studied msdn
(http://msdn.microsoft.com/library/d...bformspages.asp)
but the concept still baffles me.
Specifically, what I try to do is store the TextBoxes for comparison
in an array:
Dim ControlName As String ' temporary
ControlName = OurDataGrid.Controls(i).UniqueID
aObjFirst(0) = CType( Me.FindControl(ControlName & "First"), TextBox
)
ControlName = OurDataGrid.Controls(j).UniqueID
aObjFirst(1) = CType( Me.FindControl(ControlName & "First"), TextBox
)
' If first names match...
If ( aObjFirst(0).Text=aObjFirst(1).Text ) Then
..
(Yes, I'm comparing first names.) However, this always warns me that
j is out of range. And getting back to the msdn, it seems to be
discouraging the technique of name concatenation I used to get at the
controls I want.
Can anyone follow where I'm going with this and provide some
insightful direction?--or a totally different approach would be welcome. Anything that
could help, really.
I just thought of a less repetitive algorithm.
Provided I can dynamically obtain the index of the current row:
*PSEUDOCODE*
for i=currentIndex thru ( DataGrid.Controls.Count-1 )
if ( TextBox[currentIndex].Text==TextBox[i].Text )
TRUE
Problem is I still need help referencing the individual TextBox
controls that my DataGrid generates. I have a feeling the solution
lies with the UniqueID property, but I still can't solve it on my own.

> I have a DataGrid containing a TextBox control and a CustomValidator
> in each row. The CustomValidator fires a function that compares all
> TextBoxes for equality. The algorithm for comparison is
> straightforward:
> *PSEUDOCODE*
> for i=1 thru ( Container.Length-1 )
> for j=i+1 thru ( Container.Length-1 )
> if ( TextBox[i]==TextBox[j] )
> TRUE
>
...

> Specifically, what I try to do is store the TextBoxes for comparison
> in an array:
> Dim aObjFirst(2) As TextBox
> Dim ControlName As String ' temporary
> ControlName = OurDataGrid.Controls(i).UniqueID
> aObjFirst(0) = CType(Me.FindControl(ControlName & "First"), TextBox)
> ControlName = OurDataGrid.Controls(j).UniqueID
> aObjFirst(1) = CType(Me.FindControl(ControlName & "First"), TextBox)
> ' If first names match...
> If ( aObjFirst(0).Text=aObjFirst(1).Text ) Then
> ...
> (Yes, I'm comparing first names.) However, this always warns me that
> j is out of range. And getting back to the msdn, it seems to be
> discouraging the technique of name concatenation I used to get at the
> controls I want.
> Can anyone follow where I'm going with this and provide some
> insightful direction?
Same question, different day, (hopefully) worded more clearly.
I have a DataGrid containing a TextBox control and a CustomValidator in
each row.
The CustomValidator fires a subroutine that compares all TextBoxes the
DataGrid generates for equality.
The algorithm is pretty straightforward:
*PSEUDOCODE*
senderIndex = the row in the DataGrid of the sender
FOR ( i=senderIndex+1 ) THRU ( last Row of the DataGrid )
IF ( TextBox[i].Text==TextBox[senderIndex].Text )
TRUE
However, I'm stuck in my attempts to formally code this algorithm:
*CODE*
Sub HasDupe(sender as Object, e as EventArgs)
Dim objFirst As Textbox
value.IsValid = False
For ( i = SENDER_INDEX+1 ) To ( DataGrid.Controls.Count-1 )
' get the control in row i with the same ID as the
sender
objFirst =
OurDataGrid.Items(i).FindControl(sender.ControlToValidate)
If ( objFirst.Text=sender.Text )
value.IsValid = True
End If
Next
End Sub
I /think/ my only problem is the "SENDER_INDEX"--I can't figure out how
to reveal the row number of the sender. This is my question.
(But if any other part of my code looks screwy, I'd appreciate input on
that as well.)
(Here's an answer I received from another post, that _works._
--E.)
Use can use the parent control to find the index. All Controls have
parents and children, you can move up and down the node list to find
the one you are looking for. You can shorten the code, I just wanted to
make sure you saw the levels. Also a while back I was trying to deal
with CustomValidators in datagrids, I seem to recall some issues with
that just FYI. Hope this helps AuSable Troutbum
'Find the The control that is firing event
Dim valCustomControl As New CustomValidator
valCustomControl = sender
'Find the Parent Cell of Validation Control
Dim cell As TableCell = valCustomControl.Parent
'Find the Datagrid Item
Dim dgItem As DataGridItem = cell.Parent
'Set the Row
Dim myRow As DataRow =
DataSet.Tables("YourTableName").Rows(dgItem.ItemIndex)

0 comments:

Post a Comment