TextBox behavior on postback - c#

I am returning to ASP.NET after a long hiatus, and am sure I'm doing something simple wrong, but I can't seem to sort it out. I have a page with a few controls (a few Literals and one TextBox), the values of which I am populating from a database query in Page_Load. When the value of the TextBox is changed, it correctly does a postback and fires the TextBox_TextChanged method, but it also seems to re-execute Page_Load, so the new value entered by the user is overwritten with the original value from the database. The only way I seem to be able to find to prevent this is to wrap the line that sets the control value in an if (!this.IsPostBack), which works fine, but I'm curious if there is a more elegant way to do this.
Thank you in advance,
Steve

What do you mean, "When the value of the TextBox is changed, it correctly does a postback and fires the TextBox_TextChanged method?" What causes the postback?
Beware that the TextChanged method doesn't work like JS (or similar) -- no check is done on each keystroke. It actually only fires on a full postback and the text is then checked for changes.
Otherwise, you've answered your own question by checking for a postback.

Related

Enable/disable TextBox on FormView.ModeChanged

I have DropDownList and a TextBox in the EditTemplate of my FormView. All I want is to enable/disable the TextBox based on whether the first entry of my DropDownList is selected:
When the FormView is switched to Edit mode by the user
When user changes the selected item of the DropDownList during Edit mode.
I have achieved the second one through JS and that's working fine, but the first one is proving too difficult. I've tried to do this in ModeChanged event of the FormView, but for some reason the following call returns null in the event:
MyFormView.FindControl("MyDropDownListID");
What am I missing here?
(I'm making sure that MyFormView.CurrentMode is FormViewMode.Edit before making the above call)
One of those times when you pull your hair for hours with a problem, then post it on SO and find the solution in the next few minutes. Anyone else trapped into this, the problem is that the controls of a databound FormView aren't created yet at the time of ModeChanged or Page_Load. You must call the above line in DataBound event and it will work fine.

Identifying Postback event in Page_Load

Using the code posted as answer at this question, it is possible to find the control that caused the postback action.
However, it is possible to check which event was called by this control? For example, identify an editing event fired by a GridView before the equivalent method (editing) be executed.
Thanks in advance.
It depends if the client code is providing this information. The standard GridView, for example, sets both __EVENTTARGET and __EVENTARGUMENT when a sort header link is clicked:
Request.Form["_EVENTTARGET"] contains "ctl00$MainContent$GridView1"
Request.Form["_EVENTARGUMENT"] contains "Sort$id"
where the __EVENTARGUMENT contains information that this is a Sort operation, and the column to be sorted on, separated by $.
For paging the __EVENTARGUMENT value looks similar:
Request.Form["__EVENTARGUMENT"] contains "Page$2"
Note though that not all controls populate __EVENTARGUMENT on postback, so you'll have to test.

ASP.NET - Dynamic Control Values & Postback

I'm trying to persist the contents of a textbox through a postback, and I've exhausted all of my effort but can't get it working right.
What should happen is:
User selects a radiobutton
Depending which button was
selection, a usercontrol is loaded
to specify some data and a viewstate
to say which enum type it's
equivalent to.
When they click
save, if the UserControl is just a
textbox input - the simplest), the
contents are read and saved, then
saved to the database with the
format(the radiobutton choice) so
they can be deserialized again
later.
The page posts back, and
the value and format are read from
the database, then the right control
is loaded.
The problem is - the first time the page posts back, it works. Every other postback it resets to the default value of the textbox.
I have a very similar setup elsewhere, so I'm thinking it might be a minor thing I'd never think of. There's a lot of code, so it might be easier to talk about what to do (load the dynamic control, populate the values etc) rather than how to do it.
There was actually a bug in my original code which meant it would never have in the way I was using the modified version. Apparently state is restored in Page_Load, so any controls need to be initialized by to have their values restored.
Creating the control in Page_PreLoad, then populating it after Page_Load solved the problem.

Binding Textboxes

I have been binding textboxes in a winform with C# to a dataset. Whenever the data doesn't validate with what the database except it silently forces the focus to remain on the textbox. How can I catch the validation error and tell the user about it (and free the focus)? The BindingSource event OnDataError isn't fired.
I had a similar problem once. The focus remained in the textbox which was binded to some numeric database field when the user modified text in a textbox and then deleted it so the text property was an empty string. I solved it with something like:
textbox.DataBindings["Text"].NullValue = "";
It solved the problem for empty inputs. I don't know if it would be of any use in your case, but I'd be also interested in more general solution.
Here's also some related question on SO:
Data-bound TextBox: can't exit
Never rely on just what "Visual studio has done for me" if you don't fully understand what it's doing. I would strongly urge you to take the time and figure out how to do what it is you want to do all by yourself (meaning without designer generated code). To get you started, there are some events on the TextBox that can help you out. Start here:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.validated.aspx
Specifically the validating and validated events should be what you're looking for.

Form post doesn't contain textbox data [ASP.NET C#]

I have several "ASP:TextBox" controls on a form (about 20).
When the form loads, the text boxes are populated from a database.
The user can change the populated values, and when they submit the form, I take the values posted to the server and conditionally save them (determined by some business logic).
All but 1 of the text boxes work as intended.
The odd box out, upon postback, does not contain the updated value that the user typed into the box.
When debugging the application, it is clear that myTextBox.Text reflects the old, pre-populated value, not the new, user-supplied value.
Every other box properly shows their respective user-supplied values.
I did find a workaround.
My solution was to basically extract the text box's value out of the Request.Form object: Request.Form[myTextBox.UniqueID], which does contain the user-supplied value.
What could be going on, here?
As I mentioned, the other text boxes receive the user-supplied values just fine, and this particular problematic text box doesn't have any logic associated to it -- it just takes the value and saves it.
The main difference between this text box and the others is that this is a multi-line box (for inputting notes), which I believe is rendered as an HTML "textarea" tag instead of an "input" tag in ASP.NET.
Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?
this happens to me all the time.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// populate text boxes from database
}
}
I would second Jonathan's response I would check your databinding settings.
If you do not need ViewState for the textboxes (i.e. no postback occurs until form submit) then you should disable it.
It sounds like you are not having problems saving the data (since you said you have managed to get the control to read the correct data back). Therefore, I would say the problem loads in your databinding code.
Remember the order of the page lifecycle, and where you are databinding your form.
PreInit
Init
Load
Your Control Event Handler
If you are reading the value in the Control Event handler, yet databinding in Init or Load, you'll have the old value.
The trick is to always databind in the correct event, or check for postback and don't databind then.
Are you initially loading the data only when !Page.IsPostBack? Also, is view state enabled for the text box?
I had almost forgotten to check the ViewState, but ended up remembering to verify that it wasn't disabled before making my post here on SO. I even set EnableViewState="true" to make sure.
I did find the solution, and it coincided with most of the answers here. The form was indeed loading its data more than once (which is intentional behavior). I implemented some special code for this field, and all is well.
Thanks for your replies, all!

Categories

Resources