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:
In the WUC, something very simple :
Thanks for your help.
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.