【问题标题】:How do I access the ASP control within span through span ID?如何通过 span ID 访问 span 内的 ASP 控件?
【发布时间】:2011-11-19 01:11:19
【问题描述】:

我使用自定义控件创建一个跨度和 2 个带 int 的 asp 控件。

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

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

我可以通过

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

但显然我不想对客户端 ID 进行硬编码,那么有没有更好的方法来处理这个问题?

【问题讨论】:

标签: c# javascript asp.net


【解决方案1】:

您可以像这样将控件 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 %>

【讨论】:

    猜你喜欢
    • 2012-10-12
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-21
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多