内容占位符 Javascript 对象预期错误
我有以下代码,直到最近,它都工作得很好而且花花公子!
tbxProdAC.Attributes.Add("onclick", "$('#" + GridView1.ClientID + "').remove();
$('#" + radProdAC.ClientID + "').attr('checked', true);
$('#" + ddlBuyer.ClientID + "').val('--Choose Buyer--');
$('#" + ddlSub.ClientID + "').val('--Choose Sub Category--');
$('#" + ddlProd.ClientID + "').val('--Choose Product--');");
但是,自从我引入了内容占位符的概念(来自母版页)后,我就不断收到“预期对象”错误。
现在我明白为什么会发生这种情况,asp.net 正在修改控件名称以考虑 CPH,即 $('#ctl00_ContentPlaceHolder1_radBuyer').attr('checked', true);
我该如何做着手解决这个问题,有效地“重命名”我的 ASP.NET 控件以考虑到这一点?
I have the following code behind that was, until recently, working fine and dandy!
tbxProdAC.Attributes.Add("onclick", "$('#" + GridView1.ClientID + "').remove();
$('#" + radProdAC.ClientID + "').attr('checked', true);
$('#" + ddlBuyer.ClientID + "').val('--Choose Buyer--');
$('#" + ddlSub.ClientID + "').val('--Choose Sub Category--');
$('#" + ddlProd.ClientID + "').val('--Choose Product--');");
However, ever since I have introduced the notion of content place holders (from a master page), I am repeated getting the Object Expected error.
Now I understand why this is happening, asp.net is amending the control names to take into account the CPH i.e. $('#ctl00_ContentPlaceHolder1_radBuyer').attr('checked', true);
How do I go about reslving this issue, effectively 'renaming' my asp.net controls to take this into account?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几个选项...
第一,用 <%=radProcAC.ClientID%> 这样的脚本替换控件名称。这有点混乱,但优点是非常容易实现。
第二,将 ClientID 写入 JavaScript 并使用 RegisterClientScript 将其输出到您的页面中。因此,在客户端,您将看到类似这样的内容:
那么显然您将使用这些变量而不是原始控件名称。
无论如何,这是一个非常常见的问题,您可以通过一些谷歌搜索找到有关它的各种文章。
A couple options...
One, replace control names with script like <%=radProcAC.ClientID%>. This is somewhat messy, but has the advantage of being very easy to implement.
Two, write the ClientIDs into javascript and output this into your page using RegisterClientScript. So that on the client-side you'll have something like this:
Then obviously you'd use these vars rather than the raw control names.
At any rate, this is a very commmon problem and you can find all sorts of articles written about it with a little google search.