Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Step 1: I created a routine which dynamically creates the controls based on xslt file (which works fine) and stores the Control in Session

Private Sub CreateControls()
'Transform aspx page with fields from xml file
'Get transform result as a string
'Parse controls into a parent control holder

Dim xdoc As New XmlDocument()
Dim xsl As New XslTransform()
Dim sw As New StringWriter()
Dim xslarg As New XsltArgumentList()
Dim result As String


Try
PhysicalPath = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath)

XslFile = PhysicalPath & XslFile
XmlFile = PhysicalPath & XmlFile

xdoc.Load(XmlFile)

'load xslt to do transformation
xsl.Load(XslFile)

'get transformed results
xsl.Transform(xdoc, Nothing, sw)

result = sw.ToString().Replace("xmlns:asp=""remove""", "").Replace("<", "<").Replace(">", ">")

'parse the controls and add it to the page
Cntrl = ParseControl(result)
Panel1.Controls.Add(Cntrl)

Session("Controls") = Cntrl

Catch ex As Exception
Throw ex

Finally
If sw IsNot Nothing Then sw.Close() 'free up the memory of objects that are not used anymore

End Try

End Sub

Step 2: I am writing another sub-routine which finds the control from Session.

Private Sub FindPageControls()

For Each container As Control In Session("Controls")
For Each control As Control In container.Controls
If control.GetType = GetType(TextBox) Then
Label_Message.Text = DirectCast(control, TextBox).Text & DirectCast(control, TextBox).ID
End If
Next control
Next container
End Sub

I am getting an error - Unable to cast object of type 'System.Web.UI.Control' to type 'System.Collections.IEnumerable'. while I try to loop through Session("Controls"). Please help me with a solution or work around.
Posted

1 solution


Session("Controls") = Cntrl

In your loop, you're assigning a single control to the session variable. On each iteration, you overwrite the variable with a different control. Since System.Web.UI.Control doesn't implement IEnumerable, you can't use it in a For Each loop.

To make the For Each loop work, you would need to add the controls to a List(Of Control), and then store that list in the session variable.

However, storing controls in the session is a very bad idea. An instance of a control can only belong to a single control tree; you can't re-use it across different pages or different requests. Perhaps if you explain what you're trying to do, someone will be able to suggest a better approach.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900