Textbox not updating on page load ASP.net - asp.net

I have a dynamically built table on the page load of my web page containing textboxes with defaults that can be changed by the user. There is also some validation on the user input values (e.g., input can not be less than zero). When the user submits the table, only valid inputs are stored, and the page is refreshed pulling the stored values to populate the table when it is rebuilt. For some reason the textboxes will still display invalid inputs after submitting and reloading the page even though they are not persistent, and this problem does not happen for labels.
Is there something specifically built into the textbox control causing this issue? Is there somewhere storing the state, perhaps (I tried setting the ViewState flag to false)? Is this just some part of the page lifecycle?
The textbox is built in a method called from the Page_Load event
void Page_Load(object sender, EventArgs e)
{
//page initialization...
ValidateData(); //This is where the current data is verified, and stored if valid
BuildTable(); //This takes that stored value for the text
}
void BuildTable
{
tbTable = new Table();
tbRow = new TableRow();
tbCell = new TableCell();
TextBox tb = new TextBox();
tb.text = some stored value;
tbCell.Controls.Add(tb);
tbRow.Cells.Add(tbCell);
tbTable.Rows.Add(tbRow);
divFromPage.Controls.add(tbTable);
}
If more information is necessary to help with this issue, just let me know.

Edit :
After reproducing your problem i came to the following conclusion.
I believe the problem is not the viewstate but asp is just repopulating these values because the form data which you submited has the same name as the input element which you are returning.
Even though you generate them dynamicly since you add them to the same div the result is always the same. This leads me to 2 solutions :) :
My solution nr 3 still stands , i tried it out and on button click redirecting works as intended with no posted data creeping back into my textbox.
This statement takes care of that :
Response.Redirect("~/test.aspx");
Alternativly you could generate a random ID to make sure the inputfields names you are returning are different from the ones that were submited. You wouldn't have to change all the names but if the table ID for example is different the entire table won't get populated with the submitted data anymore.
Be aware that you might need a if(IsPostBack) in your pageload because your data will be lost right after pageload(unless you handle the saving before that)
If you are doing a postback asp.net uses it's viewstate to maintain all the textbox,textfields and all other form elements(including input type hidden).
I currently see 3 solutions to your problem :
You could do what ivowiblo suggested, before you load the data from the database( i assume you do) you could wipe all the textfields. On a first time visit this will be pointless off course since they are blank. But after a postback it will actually wipe the textfields.
You could disable the viewstate on those textboxes so asp will not persist the state. I'm not sure why your test didn't work but it should work. If you choose this approach feel free to edit your question with some actual code
My personal favorite : Redirect the page. In stead of just returning the result and causing a postback to the page do the following : after the user hits the save button you can save the data and then do a Response.Redirect to your current page. The browser will be redirected to the page and no viewstate will be involved.

Just figured this out, kind of a D'oh moment, I just populated the data in the PreRender event and it works just fine, thanks for all the input.

Related

Resetting my textboxes does not work all the time. How to fix?

I am using asp.net 3.5 (codebehind c#). My users are running IE 7 or 8. I have a data entry page with several TextBoxes.
When I want to clear the all the TextBoxes, I do a sever.transfer back to the same page.
Maybe 99% of the time, this clears the TextBoxes. Every so often it does not.
I know that a roundtrip to the server has happened because:
The dropdown lists and radiobuttonlists on the page get reset, and
DB processing happened as shown by a record having been saved to the
DB.
The inconsistency is confusing me.
What could be causing the TextBoxes to persist data in this way and how can I prevent it from ever happening?
It is resulting in users saving the same data twice.
I don't think this would have anything to do with it, but there are also various js functions on the page for validation, capitalizing the first letter of an input, etc
As this MSDN article describes the Server.Transfer method, this is probably not the right way to empty the TextBoxes. The method explicitly keeps all state information.
A better approach is described within this answer:
foreach (var item in Page.Controls)
{
if (item is TextBox)
{
((TextBox)item).Text = "";
}
}
You will keep the roundtrips to just one, and you could keep all other user inputs as they were, if you like.

(New to ViewStates) What is the best way(s) to save a dynamically generated control into the viewstate?

I am creating a web application in Asp.net and I'm still fairly new. I'm just starting to wrap my head around the basics of the ViewState. In my application, people are searching through a database and I give them ways to narrow their search. When they have entered a valid search constraint (ex: date past 10/1/11) I dynamically add another set of controls allowing them to add another constraint. I want to save the contents of the previous constraint (a set of Controls) so that I can still have it on the webpage when they enter the next constraint.
If it makes any difference, one constraint set consists of a drop-down list of attributes, a few literal control, and one or two text fields depending on what attribute was chosen from the drop down list.
How would I go about this?
Thanks so much guys.
The easiest way to track viewstate for dynamic controls is to recreate the controls in OnInit and assign the same ID to the controls every time the page is posted back. If the controls are assigned the same ID each time they're created, when the ViewState is loaded, the controls will be repopulated.
protected override void OnInit(EventArgs e)
{
TextBox txt = new TextBox();
txt.ID = "txt1";
this.Controls.Add(txt);
}
EDIT
To make things easier, try using the DynamicControlsPlaceHolder. Just put the control on the page, and it will persist the controls and their values behind the scenes:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
Check out this link:
https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx
http://www.codeproject.com/KB/viewstate/retainingstate.aspx
ViewState for dynamic controls is still maintained by the ASP.NET framework. Just make sure you add them during init or preinit, because viewstate is loaded for every control between the init and load stages.

Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate

I'm currently working on a dynamic core for several webprojects. It has a core that uses a treeview and a menu. And then for each specific projekt it loads several different wuc into a maincontent. Some business projects use business related wucs while others uses different ones. So the span of wuc's is really big.
Now to my problem, whenever a user press a menuitem or a treeitem it loads a wuc to the maincontent linked to that object.
But I'm having some viewstate errors and i've been looking around for 2 days now and none of the solutions explained are working for my projekt.
All my wuc has to have viewstate enabled.
Cycle is ->
Page(Control A) does postback with variable to change control to ControlB in wucPanel(UpdatePanel).
OnLoad LoadRequested Wuc.
Current code is
protected void Load_Page(object sender, EventArgs e)
{
//Code to decide which wuc to load.
UserControl wucc = (UserControl)Page.LoadControl(sFilePath);
ParentControl.ContentTemplateContainer.Controls.Add(wucc);
}
I've tried several fixes like adding diffrent ids to the wuc, but this either disabels the internal functions of control like handlers etc or generates the same viewstate error.
One solution i found was to load ControlA and then just removing it and then load ControlB. But this disabled the scripts for my 3rd party controller (Telerik).
I've also read about having diffrent PlaceHolders for each typof but since i expect havign up to 50 diffrent Controls I don't feel this is gonna help me.
And moving from Page_Load -> Page_Init generated the same error.
Error:
Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.
In your case Anders, you still need to add the old control to your page in the init method along with the new control that you now want to add. Keep a reference to this old control that you have just added in a class level variable. So something like
Control _oldControl = null;
protected void Init_Page(object sender, EventArgs e)
{
//Code to decide which wuc to load.
UserControl wucc = (UserControl)Page.LoadControl(sFilePath);
ParentControl.ContentTemplateContainer.Controls.Add(wucc);
_oldControl = wucc as Control;
//Now add the new control here.
}
//override the LoadViewState method and remove the control from the control's collection once you page's viewstate has been loaded
protected override void LoadViewState(object savedState)
{
base.LoadViewState(savedState);
ParentControl.ContentTemplateContainer.Controls.Remove(_oldControl);
}
Hope this helps. If it did, please check the checkbox next to this answer to accept it and vote it up if you like :)
In order to avoid ViewState related errors please make absolutely sure that in Page_Init you create the same control tree that was created the previous time ViewState was saved i.e. the previous postback. Simple page life cycle:
Page Init - create the control tree
- View State is loaded and applied here
Page Load - already loaded view state, you can do modifications to the control tree here
- Save View State
Page PreRender
For what it’s worth I recently had the same problem.
My scenario was as follows.
A fixed panel of filters (dropdown lists and textboxes) which built a search SQL string. On submission of the search consequent results were displayed in an editable gridview beneath.
On editing the gridview I cold effectively change the state of a database record thus removing it from the gridview under the filters previously chosen. In some cases this resulted in no results being returned thus causing me to hide the gridview.
I then found that if I used the new state of the record in the filter and resubmitted the search that error sometimes occurred.
The problem I eventually found had nothing to do with enabled viewstates etc but simply that the empty gridview, though no longer visible (changed programmatically), had not been rebound to a null datasource.
This appeared to cause the conflict and the error.
So it appears as though in my case the viewstate issue arose from a non-visible gridview that contained non-refreshed data.

What are the main differences of defining a control statically or creating it dynamically on page load?

I am working on a project which creates controls dynamically for a form in the page_load event, loads in their current values from the database and saves their values (using FindControl) when the user clicks the continue button.
When I added a control statically in the .aspx page and followed their same procedure of loading the value in the page load and saving it on the button press I found that the value would not save correctly. It seems that it wouldn't save because the click event fires after the page_load, so the page_load of the post back reverted the value and the user entered value was not saved.
The strange thing is that by changing the control to be dynamically created just as all the other controls on the page and keeping the loading and saving the same it now works. Even though the page load still creates the control with the old database value.
It seems like a very fundamental asp .net feature here but i'm just unclear as to what is going on. I suspect it is to do with the timing of creation and maybe when the view state kicks in.
Static page controls are created just like dynamic page controls. The difference might be coming in your Page_Load. Whenever you postback all the controls are created afresh which means they are created with their initial values. This happens because after creating the controls asp.net throws away the controls/objects.
So, when the request comes, the first thing that asp.net does it to recreate the controls by looking at their definitions (in the designer files). On each postback they are created and initialized again losing their state in the process.
But after creating the controls Asp.Net loads any viewstate that is sent along with the request which makes people think that the state is always saved at the server.
What might be happening is that either the viewstate is not enabled for your control (in case they are created in designer), in which case you may try using EnableViewState property to true of the control.
Or, when you're doing a Page_Load, you're forcefully re-initializing everything. And in process losing all the control data. If you could post the logic of Page_Load, it might get clarified.
Make sure that:
you are not setting the value again for the static control in Page_Load. The dynamic control are probably getting around it by grabbing the ViewState and form values at a different stage in the lifecycle.
The dynamic controls are added After the static control. Or at least they are added in a different container. Placement in the control's collection can affect the ViewState, although it doesn't look like your scenario / since what you mention seems to be more about the values in the current post.
The save is happening After the Page_Load in response to the corresponding event.
I've run into similar problems in the past (quite a few times actually), but what helped me the most is understanding the ASP.NET Page Lifecycle.
Microsoft has an article on it which describes it pretty well, but this post by Solomon Shaffer really cleared up everything.
I suggest reading them both and coming back with additional questions regarding to a particular state, when to load/save data etc..
Hope this helps.
Marko
Note that you may want to use Page.IsPostBack property to avoid reinitializing values on button clicks and other events.
private void Page_Load()
{
if (!this.IsPostBack)
{
// Assign values to the controls.
}
}

dynamicaly loaded controls lose values (ASP.NET)

I've got a strange problem concerning dynamically loaded controls in a asp.net application.
So there is a control where user have to select some items and/or do some text input (textboxes). The control depends on a single dropdown list element.
So user A chooses a certain value in this dropdownlist "controlselector" -> on of the many controls will be loaded. After that the user clicks on save and then it should save it to the database.
The problem is following that not every item is saved into the databse.
I create and recreate the control at every Page_Load, i've turned autopost back on the "controlselector" but the control is loading at the page_load event. When trying to save the elements are empty, but not every item :(
MyCustomControl:
FillElements(someParameter)
{
//fill some lists, dropdowns, checkboxes or whatever with some values from db
}
Foo Save()
{
//Save selected input(also some textboxes)
//and return an object
return foo;
}
Page:
Page_Load()
{
PlaceHolder.Clear();
//with Createpath the path to the control is created and loaded
PlaceHolder.Controls.Add(LoadControl(CreatePath(Selector.SelectedValue)));
//some methods are started to fill some lists in the control
((MyCustomControl)PlaceHolder.Controls[0]).FillElements(someParameter);
}
Save_Button_Click()
{
var myFoo = ((MyCustomControl)PlaceHolder.Controls[0]).Save();
myFoo.DoSomethingElse();
}
it seems that sometimes the page remembers values and sometimes not... ver strange everything
thanks
[EDIT]
The problem i see that, there is 2 time a dynamic fill action.
1.) deciding which and then loading the custom control
2.) filling the custom control with the parameters
Page_Load is too late in the life cycle to create dynamic controls, because state is restored to controls before the load event. This means you need to create your control earlier, or ASP.Net won't see it when it comes time to restore state. Try creating them in the Init event instead. Or, even better, try one of these options:
Create one custom control type that adapts itself as needed and have a normal instance of the control on the page.
Place all controls on the page but only set Visible to true for the one you care about.
You need to check for "IsPostBack" if it is you dont want to recreate those controls... its killing the values etc that you have in them.
try changing your code to something like this.
Page_Load()
{
if(IsPostBack == false){
PlaceHolder.Clear();
//with Createpath the path to the control is created and loaded
PlaceHolder.Controls.Add(LoadControl(CreatePath(Selector.SelectedValue)));
//some methods are started to fill some lists in the control
((MyCustomControl)PlaceHolder.Controls[0]).FillElements(someParameter);
}
}
thanks for your help, but the problem was on something totally different
the items which were loaded dynamically into the dropdowns which where also loaded dynamically, had some "\n" special character, but not every item
thats why not every item got lost just few
i don't know if i should/can mark this as answer, because the problem was on a other place

Resources