Control has not been declared. It may be unavailable due to its security level

I am really stuck and I tried all other examples but nothing seems to work. I'm new to ASP.NET but learned quickly!

I want to use JQuery datepicker and I paste this script

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

      

and my control on the aspx page

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

      

As soon as I close the server tag%>, a red line appears under the txtDOB control and says "txtDOB is not declared. It might not be accessible due to its security level. I made the class public in the code behind, but it doesn't make any difference. I also moved the script to the bottom of the page.If I change the asp textbox to HTML input it works fine.

Can anyone help me!

+3


source to share


3 answers


It works great with ASP.NET textbox as you have already used. Therefore it should be related to the position of your control. For example, if it's inside a repeater or grid, you won't be able to use its ID directly like this, as the framework will generate unique IDs for each row at runtime.



Create a simple web form with no other controls on the page, and you will find that it works the same way it does for you.

+3


source


Try using static id mode instead:

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

      



This simplifies the client script (especially when you move it into an external .js file to take advantage of caching) and this is an easy change for an ASP control:

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

+1


source


You can use another type of jQuery selector to select this id like this:

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

      

This selector will account for text added to the beginning of your ID due to the use of your CreateUserWizard control. It is cleaner than the one you are currently doing and you can use your original HTML.

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

      

0


source







All Articles