简体   繁体   English

未声明控制权。 由于其保护级别,它可能无法访问

[英]Control is not declared. It may be inaccessible due to its protection level

I'm really stuck and I've tried all the other examples but nothing works. 我真的被困了,我已经尝试了所有其他的例子,但没有任何作用。 I'm fairly new to ASP.NET but learning fast! 我对ASP.NET很新,但学得很快!

I want to use the jQuery datepicker and I am inserting this script 我想使用jQuery datepicker,我正在插入这个脚本

 <script>
          $(document).ready(function () {
              $(function () {
                  $("#" + '<%=txtDOB.ClientID%>').datepicker();
            });
        });
  </script>

and my control on the aspx page is 我对aspx页面的控制是

<asp:TextBox ID="txtDOB" CssClass="form-control" runat="server"></asp:TextBox>

As soon as I close the the server tag %> the red line appears under the txtDOB control and says: 一旦我关闭服务器标签%> ,txtDOB控件下面会出现一条红线,并说:

txtDOB is not declared. txtDOB未声明。 It may be inaccessible due to its protection level. 由于其保护级别,它可能无法访问。

I've made the class public in the code behind but doesn't make any difference. 我已经把这个类公开在代码中,但没有任何区别。 I've also moved the script to the bottom of the page. 我还将脚本移到了页面底部。 If I change the asp textbox to a HTML input it works fine. 如果我将asp文本框更改为HTML输入,它可以正常工作。

It will work fine with an ASP.NET TextBox as you have used. 它可以正常使用ASP.NET TextBox。 Therefore it must be to do with where your control is located. 因此,必须与控件的位置有关。 For example, if it's inside a Repeater or Grid, you will not be able to use its ID directly like that, since the framework will generate unique ids for each row at runtime. 例如,如果它在Repeater或Grid中,您将无法直接使用它的ID,因为框架将在运行时为每一行生成唯一的ID。

Create a simple webform with no other controls on the page, and you will find it works just as you have it. 创建一个简单的webform,页面上没有其他控件,你会发现它就像你拥有它一样。

Try using static ID mode instead: 请尝试使用静态ID模式:

<script>
          $(document).ready(function () {
                  $("#txtDOB").datepicker();
        });
</script>

It makes the client script easier (especially when you move it to an external .js file to take advantage of caching), and is an easy change to the ASP control: 它使客户端脚本更容易(特别是当您将其移动到外部.js文件以利用缓存时),并且很容易更改ASP控件:

<asp:TextBox ID="txtDOB" CssClass="form-control" runat="server" ClientIDMode="Static"/>

You could use a different type of jQuery selector to select for that ID as follows: 您可以使用不同类型的jQuery选择器来选择该ID,如下所示:

<script>
    $(document).ready(function () {
        $("input[id$=_txtDOB]").datepicker();
    });
</script>

That selector will account for the text that is being appended to the beginning of your ID due to the use of your CreateUserWizard control. 由于使用了CreateUserWizard控件,该选择器将考虑附加到ID开头的文本。 It is cleaner than the way you are currently doing it, and you can use your original HTML. 它比您当前的方式更干净,您可以使用原始HTML。

<asp:TextBox ID="txtDOB" CssClass="form-control" runat="server"></asp:TextBox>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 javascript 参数给我一个“它可能由于保护级别而无法访问”。 错误? - Why is my javascript parameter giving me a “It may be inaccessible due to a protection level.” error? 函数只能在严格模式的顶层声明 - Functions may be declared only at top level in strict mode 脚本以纯文本格式加载-未声明纯文本文档的字符编码。 - script loaded as plain text - The character encoding of the plain text document was not declared. 链接(带有重定向)将以隐身方式打开,或在新标签页中打开,或者显式声明新页面。 只是单击不起作用 - Link (with redirects) opens in incognito or when open in new tab, or new page is explicitly declared. Doesn't work when just clicked 函数被声明但它的值从未被使用 - Function is declared but its value is never used HTML和JS中的函数表示未声明 - Function in html and JS is saying its not declared function 已声明,但从未读取其值:React - function is declared but its value is never read : React 在上下文中声明的 state 变量没有保持其值 - The state variable declared in context is not persisting its value 'callCounter' 已声明,但其值从未被读取 - 'callCounter' is declared but its value is never read 结果已声明,但其值永远不会在离子3上读取 - Result is declared but its value is never read on ionic 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM