views:

4144

answers:

7

Hi all!

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

+2  A: 

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.

LeJeune
+3  A: 

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.

Marc Gravell
Hi Marc, Yeah! I just meant the value that would be set from: <asp:TextBox ID="txtMyTextBox" runat="server" text="My Default Text" />
peteski22
So perhaps put the "My Default Text" in a constant in the code-behind ("DRY"), and work from there...
Marc Gravell
+1  A: 

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.

Andy Rose
I had a horrible feeling that there wouldn't be such a property, shame I think it would be nice!
peteski22
@peteski22 - you could always develop a custom control to extend the textbox control to incorporate this behaviour using page viewstate.
Andy Rose
+1  A: 

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.

cyberbobcat
+1  A: 

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

Jakob Christensen
A: 

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.

Charlie
I am setting .Text initially, this could be done via the page load event, or in my mark-up (<asp:textbox text="foo" .. />).I suspected there was no default property, hence the question - I didn't mean to imply there was in the pseudo code. :)
peteski22
Gotcha, yes unfortunately you are going to have to capture your default text value elsewhere (Hidden Field, ViewState, Session Variable - all have Adv and Disadv) so that you can do a comaprison once text has changed.
Charlie
+1  A: 

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.

CraigTP