Control not rendering if added during aspx page load call

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • crabpot8
    New Member
    • Aug 2007
    • 40

    Control not rendering if added during aspx page load call

    I am working on a message system that will let out programmers easily output a message to any user. It works fine, unless the message is requested inside of a page_load on an aspx page. Then, it does not render at all.



    The code does step through, and the control is added - I believe it might be being added to late in the page lifecycle to get rendered. I can't seem to find any way to tell .NET to render that control, however.



    Here is my code for my master page, where most of the "meat" is located


    Code:
     
    1 using System;
    2 using System.Collections;
    3 using System.Configuration;
    4 using System.ComponentModel;
    5 using System.Data;
    6 using System.Data.Odbc;
    7 using System.Diagnostics;
    8 using System.ServiceProcess;
    9 using System.Drawing;
    10 using System.Web;
    11 using System.Web.SessionState;
    12 using System.Web.UI;
    13 using System.Web.UI.WebControls;
    14 using System.Web.UI.HtmlControls;
    15 using System.Text.RegularExpressions;
    16 
    17 namespace CUTS
    18 {
    19 public partial class BMW_Master : System.Web.UI.MasterPage
    20 {
    21 private void Page_Load(object sender, System.EventArgs e)
    22 {
    23 if (IsPostBack)
    24 {
    25 reCreateMessages();
    26 return;
    27 }
    28 
    29 // If we are on a new page, clear any old
    30 // messages
    31 ClearAllMessages();
    32 }
    33 private void reCreateMessages()
    34 {
    35 if (ViewState["messages"] == null ||
    36 ViewState["messages"].ToString() == "" ||
    37 ViewState["messages"].ToString() == String.Empty)
    38 return;
    39 
    40 // Create the regex
    41 string messages = (string)ViewState["messages"];
    42 Regex reg = new Regex(@"~(?<message>.+?)\*(?<severity>.+?);");
    43 Match m = reg.Match(messages);
    44 
    45 // extract the variables
    46 while (m.Success)
    47 {
    48 string message = m.Groups["message"].ToString(),
    49 severity = m.Groups["severity"].ToString();
    50 
    51 MessageSeverity _severity = MessageSeverity.Error;
    52 
    53 if (severity == "Information")
    54 _severity = MessageSeverity.Information;
    55 else if (severity == "Error")
    56 _severity = MessageSeverity.Error;
    57 else if (severity == "Success")
    58 _severity = MessageSeverity.Success;
    59 
    60 new_message_.Controls.Add(new BMW_Message(message, _severity));
    61 
    62 m = m.NextMatch();
    63 }
    64 
    65 }
    66 
    67 public void ClearAllMessages()
    68 {
    69 new_message_.Controls.Clear();
    70 }
    71 
    72 public void AddNewMessage(string _message)
    73 {
    74 AddNewMessage (_message,MessageSeverity.Error);
    75 }
    76 
    77 public void AddNewMessage(string _message, MessageSeverity _severity)
    78 {
    79 // Remove used special characters
    80 _message = _message.Replace("~", "~");
    81 _message = _message.Replace("*", "*");
    82 _message = _message.Replace(";", ";");
    83 
    84 BMW_Message m = new BMW_Message(_message, _severity);
    85 new_message_.Controls.Add(m);
    86 
    87 ViewState["messages"] += "~" + _message + "*" + _severity.ToString() + ";";
    88 Page_Load(this, new EventArgs());
    89 }
    90 
    91 public void Clear_All_Click(object sender, EventArgs e)
    92 {
    93 ViewState["messages"] = null;
    94 new_message_.Controls.Clear();
    95 }
    96 
    97 public void Clear_Me_Click(object sender, EventArgs e)
    98 {
    99 Button b = (Button)sender;
    100 BMW_Message message = (BMW_Message)b.Parent;
    101 string vs_string = message.ViewStateString;
    102 
    103 string vs = ViewState["messages"].ToString();
    104 vs = vs.Replace(vs_string, "");
    105 
    106 ViewState["messages"] = vs;
    107 if (vs == "")
    108 ViewState["messages"] = null;
    109 
    110 new_message_.Controls.Remove(message);
    111 }
    112 }
    113 }
    114 public enum MessageSeverity
    115 {
    116 Error, Information, Success
    117 }
    118 
    119 public class BMW_Message : CompositeControl
    120 {
    121 private string _message;
    122 private MessageSeverity _severity;
    123 
    124 public BMW_Message(string message, MessageSeverity severity)
    125 {
    126 this._message = message;
    127 this._severity = severity;
    128 }
    129 
    130 public string ViewStateString
    131 {
    132 get
    133 {
    134 return "~" +
    135 _message + "*" +
    136 _severity.ToString() + ";";
    137 }
    138 }
    139 
    140 protected override void CreateChildControls()
    141 {
    142 Controls.Clear();
    143 CreateControlHierarchy();
    144 ClearChildViewState();
    145 }
    146 protected virtual void CreateControlHierarchy()
    147 {
    148 string div_style = String.Empty;
    149 Color button_BackColors = Color.Transparent;
    150 
    151 if (_severity == MessageSeverity.Information)
    152 {
    153 div_style = "border: solid 3px gold; background-color: yellow; padding-left: 10px;";
    154 button_BackColors = Color.Yellow;
    155 }
    156 else if (_severity == MessageSeverity.Success)
    157 {
    158 div_style = "border: solid 3px dark green; background-color: green; padding-left: 10px;";
    159 button_BackColors = Color.Green;
    160 }
    161 else // Default to Error
    162 {
    163 div_style = "border: solid 3px dark red; background-color: red; padding-left: 10px;";
    164 button_BackColors = Color.Red;
    165 }
    166 
    167 // Panel Containing this, Then Form, Then to the BMW_Master
    168 CUTS.BMW_Master m = (CUTS.BMW_Master)Parent.Parent.Parent;
    169 
    170 LiteralControl div_start = new LiteralControl("&lt;div style='" + div_style + "'>");
    171 
    172 Button Clear_Me = new Button();
    173 Clear_Me.Text = " X ";
    174 Clear_Me.BackColor = button_BackColors;
    175 Clear_Me.Style.Add("float", "left");
    176 Clear_Me.Style.Add("position", "relative");
    177 Clear_Me.Style.Add("left", "-19px");
    178 Clear_Me.Style.Add("top", "-10px");
    179 Clear_Me.Click += new EventHandler(m.Clear_Me_Click);
    180 
    181 Button Clear_All = new Button();
    182 Clear_All.Text = "Clear All";
    183 Clear_All.BackColor = button_BackColors;
    184 Clear_All.Style.Add("position", "relative");
    185 Clear_All.Style.Add("left", "-19px");
    186 Clear_All.Style.Add("top", "-10px");
    187 Clear_All.Click += new EventHandler(m.Clear_All_Click);
    188 
    189 
    190 
    191 Label Message = new Label();
    192 Message.Text = _message;
    193 
    194 LiteralControl div_end = new LiteralControl("&lt;/div>");
    195 
    196 this.Controls.Add(div_start);
    197 this.Controls.Add(Clear_Me);
    198 this.Controls.Add(Clear_All);
    199 this.Controls.Add(Message);
    200 this.Controls.Add(div_end);
    201 }
    202 }
    203
    I would really appreciate ANY help here - this all works absolutely if the ASPX page loads, and then someone causes a postback that creates a message. However, if the message is added in the ASPX page_load call, then the item simply does not render.



    Thanks in advance,

    crab
    Last edited by DrBunchman; Jul 23 '08, 07:20 AM. Reason: Added [Code] Tags - Please use the '#' button
  • DrBunchman
    Recognized Expert Contributor
    • Jan 2008
    • 979

    #2
    Hi crab,

    Line 23 above
    Code:
     if (IsPostBack)
    is testing that the page load is a PostBack before running the reCreateMessage s() sub. Remove this test and it should run when your page loads first time.

    Hope this helps,

    Dr B

    PS Please remember to use [CODE] tags in your post by using the # button at the top of the edit window - it makes your posts much easier for everyone to read. Thanks.

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I noticed that in your Page_Load line 26, you have a return.
      Try removing this....

      Comment

      Working...