Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamics controls (Postback problem)

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello.

I've got this form which reads a list of messages from a database. Once read I create UserControl for each message containing a textbox and a save button.
On page load, I create X UserControls, generate a new ID for each saving it in ViewState and then add in a PlaceHolder. Great! Everything works up to here, controls are created and values are unique. Problem is here:
On PostBack, I re-create each UC in the PlaceHolder, and the OnClick event of the UC is executed, but the PlaceHolder is EMPTY!
WHY ???

Here some of the form code:
Code:
private void Page_Load(object sender, System.EventArgs e) 
{ 
   // Postback ? 
   if (!IsPostBack) 

   { 
      // Create message boxes
      ArrayList Component_Name = new ArrayList( ColApp.Count ); 
      this.m_CommentsList.Controls.Clear(); 

      int i = 0; 
      foreach( CMessage p in m_MessageList ) 
      { 
         // Create UserControl
         UCMessage newBox = (PCPComments)this.LoadControl( "WUC/UCMessage.ascx" ); 
         newBox.EnableViewState = true; 

         // Message value
         newBox.Message = p; 

         // Box ID
         newBox.ID = "Message_Box_" + (++i); 

         // Save ID of UC 
         Component_Name.Add( newBox.ClientID ); 

         // Add UC to PlaceHolder 
         m_PlaceHolderMessage.Controls.Add( newBox ); 
      } 

      // Save UC names collection
      ViewState.Add( "COMPONENT_NAME_COMMENTS", (ArrayList)Component_Name ); 
   } 
   else 
   { 
      // Re-create and re-assign values using Viewstate bag 
      ArrayList Component_Name = (ArrayList)ViewState["COMPONENT_NAME_COMMENTS"]; 
      if (Component_Name != null && Component_Name.Count > 0) 
      { 
         for (int i = 0; i < Component_Name.Count; ++i) 
         { 
            UCMessage p = (UCMessage)this.LoadControl( "WUC/UCMessage.ascx" ); 
            p.EnableViewState = true; 

            // Add control to the placeholder 
            m_PlaceHolderMessage.Controls.Add( p ); 

            // ID of UC 
            p.ID = (string)Component_Name[i]; 

            // Value of message 
            p.Message = m_MessageList[i]; 
         } 
      } 
   } 
}

In the WUC, something very simple :

Code:
private void m_Bt_Save_Click(object sender, System.EventArgs e) 
{ 
    // Update from text box
} 

#region Web Form Designer generated code 
override protected void OnInit(EventArgs e) 
{ 
   InitializeComponent(); 
   base.OnInit(e); 
} 

private void InitializeComponent() 
{ 
   this.Load += new System.EventHandler(this.Page_Load); 
   this.m_bt_Save.Click += new System.EventHandler( this.m_Bt_Save_Click ); 
} 
#endregion

Thanks for your help.
 
If you want the UC events to fire properly, you need to load those controls in the Init event of your page, because they must be loaded before LoadViewState comes, which is prior to Page_Load.

A better explanation:
Even more information:

However, I would suggest (for this particular application), loading a datagrid with those controls, and just handling everything on the single page. Getting the UC solution to work is just too much hassle, and you will run into other problems along your way.

The datagrid solution would wind up being much more elegant and simpler in the end. You would just wire up the OnItemCommand event for the datagrid for each command button on each row... passing in relavent ID's as your CommandArgument.

Hope this helps.

-paul

penny.gif
penny.gif

The answer to getting answered -- faq855-2992
 
Hi Feltar

I think it may be because you are adding the user control to the placeholder before setting the ID and Message properties.

I'm kind of guessing here but when you reference p your not referencing the control in the placeholders control collection. To access that you would need to use m_PlaceHolderMessage.Controls[index]. Hence the control you add is empty and doesnt appear?

Try switching the code around so your adding the control after assigning ID and Message and see if that works.

Rob

Every gun that is made, every warship launched, every rocket fired, signifies in the final sense a theft from those who hunger and are not fed, those who are cold and are not clothed - Eisenhower 1953
 
Hello.

link 9 :
I can't use DataGrid, because I have multiple style of message box. Sometime it is an Approver box, other time it is a box with multiple choice, etc... So I need to use UC. I tried to load it in the Page_Init but I that doesn't work anymore

crazyboybert : I try it, but same things, the dynamics UC dissapear after a click the submit button.

Thanks for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top