ReadOnly ASP.NET TextBox at RunTime using jQuery

Posted by: Suprotim Agarwal , on 6/28/2010, in Category jQuery and ASP.NET
Views: 82059
Abstract: In this short and simple article, we will see how to check a condition and make a TextBox readonly at runtime using jQuery.
In this short and simple article, we will see how to check a condition and make a TextBox readonly at runtime using jQuery.
This article is a chapter from my EBook called 51 Recipes with jQuery and ASP.NET Controls. The chapter has been modified a little to publish it as an article.
Note that for demonstration purposes, I have included jQuery code in the same page. Ideally, these resources should be created in separate folders for maintainability. The code shown below has been tested on IE7, IE8, Firefox 3, Chrome 2 and Safari 4.
Let us quickly jump to the solution to check if the TextBox has some value in it and make it read-only.
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Make TextBoxes ReadOnly at RunTime</title>
   
    <script type="text/javascript"
        src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js">
    </script>      
   
    <script type="text/javascript">
        $(function ()
        {
            $('input:text[value!=]').each(function ()
            {
                $(this).attr('readonly', true);
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div class="bigDiv">
        <h2>Make TextBoxes ReadOnly at RunTime</h2><br />
        <asp:TextBox ID="tb1" runat="server" Text="ReadOnlyText"/><br />
        <asp:TextBox ID="tb2" runat="server" Text=""/><br />
        <asp:TextBox ID="tb3" runat="server" Text=""/><br />
        <asp:TextBox ID="tb4" runat="server" Text="ReadOnlyText" />
        <br /><br />
        Tip: 1st and 4th TextBoxes have been made read-only
        and cannot be edited
    </div>
    </form>
</body>
</html>
 
This is a very common requirement that most developers face on a daily basis. While editing a form, textboxes that have text in it, should be made read-only. The code shown in the example filters the textboxes that have values in it (tb1 and tb4) and applies the ‘readonly’ attribute to them.
$('input:text[value!=]').each(function() {
     $(this).attr('readonly', true);
});
When the document loads, the user is able to enter text in the second and third textboxes, but not in the first and fourth, since they are now read-only. It is that simple!
TextBox
See a Live Demo
I hope you found this article useful and I thank you for viewing it. This article was taken from my EBook called 51 Tips, Tricks and Recipes with jQuery and ASP.NET Controls which contains similar recipes that you can use in your applications
Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Albas on Monday, June 28, 2010 11:01 AM
Thank you for this tip can we also add css and do the same code in different way?
Comment posted by Rasel on Saturday, September 4, 2010 2:32 AM
When I entered text in Editable TextBox, PostBack event marked all the textbox as readOnly. How can I solve this problem?
Comment posted by Suprotim Agarwal on Saturday, September 4, 2010 4:35 AM
Rasel: That's exactly what this example shows you - While editing, mark all textboxes as readonly that have values in it. What is your requirement?
Comment posted by Deepika Gupta on Thursday, September 23, 2010 6:00 PM
perfect code !..worked for me ...thanks a lot
Comment posted by sg on Monday, May 16, 2011 5:08 AM
wer

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel