问题描述
今天,我得到了一个面试问题作为标题。
Today I got an interview question as titled.
例如,一个更新面板包含一个面板,面板上包含一个文本框。
For example, a Update Panel contains a Panel, the Panel contains a TextBox.
我怎样才能在TextBox的ClientID?
How can I get the TextBox ClientID?
我能想到的唯一的是:
-
我们可以predict文本框的ClientID取决于ClientID的模式,使用的getElementById。
We can predict the TextBox ClientID depends on the ClientID mode and use getElementByID.
即。 <$c$c>getElementByID('ct100$MasterPageBody$ct100$UpdatePanelID$ct100$PanelID$ct100$TextBoxID')
我们可以使用JavaScript来解析HTML,解析输入控件放入数组中。
We can use JavaScript to parse the HTML, parse the input controls into arrays.
即假设我们知道我们想要找到的第一个UpdatePanel中的第一小组的第一个文本框。
我们可以从myUpdatePanel找到[0],然后使用JavaScript得到它的孩子呢? (我只是猜测这里)
i.e Assume we know we want to find the first UpdatePanel's first Panel's first TextBox.We can find it from myUpdatePanel[0] then get its children using JavaScript? (I am only guessing here)
的getElementById('&LT;%= MyTextBox.ClientID%GT;')
不过,假设文本框添加动态的,但什么是做它的正确方法?
But assuming the TextBox is added dynamic, but what's the proper way of doing it?
推荐答案
您可以从一个jQuery选择基于喜欢这里的例子中,ID月底访问控制
You can access the control from a jQuery selector based on the end of the id like the example here:
<asp:CheckBox ID="chkEnable" Text="My Checkbox" runat="server" />
<script type="text/javascript">
$(function() {
var checked = $("input[id$=chkEnable]").attr("checked");
});
</script>
下面是对确切的事情了一篇文章:
http://weblogs.asp.net/joelvarty/archive/2009/02/09/jquery-get-a-handle-on-a-server-element-in-javascript-without-using-lt-elem-clientid-gt.aspx
Here's an article for the exact thing:http://weblogs.asp.net/joelvarty/archive/2009/02/09/jquery-get-a-handle-on-a-server-element-in-javascript-without-using-lt-elem-clientid-gt.aspx
这篇关于如何使用JavaScript来获取嵌套控制的客户端ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!