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. |
|||
|
|||
16 comments :
thanks dude
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
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
excellent sir...
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,
thanks a lot for the tutorial!
I have Check .....When I Click the Check Box.... Text Has to loaded to Password TextBox... Thanks In advance
thnks a lot
Note: Only a member of this blog may post a comment.