查找动态创建的控件的客户端 ID?
如何找到动态创建的控件的客户端 ID?
在我的 ascx 中,我有以下片段。
function DoSomething() {
var loneStar= $find("<%= loneStar.ClientID %>");
loneStar.hide();
}
在我后面的代码中,我遇到的
public partial class SomeControl: System.Web.UI.UserControl
{
protected Label loneStar = new Label { Text = "Raspeberry", ForeColor = System.Drawing.Color.DarkGray};
private void someOtherMethod()
{
somePanel.Controls.Add(loneStar);
}
}
问题是渲染页面中的 ClientID 显示为空。
我在这里缺少什么?
How can I find the client ID of a dynamically created control?
In my ascx, I have the following snippet.
function DoSomething() {
var loneStar= $find("<%= loneStar.ClientID %>");
loneStar.hide();
}
In my code behind, I have
public partial class SomeControl: System.Web.UI.UserControl
{
protected Label loneStar = new Label { Text = "Raspeberry", ForeColor = System.Drawing.Color.DarkGray};
private void someOtherMethod()
{
somePanel.Controls.Add(loneStar);
}
}
The problem is that the ClientID in the rendered page comes up as empty.
What am I missing here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
需要给控件一个ID,否则不会生成ID属性。对您的 c# 进行更改,如下所示:
You need to give the control an ID, otherwise no ID attribute will be generated. Make a change to your c# that looks something like this:
http://jagregory.com/writings/ how-to-use-clientids-in-javascript-without-the-uginess/
也许你应该检查一下,是否每次都使用相同的 id 动态添加控件
简单形式:
CodeBehind:
Html 输出:
http://jagregory.com/writings/how-to-use-clientids-in-javascript-without-the-ugliness/
Maybe you should check, if you are adding dynamically the control each time with the same id
Simple Form:
CodeBehind :
Html Output: