By 06/02/2014 9 Comments

How To Make an ASP.NET TextBox ReadOnly at RunTime using jQuery

What is Read only text box

  • Some time we need a textbox which has value inside it, and we want to that value can not be changed / edited by end-user that textbox called read only textbox
  • It can be easily achieved by JQuery

Following code describes how to make a textbox control read only in asp.net using JQuery

JQuery Code snippet

    <script type="text/javascript" src="Scripts/jquery-1.8.2.js"></script> <!--- import jquery library --->
    <script>
        $(document).ready(function() { // method called, when document loaded by browser

            $('input:text[value]').each(function () { //  filters the textboxes that have values in it (TT_TextBox1 and TT_TextBox4) and applies the ‘readonly’ attribute to them, each() method execute that function each time

                $(this).attr('readonly', true); // set the attribute 'readonly' of selected textboxes, attr() method returns values and attributes of the selected elements, attr() method pass 2 parameters first return attributes name and second is its value

            });

        });

    </script>

ASP.NET code snippet

<!--- Following code renders the 4 text fields and a label on browser --->

<asp:Label runat="server" Text="1st and 4th TextBoxes have been made read-only and cannot be edited"></asp:Label>
        <br/>
        <asp:TextBox ID="TT_TextBox1" runat="server" Text="ReadOnlyText" />
        <asp:TextBox ID="TT_TextBox2" runat="server" Text="" />
        <asp:TextBox ID="TT_TextBox3" runat="server" Text="" />
        <asp:TextBox ID="TT_TextBox4" runat="server" Text="ReadOnlyText" />






Posted in: ASP.NET, jQuery

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .