Hey Guys,
I have a textbox which had readonly property set to true.
This textbox is used to take LongDate as it's input from a pop up calender. When i click on the button, it gives an error
"String was not recognised as valid datetime"
But when i set the textbox's readonly property to false it works completely alright, it returns values fromthe database.
Here is the code which i used

TextBox date_search = (TextBox)Page.PreviousPage.FindControl("tbMyDate");
            DateTime dateData;
            dateData = Convert.ToDateTime(date_search.Text.Trim());
            TextBox2.Text = dateData.ToLongDateString();

Why does it work when the readonly property is set to false????

Dani AI

Generated

The behavior is caused by how ASP.NET treats a server TextBox when its ReadOnly property is true. With ReadOnly=true the control will render the HTML readonly attribute, but ASP.NET will not update the server-side Text property from the posted form data. If the calendar sets the value on the client, that client-side change can be ignored on postback so the server sees an empty or unexpected string — which leads to the "String was not recognised as a valid DateTime" error. This explains why it works when ReadOnly is false.

and were right to check for empty or culture-mismatched values, but the underlying issue is the ReadOnly handling. Fixes that keep the UI read-only while allowing the posted value to reach the server:

  • Do not use the server ReadOnly property. Instead add the HTML readonly attribute so ASP.NET still accepts posted data:
tbMyDate.Attributes["readonly"] = "readonly";
  • Store an unambiguous value (ISO 8601) in a hidden field and parse that on the server. This avoids culture parsing issues.

  • Parse defensively on the server using TryParse/TryParseExact and an explicit culture/format to avoid exceptions:

using System.Globalization;

string posted = Request.Form[tbMyDate.UniqueID];
DateTime dt;
if (!string.IsNullOrEmpty(posted) &&
    DateTime.TryParseExact(posted, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
{
    TextBox2.Text = dt.ToLongDateString();
}

Troubleshooting tips: set a breakpoint and inspect Page.PreviousPage.Request.Form to see the raw value; check the control UniqueID when using PreviousPage.FindControl; do not use disabled attributes (they are not posted). These steps will confirm whether the value was posted and help choose the best parsing/transfer strategy.

Recommended Answers

All 2 Replies

First you need to check whether the value returned by 'date_search' does not contain blank value by putting break point.

Also the datetime parsing in C# is based on the local date time settings. Therefore if your local date format is dd/mm/yyyy and if you enter date in mm/dd/yyyy format then this error will be happened.

TextBox date_search = (TextBox)Page.PreviousPage.FindControl("TextBox1");
        DateTime dateData;
       
        if (date_search.Text!="" )
        {
           dateData =Convert.ToDateTime(date_search.Text.ToString());
           TextBox2.Text = dateData.ToLongDateString();
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.