Question

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

Was it helpful?

Solution

There is no built in way of retrieving the default value of a textbox during postback.

One option would be to use ViewState to store the value during the initial PageLoad and retrieving it from there during the postback to make the comparison.

OTHER TIPS

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.

There is no "DefaultText" property on a textbox (or any other control). You probably have defined the default through a constant string, so just compare the Text property to that constant string.

The only property you can check is the Text property. If you need to compare an original value then you would be best to store that as maybe a hidden field or session variable. You can then check this against anything in the textbox.Text property.

Put the original value in a hidden field or in viewstate.

The TextBox class only supports a .Text property, so your "default" value will have to be stored somewhere in advance of first rendering the page so that you can check the textbox's .text property when the page is posted back. This "default" value could be stored in a cookie (if small enough), in the ViewState of the page, in a hidden form field on the page, or even in the application or session state.

TextBox does not have a DefaultText property, so I am confused. How are you setting a default text value? If you are just setting it in the code i.e.

<asp:TextBox ...>Default Value</asp:TextBox>

Then it will be the value of the .Text property.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top