ASP.NET TextBox Control - Get the default text value in code behind?

peteski picture peteski · Feb 13, 2009 · Viewed 9.9k times · Source

I have been spying the MSDN and can't see a property/method for TextBox that allows you to get the default text value that was set on a field; I want to be able to compare the current txtMyTextBox.Text to the default value (like this psuedo code shows):

var myValue = (String.Compare(txtMyTextBox.Text, txtMyTextBox.DefaultText) ? "" : txtMyTextBox.Text)

Is this something which exists in the ASP.NET control? Or am I asking too much? :)

Thanks for any help (as always)!

Pete

Answer

Marc Gravell picture Marc Gravell · Feb 13, 2009

By DefaultText do you mean the initial text before editing?

Perhaps declare this in a constant / field / etc somewhere, and set it programatically rather than in the markup - i.e. in the first load, txtMyTextBox.Text = defaultText; - then later you can just compare again to defaultText to track changes.