I have a form with 3 textboxes that I create dynamically.
The 3 are txtAge, txtUnits and txtPrice.
txtPrice is set by getting a value using txtAge's value and multiplying it by txtUnit's value.
The controls are currently created OnInit (though I've tried doing it on Page Load and it doesn't change anything).
During my Page_Load method I find the textboxes using the unique names that I give them and when I check to see if they have a value, they are blank, however once the page loads they have a value just like I would expect them to.
For example if I change txtUnits from "5" to "4" and then cause a postback, on Page_Load txtUnits has no value but by the time the page Renders the value is "4".
I thought the order went like this:
Page.Init
Page.LoadViewState
Page.ProcessPostData
Page.Load
Change events for controls
Server side validation
Button.Click or Button.Command events
Page.PreRender
Page.SaveViewState
Page.RenderWhich means by Page.Load I should have data in my textboxes. What am I missing?
![]() |
0 |
![]() |
So.....no one?
![]() |
0 |
![]() |
Hiya,
I'm guessing its because you are creating the control dynamically at each page refresh (ie postback). Could you place the code that creates the controls in a block like
if (!page.IsPostBack)
{
// create control here
}
As I say this is an untested guess, but maybe worth a try.
HTH
![]() |
0 |
![]() |
I appreciate the advice, however it won't work for me because if I don't recreate the controls on Postback then they definitely won't have any values.
The problem is that the controls are being recreated and they are being reassigned their values just like they should be...but that value is not being reassigned until after the code I need to check the new value has run through.
![]() |
0 |
![]() |
Sorry, I do not know what is the missing, as a solution, I worte a demo for your reference,
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { TextBox txtTemplate = new TextBox(); txtTemplate.ID = "txtAge"; txtTemplate.AutoPostBack = true; txtTemplate.TextChanged += new EventHandler(txtTemplate_TextChanged); form1.Controls.Add(txtTemplate); txtTemplate = new TextBox(); txtTemplate.ID = "txtUnits"; txtTemplate.AutoPostBack = true; txtTemplate.TextChanged += new EventHandler(txtTemplate_TextChanged); form1.Controls.Add(txtTemplate); txtTemplate = new TextBox(); txtTemplate.ID = "txtPrice"; txtTemplate.AutoPostBack = true; txtTemplate.TextChanged += new EventHandler(txtTemplate_TextChanged); form1.Controls.Add(txtTemplate); } protected void txtTemplate_TextChanged(object sender, EventArgs e) { if ((form1.FindControl("txtAge") as TextBox).Text != "" && (form1.FindControl("txtUnits") as TextBox).Text != "") { string s1 = (form1.FindControl("txtAge") as TextBox).Text; string s2 = (form1.FindControl("txtUnits") as TextBox).Text; float f1 = float.Parse(s1);// please input the number in the TextBox float f2 = float.Parse(s2);// please input the number in the TextBox float result = f1 * f2; (form1.FindControl("txtPrice") as TextBox).Text = result.ToString(); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>P</title> </head> <body> <form id="form1" runat="server"> <div> </div> </form> </body> </html>
Hong-Gang Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |
That is pretty much exactly what I do (with some things in between obviously since the page is alot more complicated than that) and it's still not working correctly.
![]() |
0 |
![]() |
Maybe time to post your code??
![]() |
0 |
![]() |
No can do. (Non-Disclosure Agreement)
plus...the code is spread across multiple files (part is on a user control) and is split up amongst hundreds of lines of code.
:(
I suppose I'll just work at figuring out a different way to do it.
Thanks for the help.
![]() |
0 |
![]() |
jwright@watlow.com:
That is pretty much exactly what I do (with some things in between obviously since the page is alot more complicated than that) and it's still not working correctly.
I have tested my code, I am sure it works. Hope you test again.
For testing, please create a ASPX page with C# language, and switch to the "Source" view, copy and paste my code instead of the page code, and set this page as start page, and run.
Hong-Gang Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
![]() |
0 |
![]() |