How to Get or Read Value from Readonly TextBox using Asp.Net C# VB.Net

As we know that when we declare a TextBox as ReadOnly in Design Mode then we did not get value from that ReadOnly TextBox from server side or from code behind. Because Asp.Net rejects any changes in ReadOnly TextBox after postback. Its a bug. Its a problem for developers. There is a workaround on this issue which I will share with you. The solution is simple.

1. Dont assign readonly property=true in design mode.
2. Bind the readonly property from serverside page load event like:

C# Code:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            txt_ReadOnly.Attributes.Add("readonly", "readonly");

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            txt_ReadOnly.Attributes.Add("readonly", "readonly")
        End If
    End Sub

Hope now your problem has been resolved.

How to Get Readonly TextBox Value from Codebehind or Server Side CS File in Asp.Net Csharp

Ok still if you want to set the ReadOnly property to true in design mode then follow the below HTML Markup:

<script type="text/javascript">
    function setValue() {
        document.getElementById('<%= txt_ReadOnly.ClientID %>').value = "Hello World!";
        return false;
    <asp:TextBox runat="server" ID="txt_ReadOnly" ReadOnly="true"></asp:TextBox><br />

    <asp:Button runat="server" ID="cmdSet" Text="Set value in readonly textbox" OnClientClick="return setValue();" />
    <asp:Button runat="server" ID="cmdGetValue" Text="Get value from readonly textbox" OnClick="cmdGetValue_Click" />

Note: Here i write javascript code to set some value in ReadOnly TextBox.

Read ReadOnly TextBox Value under cmdGetValue Button:
C# Code:

    protected void cmdGetValue_Click(object sender, EventArgs e)

VB.Net Code:

    Protected Sub cmdGetValue_Click(sender As Object, e As EventArgs)
    End Sub

Hope Request.Form will resolve your problem.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code