ReadOnly ASP.NET TextBox at RunTime using jQuery
Posted by: Suprotim Agarwal
in Category jQuery and ASP.NET
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.
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.
<head id="Head1" runat="server">
<title>Make TextBoxes ReadOnly at RunTime</title>
<form id="form1" runat="server">
<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
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.
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!
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry
, DNC Magazine for Developers
. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls
and The Absolutely Awesome jQuery CookBook
Suprotim has received the prestigious Microsoft MVP award for Fifteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn