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!

I want to use the jQuery datepicker and I am inserting this script

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

and my control on the aspx page is

<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 is not declared. 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.


It will work fine with an ASP.NET TextBox as you have used. 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.

Create a simple webform with no other controls on the page, and you will find it works just as you have it.


Try using static ID mode instead:

<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:

<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:

<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. It is cleaner than the way you are currently doing it, and you can use your original HTML.

<asp:TextBox ID="txtDOB" CssClass="form-control" runat="server"></asp:TextBox>
链接地址: http://www.djcxy.com/p/65216.html

上一篇: *没有声明。 由于其保护级别,它可能无法访问

下一篇: 控制未被声明。 由于其保护级别,它可能无法访问