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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't find nested textbox

Status
Not open for further replies.

tigerjade

Programmer
Mar 17, 2004
237
US
I'm getting object errors when I try to FindControl on a dynamically created textbox. The textbox is added to a placeholder, which resides on a datagrid, which in turn resides on a repeater.

Code:
			foreach (RepeaterItem rItem in QuestionRepeater.Items)
			{
				DataGrid RADG = rItem.FindControl("AnswerDG") as DataGrid;
				foreach (DataGridItem dgItem in RADG.Items)
				{
					if (dgItem.ItemType == ListItemType.Item || dgItem.ItemType == ListItemType.AlternatingItem)
					{
						Label QIDLbl = dgItem.FindControl("DGQuestionIDLbl") as Label;
						Label AnsIDLbl = dgItem.FindControl("AnswerIDLbl") as Label;
						Label GNLbl = dgItem.FindControl("GroupNameLbl") as Label;
						string SubQID = QIDLbl.Text;
						string SubAID = AnsIDLbl.Text;
						string SubGroupName = GNLbl.Text;
						string ThisAnswer = String.Empty;
						string ThisResponse = String.Empty;

						string TBName = "AnswerTB" + SubQID.ToString() + SubAID.ToString();
						TextBox thisTB = dgItem.FindControl(TBName) as TextBox;
						ThisResponse = thisTB.Text.Trim();

When I test this, I get errors on the line "ThisResponse = thisTB.Text.Trim();". I've also tried accessing the textbox as a child of the placeholder, but that doesn't seem to help either. Any ideas?

thanks!

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Thanks xr280xr :)

"Object reference not set to an instance of an object"

For more weirdness, it DOESN'T give me that error on the declaration of the textbox itself unless I screw up the path. For example, if I attempt to find the textbox by doing this:

Code:
Textbox thisTB = rItem.FindControl("AnswerDG").FindControl("AnswerPlaceholder").FindControl(TBName) as Textbox

it then throws the error on that line instead of on the line where I try to pull text.

thanks!


tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
bump

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
Well, I didn't figure out what was going on, but I did figure out a way around it. I replaced the Placeholder with a Panel and voila! No more object errors. :)

tigerjade

"Always code as if the person who ends up maintaining your code will be a violent psychopath who knows where you live." -- Martin Golding


 
If you want to use a placeholder in the future you first do FindControl on the PlaceHolder and then do FindControl for your target control contained in the placeholder
 
The FindControl method only searches the current container, not its children (or parents).

The TextBox you're looking for must exist at a different level than the one you were searching.
 
Placeholder, ASP:Tables and just a few other controls are naming containers.
Namingcontainers break the chain of searching for child controls.
A work around is write a function that recursively searches the naming container and subsequent child naming containers. The only rule you should adhere too is don't name two objects the same id.

Code:
 Public Function FindControlDeep(ByVal Parent As Object, ByVal ChildControlID As String) As Control
        Try
            If Parent.HasControls Then
                If Not Parent.FindControl(ChildControlID) Is Nothing Then
                    Return Parent.FindControl(ChildControlID)
                End If
                For Each obj As Object In Parent.Controls
                    Dim Found As Control = FindControlDeep(obj, ChildControlID)
                    If Not Found Is Nothing Then
                        Return Found
                    End If
                Next
            End If
        Catch
        End Try

    End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top