我如何通过span ID访问span内的ASP控件

How do I access the ASP control within span through span ID?

本文关键字:span 内的 ASP 控件 访问 ID 何通过      更新时间:2023-09-26

我使用自定义控件创建了一个span和两个asp控件。

<abc:edf runat="server" ID="testing" ... />

现在我的问题是如何通过javascript中的span ID访问span内的asp控件?

我可以通过

找到它
var test = $get('<%=testing.ClientID %>');
alert(test.all.[hard coded client ID of inner asp control].value)

,但显然我不想硬编码客户端ID,所以有什么更好的方法来处理这个?

您可以像这样将控件ClientID公开为公共属性:

public class TestControl : CompositeControl
{
    private TextBox textBox;
    public string TextBoxClientID
    {
        get
        {
            return textBox.ClientID;
        }
    }
    protected override void CreateChildControls()
    {
        textBox = new TextBox();
        textBox.ID = "textBox1";
        Controls.Add(textBox);
    }
}

这样你就可以在你的代码块中访问它了:

<%= TestControl1.TextBoxClientID %>