I have a control that I want to be readonly because another control will populate it. Then, on postback, I want to extract the controls value. To get at the controls value it must participate in __ViewState.
The answer I found was to set ReadOnly = false, which is the default value, then set the control to readonly by adding a readonly Attribute on page load.
In this example I have a TextBox control for holding a date value that is selected from the Ajax Toolkit’s CalendarExtender control. I don’t want the user to type in the date because I don’t want to deal with validation.
Here is the definition in markup of the TextBox, CalendarExtender, and ImageButton that is selected by the user.
Here is the server side code for adding the readonly attribute.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ToDate.Attributes.Add("readonly", "readonly");
}
}
I ran into this problem a few weeks ago in a project. I coded around it (chsnged the process) but I wish I had thought of just adding the HTML attribute instead of using the control property. Thanks for the tip!
ReplyDelete