Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Introduction

Here I will explain how to get read only control values in codebehind using asp.net 

Description

If we set ReadOnly="true" property  for textbox it won’t give any chance to enter value or change the existed value .If we try to get data in textbox server doesn’t process the read only text box because the value of the text property is preserved in the view state between postbacks unless modified by server-side code. In some situations we don’t want give chance to user to enter data except from calendar control in that situation we need to set that textbox read only in serverside like this 

protected void Page_Load(object sender, EventArgs e)
{
txtStartDate.Attributes.Add("readonly","readonly");
}

 If set ReadOnly property like this in code behind now we have a chance to get the data from that read only control without giving chance to user to enter data in textbox

Now I will explain with simple example take two textboxes set tbStartDate textbox ReadOnly property in code behind and tbEndDate textbox ReadOnly property in aspx page and check

Add ajaxtoolkit reference to your project and Design your form like this


<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="script" runat="server">
</asp:ScriptManager>
<div>
<table>
<tr>
<td>
StartDate:
</td>
<td>
<asp:TextBox ID="tbStartDate" runat="server" /><br />
<ajaxToolkit:CalendarExtender ID="ce1" runat="server" TargetControlID="tbStartDate" />
</td>
</tr>
<tr>
<td>
EndDate:
</td>
<td>
<asp:TextBox ID="tbEndDate" runat="server" ReadOnly="true" />
<ajaxToolkit:CalendarExtender ID="ce2" runat="server" TargetControlID="tbEndDate" />
</td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="btnsubmit" runat="server" Text="Submit" onclick="btnsubmit_Click" />
</td>
</tr>
<tr>
<td>
StarDate Text:
</td>
<td>
<b><asp:Label ID="Label1" runat="server"></asp:Label></b>
</td>
</tr>
<tr>
<td>
EndDate Text:
</td>
<td>
<b><asp:Label ID="Label2" runat="server"></asp:Label></b>
</td>
</tr>
</table>
</div>
</form>
</body>
</html>


And write the following code in code behind like this


protected void Page_Load(object sender, EventArgs e)
{
tbStartDate.Attributes.Add("readonly", "readonly");
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
Label1.Text = tbStartDate.Text;
Label2.Text = tbEndDate.Text;
}


Now run the code and check how it will works we will get the only one textbox value that value belongs to textbox which sets read only property in serverside

Demo



Download sample code attached

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

16 comments :

Anonymous said...

thanks dude

abhijit salve said...
This comment has been removed by the author.
Anitha.M.S said...

Hi Suresh,,,
I added txBxId.Attributes.Add("readonly","readonly") in codebehind page but am getting the same problem. Not posssible to get the value in textbox after postback

murtaza farooqui said...

I have a textbox called paysalary ok

down to it i have another box of interest i want the interest to be calculated and displayed if i press tab from paysal to interest box... i have the logic for interest .. but how do i display it in another ttextbox

Anonymous said...

excellent sir...

Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...
This comment has been removed by a blog administrator.
Anonymous said...

Hey Suresh,
I am doing same thing but on buttonsubmit I am trying to save to DB table using tbStartDate.Text. There it says cant convert string to system.datetime.Any ideas?
Thanks Much,

Anonymous said...

thanks a lot for the tutorial!

Likhith S said...

I have Check .....When I Click the Check Box.... Text Has to loaded to Password TextBox... Thanks In advance

Jyotika Paul said...

thnks a lot

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.