内容占位符 Javascript 对象预期错误

发布于 2024-08-19 05:53:12 字数 609 浏览 3 评论 0原文

我有以下代码,直到最近,它都工作得很好而且花花公子!

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妥活 2024-08-26 05:53:12

有几个选项...

第一,用 <%=radProcAC.ClientID%> 这样的脚本替换控件名称。这有点混乱,但优点是非常容易实现。

第二,将 ClientID 写入 JavaScript 并使用 RegisterClientScript 将其输出到您的页面中。因此,在客户端,您将看到类似这样的内容:

var radProdACClientID = 'ctl00_ContentPlaceHolder1_radBuyer';
var ddlBuyerClientID = 'ctl100_ContentPlaceHolder1_ddlBuyer';

那么显然您将使用这些变量而不是原始控件名称。

无论如何,这是一个非常常见的问题,您可以通过一些谷歌搜索找到有关它的各种文章。

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:

var radProdACClientID = 'ctl00_ContentPlaceHolder1_radBuyer';
var ddlBuyerClientID = 'ctl100_ContentPlaceHolder1_ddlBuyer';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文