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

时间:2015-04-10 19:08:12

标签: html asp.net controls code-behind identifier

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

2)我在代码隐藏中创建并注册了一个javascript块,它通过ID来引用Panel。因此,在代码隐藏中,我需要检索面板的未来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;";

2 个答案:

答案 0 :(得分:1)

您无法使用服务器上创建的字符串中的内联语法。相反,只需将其连接起来。

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;";

答案 1 :(得分:0)

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

我没有检索html生成的ID,而是使用以下构造向控件添加id:Panel123.Attributes.Add(&#34; id&#34;,&#34; Panel123&#34;)。

它直接设置纯html标识符,而不需要任何其他服务器生成的&#34; bla_bla_bla&#34;东西。