Wednesday, February 17, 2010

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:


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:
Readonly textbox problem
So now hope you understand what is the problem & how we can resolve it.

22 comments:

VIjay Patel said...

thanks dear

Eren said...

Thanks mate!

Anonymous said...

thanks boss :)

Anonymous said...

Thank you so much.

Anonymous said...

thanks master! thats awesome

Anonymous said...

thanks a lot.best solution

Anonymous said...

thanks ...dost great!!

Anonymous said...

really helpful

Anonymous said...

Hey Thanks....

Anonymous said...

thnx bro..it really worked

Anonymous said...

thanks a lot :)

MDMALIK said...

really thanks a lot man.... it was taking me hours to get that value... ur simple awesome code really helped


thnxs

Anonymous said...

thanks a lot :)

Anonymous said...

doesnt work in asp.net 4!

Anonymous said...

Nice Article...it worked for me Thanks!!!!!

Anonymous said...

good

Anonymous said...

Thank u !!!!!!!!!!11
from Amiya

Unknown said...

there is an asier way i guess

Request.Form["yourtextbox"]

Anonymous said...

So nice of you great work.... after getting so much insult from my boss finally I got this thumbs up :)

Rahule bajaj said...

Thank's.... for solution

if (!IsPostBack)

txt_ReadOnly.Attributes.Add("readonly", "readonly");


chetan_

Anonymous said...

Thanks pal

Madhur Bhorali said...

Great work. Thanks dear.

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash