Please visit my new Web Site https://coderstechzone.com
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:
if (!IsPostBack) 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:
<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:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readonly_textbox.aspx.cs" Inherits="readonly_textbox" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Read readonly textbox value in code file</title> <script type="text/javascript"> function setValue() { document.getElementById('<%= txt_ReadOnly.ClientID %>').value="Hello World!"; return false; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox runat="server" ID="txt_ReadOnly" ReadOnly="true"></asp:TextBox> <br /> <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" /> </div> </form> </body> </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:
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; public partial class readonly_textbox : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) txt_ReadOnly.Attributes.Add("readonly", "readonly"); } protected void cmdGetValue_Click(object sender, EventArgs e) { Response.Write(txt_ReadOnly.Text); } }The output will look like:
So now hope you understand what is the problem & how we can resolve it.
22 comments:
thanks dear
Thanks mate!
thanks boss :)
Thank you so much.
thanks master! thats awesome
thanks a lot.best solution
thanks ...dost great!!
really helpful
Hey Thanks....
thnx bro..it really worked
thanks a lot :)
really thanks a lot man.... it was taking me hours to get that value... ur simple awesome code really helped
thnxs
thanks a lot :)
doesnt work in asp.net 4!
Nice Article...it worked for me Thanks!!!!!
good
Thank u !!!!!!!!!!11
from Amiya
there is an asier way i guess
Request.Form["yourtextbox"]
So nice of you great work.... after getting so much insult from my boss finally I got this thumbs up :)
Thank's.... for solution
if (!IsPostBack)
txt_ReadOnly.Attributes.Add("readonly", "readonly");
chetan_
Thanks pal
Great work. Thanks dear.
I WOULD BE DELIGHTED TO HEAR FROM YOU