我使用自定义控件创建一个跨度和 2 个带有 int 的 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)属性,如下所示:
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 %>
关于c# - 如何通过span ID访问span内的ASP控件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7451112/