Walk all controls on a page [modified]
-
I've created a literal for each of my text snippets (blah blah blah). I have other literals that are updated based on code execution, so that is the reason all literals start with ABCD. In Page_Load, I do something like:
string pageName = "UniqueName"; languageCode = 1; // Maps to English in database // Global to page TranslateControls(pageName, this.Controls);
I then define the TranslateControls method as:private void TranslateControls(string pageName, ControlCollection controls) { for (int i=0; i < controls.Count; i++) { Control ctrl = controls[i]; if (ctrl.HasControls()) { // logger.Trace("Control: {0} has {1} child controls.", ctrl.UniqueID, ctrl.Controls.Count); TranslateControls(pageName, ctrl.Controls); } else { if (ctrl.GetType() == typeof(System.Web.UI.WebControls.Literal)) { // check name if (ctrl.UniqueID.StartsWith("ABCD")) { // Replace with database lookup Literal litTemp = (Literal)ctrl; Database db = new Database(); string textTranslation = db.GetText(pageName, ctrl.UniqueID, languageCode); litTemp.Text = textTranslation; } } } } }
Problem is, some controls are ignored -- when logging/stepping through, it appears that they are not part of the ControlCollection. Any thoughts/ideas? -- modification When page is initially loaded, all controls are displayed properly. However, on postback, some controls are missed and appear blank (odd, as they have initial text, and the db.GetText method returns either valid text or ctrl.UniqueID surrounded by underscores). -- end modification Thanks, Glenn -- modified at 16:07 Wednesday 25th April, 2007 -
I've created a literal for each of my text snippets (blah blah blah). I have other literals that are updated based on code execution, so that is the reason all literals start with ABCD. In Page_Load, I do something like:
string pageName = "UniqueName"; languageCode = 1; // Maps to English in database // Global to page TranslateControls(pageName, this.Controls);
I then define the TranslateControls method as:private void TranslateControls(string pageName, ControlCollection controls) { for (int i=0; i < controls.Count; i++) { Control ctrl = controls[i]; if (ctrl.HasControls()) { // logger.Trace("Control: {0} has {1} child controls.", ctrl.UniqueID, ctrl.Controls.Count); TranslateControls(pageName, ctrl.Controls); } else { if (ctrl.GetType() == typeof(System.Web.UI.WebControls.Literal)) { // check name if (ctrl.UniqueID.StartsWith("ABCD")) { // Replace with database lookup Literal litTemp = (Literal)ctrl; Database db = new Database(); string textTranslation = db.GetText(pageName, ctrl.UniqueID, languageCode); litTemp.Text = textTranslation; } } } } }
Problem is, some controls are ignored -- when logging/stepping through, it appears that they are not part of the ControlCollection. Any thoughts/ideas? -- modification When page is initially loaded, all controls are displayed properly. However, on postback, some controls are missed and appear blank (odd, as they have initial text, and the db.GetText method returns either valid text or ctrl.UniqueID surrounded by underscores). -- end modification Thanks, Glenn -- modified at 16:07 Wednesday 25th April, 2007Delete else please.This is a logical error.
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
-
Delete else please.This is a logical error.
:^):^):^):^):^):^):^):^):^):^):^):^) :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::^):^):^):^)▒▒〓▒〓▒▒ :^):rose::^):^):^):^)▒〓〓〓〓〓▒ :^):rose::rose::rose::rose::rose:▒▒〓▒〓▒▒ :^):^):^):^):^):^):^):^):^):^):^):^)
Even with the else removed, the literals do not appear.
-
I've created a literal for each of my text snippets (blah blah blah). I have other literals that are updated based on code execution, so that is the reason all literals start with ABCD. In Page_Load, I do something like:
string pageName = "UniqueName"; languageCode = 1; // Maps to English in database // Global to page TranslateControls(pageName, this.Controls);
I then define the TranslateControls method as:private void TranslateControls(string pageName, ControlCollection controls) { for (int i=0; i < controls.Count; i++) { Control ctrl = controls[i]; if (ctrl.HasControls()) { // logger.Trace("Control: {0} has {1} child controls.", ctrl.UniqueID, ctrl.Controls.Count); TranslateControls(pageName, ctrl.Controls); } else { if (ctrl.GetType() == typeof(System.Web.UI.WebControls.Literal)) { // check name if (ctrl.UniqueID.StartsWith("ABCD")) { // Replace with database lookup Literal litTemp = (Literal)ctrl; Database db = new Database(); string textTranslation = db.GetText(pageName, ctrl.UniqueID, languageCode); litTemp.Text = textTranslation; } } } } }
Problem is, some controls are ignored -- when logging/stepping through, it appears that they are not part of the ControlCollection. Any thoughts/ideas? -- modification When page is initially loaded, all controls are displayed properly. However, on postback, some controls are missed and appear blank (odd, as they have initial text, and the db.GetText method returns either valid text or ctrl.UniqueID surrounded by underscores). -- end modification Thanks, Glenn -- modified at 16:07 Wednesday 25th April, 2007Glenn E. Lanier II wrote:
When page is initially loaded, all controls are displayed properly. However, on postback, some controls are missed and appear blank (odd, as they have initial text, and the db.GetText method returns either valid text or ctrl.UniqueID surrounded by underscores).
Knowing this bit had something to do with it, after much head-banging/debugging, I realized that my literal was not listed in the .cs file. My non-required fields displayed, as they are defined:
<label for="txtBillFirstName"><asp:label id="lblBillFirstName" runat="server"><asp:literal runat="server" id="litTextBillNameFirst">First Name:</asp:literal></asp:label></label>
but the [initially] required fields were not, defined as:<label for="txtBillFirstName"><asp:label id="lblBillFirstName" runat="server">* <asp:literal runat="server" id="litTextBillNameFirst">First Name:</asp:literal></asp:label></label>
Apparently, you can have an asp:literal in an asp:label if it is the only thing in the label, but mixing text with a literal doesn't appear to work. The initial text displayed on the page load, but not on a postback - not sure why even the * didn't show up. Some of the fields are only required based on selections made on the form, so if I cause the error conditions, those labels displayed as required, which threw me off track a little. --G -- modified at 12:41 Thursday 26th April, 2007