如何使 HtmlGenericControl 属性 runat = "server". 从代码中访问它?

发布于 2025-01-07 17:32:07 字数 236 浏览 0 评论 0 原文

我在访问运行时创建的一些 HtmlGenericControl 时遇到了一个奇怪的问题。

如何使 HtmlGenericControl runat ="server"?这样我就可以访问它


 HtmlGenericControl myli = new HtmlGenericControl("li");

I face a strange problem to access some HtmlGenericControl which created in run time .

How to make the HtmlGenericControl runat ="server"?so i can access it


 HtmlGenericControl myli = new HtmlGenericControl("li");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落□心尘 2025-01-14 17:32:07

是的,因为 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?

薄荷→糖丶微凉 2025-01-14 17:32:07

如果您在运行时创建任何 HtmlGenericControl ,这意味着服务器控件,您可以在代码中轻松使用它,如果您想使用当时 .aspx 页面上存在的任何 HTML 控件,请使用此控件将此 HTML 控件转换为服务器控件的方法。

  1. 在此 HTML 控件中添加 2 个新属性 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.

  1. Add 2 new properties in this HTML Controls runat="server" and id="id" in the HTML control.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文