TextBox Text is empty on postback - asp.net

I have a problem that occures from time to time but I can't figure out the reason why it's happening.
I have a txtFilter. I enter the value in textbox hit the asp:button that posts the form back to server and txtFilter.Text property is empty. The control where texbox resizes is added to the page dynamically but it depends on a request parameter so it's not the case that I'm not loadding some controls that were loaded before.
The strangest part in this situation is that even though txtFilter.Text is empty,
Request[txtFilter.Text.UniqueID] has the value that I entered.
Any ideas?

If you are dynamically creating/adding the textbox control you need to do it in page_init so it participates in the normal page event lifecycle. This link may help: http://support.microsoft.com/kb/317794
Request[controlid] will always contain the value that you entered since you are accessing the raw HTML form post data.

Related

ViewState Management in ASP.Net

i was recently surfing the msdn for the session state management tools, i came accross viewstate, which can be used the retain the page or controls value accross the page postback, so i created a simple application, which contains a asp:textbox and asp:button now i made the EnableViewState="false" for textbox and run the page, entered some values and clicked on the button, the page postbacked but the value was retained, i thought that would be because the pages viewstate property is enabled, so i changed the EnableViewState="false" in the page directives, and run the page, still the textbox value was retained in the textbox accross the postback, can anyone tell me with small example how the does viewstate work in my scenario
ViewState can probably not be explained with a small example ;-)
I'd recommend to read this article: Truly Understanding ViewState
The TextBox is rendered as input control, so the value is post back and set again to the TextBox.
The viewstate have a meaning on the TextBox for the other attributes that you can set it pro grammatically, or in case that you make it hidden and you like to keep the content of it.

Get element text before page reloads in ASP.net web forms

I am unfortunately having to work with asp.net web forms. I have a label that has a different Text property every time the page loads. I have a button that is clicked. I have double clicked on the button and it has shown me a code view.
I get a reference to the label via labelID.Text, but it refers to the value of the text that is about to be displayed on the next page load. How would I get the text of the value when the button was actually clicked? Or is web forms not advanced enough for that.
Search where the labelID.Text is modified (maybe Page_Load event), and save the text before in a global variable.
It sounds like on every page load or postback, somewhere there is code which applies a new value to labelID.Text. Where is that work being done? Page_Load?
In any case, wherever that work is being done, you most likely have access to both the existing text value of the control, and the new text value you're about to give it.

ViewState - how to persist state between two pages

On page1.aspx i hava a textbox with id="tb1"
On page2.aspx I have a textbox with id="tb2"
page2.aspx is set as postbackurl for page1.aspx. For both labels enabl;eViewState is true.
Shouldn't the text I write in page1 also appear in the label on page2? Can I implement this without looking into Request headers, and setting the text myself?
ViewState is a blob of Javascript that is persisted for a single page, not across multiple. When you use the PostBackUrl property to post to a different page than the initial page, you need to perform your own validation, and pull property values from the controls on the "previous page":
// Get the value of TextBox1 from the page that
// posted to this page.
text = ((TextBox)PreviousPage.FindControl("tb1")).Text;
Also, ViewState is honored on a per-control basis so even if what you wanted to happen was possible, the fact that your TextBoxes have two different IDs would mean ViewState wouldn't be able to match them up...

ASP.Net Web Form + Ajax Data lost on post back. Possible cause ? Ajax or changing visible property

I have two Repeater controls, each hosted in a user control. Both user controls are contained in the same aspx page. Only one User Control is visible at any one time. The repeaters are comprised of checkboxes, and text boxes for user input.
The aspx page is configured with an Ajax ScriptManager; and contains several Ajax UpdatePanels. These UpdatePanels result in partial page post backs when text is changed in a textbox control in controls on the aspx page (this is not the behaviour for the Repeaters in the User Controls). Through use of several AsyncPostBackTriggers, various controls contained in the other UpdatePanels on the page have their content refreshed in response to the partial page post backs they are configured be notified about.
Depending on a radio button group selection, I set the visible property to true or false – as appropriate for the User control containing a repeater control. The Repeater control is then populated with data using databinding. All of this works.
However, when the Submit button is clicked, the Repeater control contains no data.
Given that I am not dynamically adding the Controls containing the Repeater controls (but using Visible true / false). I would have thought that the State of the fields and the data in the visible control would be preserved during the post back.
The User Controls are contained within the UpdatePanel that contains the Submit Button.
I have explicitly Enabled View state without any effect.
Am I correct in assuming that I should not have to do any explicit handling of data changes the user makes (via client side script and manipulation of an Data Structure Representing the Repeater Data); and the View State should maintain the data I need to access on the server when submitted?
I do not believe that it is the User Control visible state changes that are causing the issue because when the page is initially loaded on of the User controls is populated with dummy rows (so it displays).
I am suspicious that because the visible state of the controls is changed during partial page post back, that the Page View State ends up with no knowledge of the User Control and therefore cannot track its data (or changes).
I have investigated a lot of similar sounding posts but so far do not feel that I have come across a solid explanation that can help me understand and fix the issue.
Hopefully someone can help.
I must be the only person who was unaware of the finer points of DataBind() method; be it called on a specific control being bound to data; or on the page itself. I hope the below helps others who find themselves in a position where data binding appears to be inexplicably lost'.
My problem was that my page was pretty complex. It has several User Controls on it. The problem was that in these User Controls, I was calling Page.DataBind() in the Page_Load event handler to 're-bind' the page data on PostBacks. This resulted in the Repeater control located on another User Control, to lose its data binding.
Removing the Page.DataBind method call from each of the User Controls on the Page_Load event handler method resolved the problem. It had nothing to do with Update panels / refreshes etc.

I have to do two seemingly mutually exclusive things on leaving an asp:textbox. Please help me get some clairity

This project has gone from being a simple '99 Ford F-150 to the Homer.
I've got controls with a gridview with textboxes for data entry.
All the user controls on the pages are in AJAX updatepanels.
User types in a database column or budget entity or some other financial thing they want to include in the report.
The textboxes in the gridview have autopostback = true set.
overly long background info
When the user leaves the textbox, during the postback (triggered by onTextChanged) I do some validation back on the server on their entry - regexs, do they have rights to that column, is that column locked, etc. If it fails, I put a error message next to the textbox. If it passes, I wipe out any title or error that used to be next to the code.
Focus is getting lost from the postback if they're tabbing out of the box, rather than going to the next textbox in the gridview.
So to fix that I need, if their leaving the tb via the tab key, to also figure out what textbox or gridviewrow they're on, if they're not on the last row, and after the validation and labeling, put the focus on the textbox in the next row.
I can't figure out how, in ontextchanged, to find what caused me to leave the textbox, so I'm thinking use javascript onkeyup to test the key pressed and then find the next box etc, but the ontextchanged fires first and then the js never does, and also, since the control is all AJAXed, the javascript can't find the textboxes because when you enter the page everything is collapsed (the requirements people loooove to collapse and expand things), and so when it's expanded, all the 'new' textboxes are up in the viewstate stuff in the page source, and not down where javascript can see them.
The questions
So I'm wondering if I can have an onblur in the javascript that can trigger a postback where I can do my validation and such, and either 1) include the keypressed or pick it out of sender in the event or 2) followup the onblur with onkeyup and somehow figure out what textbox is next on the grid and throw focus there.
Or, is there another .NET based approach that could work for this? In terms of tearing the whole thing down and starting from scratch, I couldn't sell that to the bosses, I'm past the point of no return as far as that goes.
Does changing the one textbox value change anything else on the screen besides the error message/title associated with the textbox?
If not, my first thought would be to stop using the AutoPostBack and change to a Page Method hooked up to the onblur event. This should fix your tab issue and also reduce the back and forth between client and server.
Here's a blog post that talks about update panels and also Page Methods; The blog also includes a lot of useful posts about using javascript with .net, ajax, etc.

Resources