使用其 html 标识符引用在代码隐藏中创建的 HTML 控件

Refer to an HTML control created in code-behind using its html identifier

1) 我在代码隐藏中创建了一个面板并将其 ID 指定为 "Panel123"。当在 html 页面上呈现时,面板稍后会收到其 html ID。 html 中的这个 ID 看起来像 "ctl00_ctl00_bla_bla_bla_Panel123"。

2) 我在代码隐藏中创建并注册了一个 javascript 块,它通过 ID 引用面板。因此,在代码隐藏中,我需要检索面板的未来 html ID 并将此 ID 嵌入到脚本中。

我尝试了 ClientID、UniqueID、ID,但所有这些服务器端属性只给出了面板 ID 的结尾部分,而不是整个 bla_bla_bla 东西。

我也试过将字符串“<%=Panel123.ClientID%>”放入脚本文本中,但是在html中呈现如下(我故意添加空格以防止替换符号): ' & lt ; %=Panel123.ClientID%>'。因此,如您所见,脚本无法将其识别为有效的 html ID。

如何在代码隐藏中获取整个 html id?

代码隐藏

Panel123 = new Panel();
Panel123.ID = "Panel123";

...

LinkButton123.OnClientClick = "function123(this,'<%=Panel123.ClientID%>'); return false;";

您不能像那样使用在服务器上创建的字符串中的内联语法。相反,只需将它连接起来。

LinkButton123.OnClientClick = "function123(this,'" + Panel123.ClientID + "'); return false;";

要么,要么将面板的 ClientIDMode 更改为 Static。那么 ID 将永远只是 Panel123.

Panel123 = new Panel();
Panel123.ID = "Panel123";
Panel123.ClientIDMode = "Static";
LinkButton123.OnClientClick = "function123(this,'Panel123'); return false;";

好的,我自己找到了解决方案。

我没有检索 html 生成的 ID,而是使用以下结构将 ID 添加到控件:Panel123.Attributes.Add("id", "Panel123").

它直接设置纯 html 标识符,没有任何其他服务器生成的 "bla_bla_bla" 内容。