Why can Controls be found on an OnClick Event, but not in Page Load?

ZosimaD

New Member
So I'm assuming this has something to do with the Page lifecycle, and maybe that Controls aren't yet bound on Page Load? Honestly I'm still learning about the ASP.NET Page lifecycle, when binding occurs, when you can access certain data and when it's too early/late, etc. I'd just like a good explanation on why the following won't work. Assuming a method that recursively searches Controls, beginning with a RepeaterItem, looking for a TextBox, like so...\[code\] private void FindMyTextBox() { foreach (RepeaterItem repeated in myRepeater.Items) { TextBox txtPercentage = (TextBox)FindControlRecursive(repeated, "txtPercentage"); . . . } }\[/code\]....where....\[code\]public static Control FindControlRecursive(Control root, string id){ if (root.ID == id) { return root; } return root.Controls.Cast<Control>() .Select(c => FindControlRecursive(c, id)) .FirstOrDefault(c => c != null);}\[/code\]...why will this work called from a method bound to an \[code\]OnClick\[/code\] event, like....\[code\]<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="Validate" /> protected void Validate(object sender, EventArgs e) { FindMyTextBox(); }\[/code\]...but not when called from Page_Load, like....\[code\]protected void Page_Load(object sender, EventArgs e){ FindMyTextBox();}\[/code\]
 
Top