如何使用jquery获取Datalist控件的clientID
如何使用 jquery 获取 Datalist 控件的 clientID ,我尝试使用下面的代码但没有成功:
$(document).ready(function(){
$('#<%=txtRenewalDate.ClientID %>').datepicker();
$('#<%=txtCallBackDate.ClientID %>').datepicker();
});
<asp:DataList ID="dlCustomers" runat="server" ClientIDMode="Predictable">
<ItemTemplate>
<table border="0">
<tr>
<td class="">
RenewalDate:
</td>
<td class="">
<asp:TextBox ID="txtRenewalDate" runat="server" Text='<%# Eval("RenewalDate") %>' ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td class="">
Callback
</td>
<td class="">
<asp:TextBox ID="txtCallBackDate" runat="server" Text='<%# Eval("Callback") %>' ClientIDMode="Static"></asp:TextBox>
</td>
</tr></table>
</ItemTemplate>
</asp:DataList>
注释脚本时的标记
<td class="">
RenewalDate:
</td>
<td class="">
<input name="ctl00$MainContent$dlCustomers$ctl00$txtRenewalDate" type="text" value="27/01/2012 00:00:00" id="txtRenewalDate" />
</td>
</tr>
<tr>
<td class="">
Callback
</td>
<td class="">
<input name="ctl00$MainContent$dlCustomers$ctl00$txtCallBackDate" type="text" value="27/01/2012 00:00:00" id="txtCallBackDate" />
</td>
</tr>
当我运行时出现以下错误: 当前上下文中不存在名称“txtRenewalDate” 当前上下文中不存在名称“txtCallBackDate”
任何我弄错的想法。
谢谢
How to get clientID of Datalist control using jquery , I tried using the code below with no succes:
$(document).ready(function(){
$('#<%=txtRenewalDate.ClientID %>').datepicker();
$('#<%=txtCallBackDate.ClientID %>').datepicker();
});
<asp:DataList ID="dlCustomers" runat="server" ClientIDMode="Predictable">
<ItemTemplate>
<table border="0">
<tr>
<td class="">
RenewalDate:
</td>
<td class="">
<asp:TextBox ID="txtRenewalDate" runat="server" Text='<%# Eval("RenewalDate") %>' ClientIDMode="Static"></asp:TextBox>
</td>
</tr>
<tr>
<td class="">
Callback
</td>
<td class="">
<asp:TextBox ID="txtCallBackDate" runat="server" Text='<%# Eval("Callback") %>' ClientIDMode="Static"></asp:TextBox>
</td>
</tr></table>
</ItemTemplate>
</asp:DataList>
Markup when script is commented
<td class="">
RenewalDate:
</td>
<td class="">
<input name="ctl00$MainContent$dlCustomers$ctl00$txtRenewalDate" type="text" value="27/01/2012 00:00:00" id="txtRenewalDate" />
</td>
</tr>
<tr>
<td class="">
Callback
</td>
<td class="">
<input name="ctl00$MainContent$dlCustomers$ctl00$txtCallBackDate" type="text" value="27/01/2012 00:00:00" id="txtCallBackDate" />
</td>
</tr>
I get the following errors when i run :
The name 'txtRenewalDate' does not exist in the current context
The name 'txtCallBackDate' does not exist in the current context
Any ideas where i am getting it wrong .
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为问题可能在于,由于这些控件位于数据列表内,并且您从数据列表外部调用它们,因此从技术上讲,它们并不存在于表单的根部。你可能需要做这样的事情:
我可能是错的,但这是我最好的猜测。
I think that the issue may be that since those controls are within the datalist and you are calling them from outside of the datalist they technically do not exist at the root of the form. You may need to do something like this:
I may be wrong but that is my best guess.
这是完整/实际页面源吗?或者你真的在头部有这个脚本吗?如果您在 head 部分中有此内容,我相信您需要将 runat="server" 添加到 head 标记中。在主表单标记之外运行内联代码会出现一些问题。
Is that the full/actual page source? Or do you actually have that script up in the head section? If you have that in the head section, I beleive you need to add runat="server" to your head tag.. there are some issues with running in-line code outside of the main form tag.
如果您使用asp.net 4,那么您可以将客户端ID模式设置为静态,这比在DOM中查找客户端ID更容易 asp.net 4.0 clientID 模式
if your using asp.net 4, then you can set client ID mode to static, it is more easier than finding the client id in DOM asp.net 4.0 clientID modes
为了选择该元素,您应该使用如下内容:
In order to select this element, you should use something like this:
您可以使用表达式 $('<%= "#" + control.ClientID %>') 在 jquery 中选择控件。
希望这有帮助。
You can select the control in jquery using the expression $('<%= "#" + control.ClientID %>').
Hope this helps.