Tuesday, June 12, 2012

How to get readonly textbox value in codebehind or CS file

As we know that when we declare a textbox as readonly then we didnot get value from that readonly textbox from code file. Because asp.net rejects any changes in readonly textbox after postback. Here in this article i will describe ASP.NET Read Only TextBox lose client side changes, values across post back? Why Readonly Text box values are empty in code behind ? Issue in Retrieving textbox value when readonly = true in codebehind.? The real thing is when you declare a textbox as readonly in design mode and assign a value using a javascript function then from code file you did not get the value in server side. 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.

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


1if (!IsPostBack)
2            txt_ReadOnly.Attributes.Add("readonly""readonly");
Hope now your problem has been resolved.


Ok now i am going to create an example. In this example first take a textbox in your page with readonly=true. like:
1<asp:TextBox runat="server" ID="txt_ReadOnly" ReadOnly="true"></asp:TextBox>
Then add two button. One is to set some value into the readonly textbox using javascript. And another button will try to read the readonly textbox value that i have set before. Look at this moment you would not get the value & the readonly textbox will loose the value after second button postback. Now remove the readonly property from design page. So now your complete HTML markup will look like:


01<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readonly_textbox.aspx.cs" Inherits="readonly_textbox" %>
02
03<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04
05<html xmlns="http://www.w3.org/1999/xhtml" >
06<head runat="server">
07    <title>Read readonly textbox value in code file</title>
08    <script type="text/javascript">
09    function setValue()
10    {
11        document.getElementById('<%= txt_ReadOnly.ClientID %>').value="Hello World!";
12        return false;
13    }
14    </script>
15</head>
16<body>
17    <form id="form1" runat="server">
18    <div>
19    <asp:TextBox runat="server" ID="txt_ReadOnly" ReadOnly="true"></asp:TextBox>
20     
21
22     
23
24    <asp:Button runat="server" ID="cmdSet" Text="Set value in readonly textbox"OnClientClick="return setValue();" />
25    <asp:Button runat="server" ID="cmdGetValue" Text="Get value from readonly textbox"OnClick="cmdGetValue_Click" />
26    </div>
27    </form>
28</body>
29</html>
Here you found that under first button i have assigned a javascript function to write something in the readonly textbox. Now under second button just write the readonly textbox value into the form. Also do not forget add the readonly attribute under page_load event like below. So your server side code will look like:
01using System;
02using System.Data;
03using System.Configuration;
04using System.Collections;
05using System.Web;
06using System.Web.Security;
07using System.Web.UI;
08using System.Web.UI.WebControls;
09using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12public partial class readonly_textbox : System.Web.UI.Page
13{
14    protected void Page_Load(object sender, EventArgs e)
15    {
16        if (!IsPostBack)
17            txt_ReadOnly.Attributes.Add("readonly""readonly");
18    }
19    protected void cmdGetValue_Click(object sender, EventArgs e)
20    {
21        Response.Write(txt_ReadOnly.Text);
22    }
23}
The output will look like:
Readonly textbox problem
So now hope you understand what is the problem & how we can resolve it.