0

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.

wchargin
  • 15,589
  • 12
  • 71
  • 110
jas
  • 537
  • 1
  • 7
  • 12
  • So, your controls continue having the values they have when submitted? You'll need to blank them in the Page_Load event – Ivo May 10 '12 at 23:14
  • how do i do that? they're already dynamic, so shouldn't they be blank in the page load by default? – jas May 11 '12 at 03:48
  • jas, i managed to reproduce your exact problem(huray :) ). Ill look into this bizar behaviar and get back to you. Out of curiosity : Have you considered using an asp:repeater? This component is usually used in these kind of cases. And as is often the case : choose the path of the least pain :) – Kristof May 11 '12 at 14:14
  • Edited my answer, lemme know if it makes any sense to you or not :)(gone in about 30 mins btw) – Kristof May 11 '12 at 14:26
  • Response.Redirect doesn't work for me because the user input only lasts for the life of the specific instance of the page and is lost when I redirect. The random id works, but it seems to me that if this is usual behaviour of textboxes, this should be a solved problem, no? – jas May 11 '12 at 14:56
  • To be honest your setup is kinda weird as in i can't see a practical problem that would require this solution. The behavior of textboxes getting filled up automaticly is something fishy which i can't explain on the spot. But it would make more sense to me if you had a repeater and wouldn't rebuild your page on every postback. – Kristof May 11 '12 at 15:05
  • Fyi : you could always try client side validators so you block the submit completely. – Kristof May 11 '12 at 15:05
  • I'm fairly inexperienced with .NET, so just to clarify are you suggesting I use static controls in a repeater, that I would modify on postback? – jas May 11 '12 at 15:18

2 Answers2

0

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.
Kristof
  • 3,267
  • 1
  • 20
  • 30
  • what do you mean wipe the textfields? since they are being built at that point, shouldn't they be blank by default? and in any case since I am changing the text property should that matter? I disabled viewstate on the parent div, the table, the row, column, cell and textbox using Control.EnableViewState = false For your third option the data persists for each instance of the page, and defaults back to the original values on a new one. Will that affect your solution at all? – jas May 11 '12 at 09:52
  • I'm guessing you do Control.EnableViewState somewhere from the codebehind. To avoid triggering this at the wrong momentum(page_init,page_load,page_render,...) you should just put it on your aspx as EnableViewState="False". Wipe the textfields meaning txtfirst.Text = "". They are blank when they are created by asp(check the Page_init event, they will be blank there). They get filled on page_load when the viewstate gets loaded. I am not understanding you correctly on your last remark : When you load up your page it should load it's data from the DB, no? – Kristof May 11 '12 at 11:15
  • Btw : to avoid further confusion. Could you post your code where you actually create your dynamic textboxes? – Kristof May 11 '12 at 11:19
  • Does that help at all? Do you need more than that? – jas May 11 '12 at 13:37
  • Could you post your entire Page_load as well? Are you building it up only on a non postback or everytime? – Kristof May 11 '12 at 13:49
  • I build it up every time, and it is the last thing to happen in my page load – jas May 11 '12 at 14:14
0

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.

jas
  • 537
  • 1
  • 7
  • 12