Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4.5.1 > ASP.NET 4.5.1 General Discussion
Password Reminder
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 4.5.1 General Discussion For ASP.NET 4.5.1 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4.5.1 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Thread Tools Display Modes
  #1 (permalink)  
Old May 18th, 2016, 02:06 PM
Friend of Wrox
Points: 2,813, Level: 22
Points: 2,813, Level: 22 Points: 2,813, Level: 22 Points: 2,813, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 427
Thanks: 57
Thanked 2 Times in 2 Posts
Default span.innerHTML rendering as literalcontrol?

I am trying to insert a link into span that is part of a web user control that is included on page that uses a master page for formatting:

span1.InnerHtml = "<a id='lnk1' href='url' runat='server'>text</a>

If I then dump this span's InnerHtml property, it returns HTML for the link, just as I'd expect.

But when I then try to iterate through the controls in span1:

For Each objControl as Object in span1
dim strObjectID as String = objControls.clientid


Instead of returning a link named 'lnk1', strObject is a LITERALCONTROL named 'ct105'? It has no innerHtml property but its Text property returns the HTML defining the link I am trying to insert.

The link works, but that's not the issue. I need to modify properties associated with it but cannot do so if it cannot be found by simply looping through the controls like this.

What do I need to do to make the link render properly in the InnerHtml property of the span instead of as a LiteralControl?

Last edited by Ron Howerton; May 18th, 2016 at 02:09 PM.
Reply With Quote
  #2 (permalink)  
Old June 22nd, 2016, 09:12 AM
Friend of Wrox
Points: 2,813, Level: 22
Points: 2,813, Level: 22 Points: 2,813, Level: 22 Points: 2,813, Level: 22
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Jun 2003
Location: Denver, CO, USA.
Posts: 427
Thanks: 57
Thanked 2 Times in 2 Posts

Is the object you're seeking nested in a container that is itself nested within the control being searched? I discovered that the loop in my original example did not search the contents of any containers in any Objects found in span1 in the For loop. Page.FindControl suffers from the same problem. I had to develop a recursive control search to find any object in any control nested within the root control. It's not clear which language you're using, but this VB code is being used in a production application:

Public Function FindControlRecursive(v_ctlRoot As Control, _
v_strControlIdSought As String) As Control

dim strRootID As String = v_ctlRoot.ID

If strRootID = v_strControlIdSought Then
Return v_ctlRoot ' exit immediately if the root is the control sought
End If

' loop through all of the controls in the current container

For Each ctlWorking As Control In v_ctlRoot.Controls
Dim strControlClientID As String = ctlWorking.ClientID

If strControlClientID = v_strControlIdSought Then ' if the current control is the one sought
Return ctlWorking ' exit here returning the control found
End If

If ctlWorking.Controls.Count > 0 Then ' if the current control is a container
' call this function recursively passing this control
Dim ctlFound As Control = FindControlRecursive(ctlWorking, _

If ctlFound IsNot Nothing Then ' if a control was found in the recursive call above, no need to search further
Return ctlFound ' return the control to whatever called this iteration of the function
End If
End If

Return Nothing ' if the control was not found in the current iteration
m_strError = Err.Description ' return error in module level variable

Return Nothing
End Try
End Function

Pass it the Form or any container on the Form as a control reference in ctlRoot and the ID of the control sought in strControlID. It returns a reference to the control sought, if it was found, which can then be used to change the control's attributes. It returns nothing if the control could not be found anywhere below the root control initially passed, so if you're sure it's there somewhere but cannot find it, start at the Form level.

Last edited by Ron Howerton; June 22nd, 2016 at 09:15 AM.
Reply With Quote
The Following User Says Thank You to Ron Howerton For This Useful Post:

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Selecting Rows That Span 360 Degrees DebbieLeigh MySQL 0 June 13th, 2013 09:44 AM
Column Span Problem fortier BOOK: Professional Microsoft SQL Server 2008 Reporting Services ISBN: 978-0-470-24201-8 2 January 13th, 2012 01:31 AM
Span Bug? interrupt HTML Code Clinic 4 June 20th, 2006 09:40 AM
Span tags not working Netscape apd8x Javascript 4 January 27th, 2005 05:11 AM
span tag with code behind jtyson General .NET 3 April 22nd, 2004 02:50 PM

All times are GMT -4. The time now is 09:19 PM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.