循环通过会话(“控制”) [英] Looping through Session("Controls")

查看:54
本文介绍了循环通过会话(“控制”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

步骤1:我创建了一个例程,它根据xslt文件动态创建控件(工作正常)并将控件存储在会话中



Private Sub CreateControls( )

'使用xml文件中的字段转换aspx页面

'将转换结果作为字符串获取

'将控件解析为父控件持有者



Dim xdoc As New XmlDocument()

Dim xsl As New XslTransform()

Dim sw As New StringWriter ()

Dim xslarg As New XsltArgumentList()

Dim result As String





尝试

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



XslFile = PhysicalPath& XslFile

XmlFile = PhysicalPath&XmlFile



xdoc.Load(XmlFile)



'加载xslt进行转换

xsl.Load(XslFile)



'get转换结果

xsl.Transform(xdoc,Nothing,sw)



result = sw.ToString()。替换(xmlns:asp =删除,。更换(<,<)。替换(>,>)



'解析控件并将其添加到页面

Cntrl = ParseControl(结果)

Panel1.Controls.Add(Cntrl)



会话(控制)= Cntrl



Catch ex As Exception

Throw ex



最后

如果sw IsNot Nothing则sw.Close()'释放不再使用的对象的内存



结束尝试



结束次级



第2步:我我在写另一个寻找的子程序来自Session的控件。



Private Sub FindPageControls()



For Each container As Control in Session (控件)

对于每个控件作为控件在container.Controls

如果control.GetType = GetType(TextBox)那么

Label_Message。 Text = DirectCast(control,TextBox).Text&DirectCast(control,TextBox).ID

结束如果

下一个控件

下一个容器< br $>
结束子



我收到错误 - 无法将'System.Web.UI.Control'类型的对象强制转换为'System' .Collections.IEnumerable。而我试图循环通过Session(控件)。请帮我解决方案或解决。

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.

推荐答案



会话(控制)= Cntrl


Session("Controls") = Cntrl



在循环中,您将单个控件分配给会话变量。在每次迭代时,使用不同的控件覆盖变量。由于 System.Web.UI.Control 未实现 IEnumerable ,因此无法在<$中使用它c $ c>对于每个循环。



为每个循环制作工作时,您需要将控件添加到 List(Of Control),然后将该列表存储在会话变量中。



但是,在会话中存储控件是一个非常糟糕的主意。控件的实例只能属于单个控制树;您无法在不同页面或不同请求中重复使用它。也许如果你解释一下你想要做什么,有人可以建议一个更好的方法。


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.


这篇关于循环通过会话(“控制”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆