如何使 HtmlGenericControl 属性 runat = "server". 从代码中访问它?
我在访问运行时创建的一些 HtmlGenericControl
时遇到了一个奇怪的问题。
如何使 HtmlGenericControl
runat ="server"
?这样我就可以访问它
HtmlGenericControl myli = new HtmlGenericControl("li");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,因为 runat 在编译时被解释,它实际上在 .designer 文件中创建了控件,这基本上就是您在 .cs 中所做的事情(代码隐藏)因此,在运行时创建控件,您需要手动创建控件树,这就是如果您使用设计器,则会自动发生。
我相信您会创建某种结构、数组、字典等,将 li 列表映射到其父级 UL,然后您可以通过通过 li 散列到的 UL 键拉出 li 列表来在代码中进行交互。
如果您愿意的话,可以使用列表地图。因此 Map.get("ulKey") 返回一个 li 列表,您现在可以迭代并运行适当的代码。有道理吗?
Right because runat is interpeted at compile time it actually creates the control in the .designer file which is basically what you are doing in you .cs (code behind) So creating the control at runtime you need to manually create your Control tree, something that would happen automatically if you were using the designer.
I believe you would create some kind of Structure, Array, Dictionary etc. that would map a list of li's to their parrent UL then you can interate those in code by pulling a list of li's by the UL key that the li's are hashed to.
A Map of lists if you will. So Map.get("ulKey") returns a list of li's you can now iterate and run appropriate code on. Make sense?
如果您在运行时创建任何
HtmlGenericControl
,这意味着服务器控件,您可以在代码中轻松使用它,如果您想使用当时 .aspx 页面上存在的任何 HTML 控件,请使用此控件将此 HTML 控件转换为服务器控件的方法。runat="server"
和id="id"
在 HTML 控件中。If you are creating any
HtmlGenericControl
at run time that means is server control and you can use it in your code easily and if you want to use any HTML control which exists on .aspx page at that time use this way to convert this HTML control to server control.runat="server"
andid="id"
in the HTML control.