unable to generate dynamic code for Updatepanel in user ctrl

Jul 5 2014 3:32 AM
I am trying to generate update panel dynamically on user control so that i can use that user control on other pages. But the update panel is not working properly on user control. I searched on google but didnt get any desired output. My Script manager is on Master Page.
Exception is :

1. The control collection cannot be modified during DataBind, Init, Load, PreRender or Unload phases.

m.Invoke(ScriptManager.GetCurrent(Page), new object[] { panel});
the above syntax is returning the null value.


here is my code....

--Code start here

static int iValue = 0;
static Panel[] pnlAttactCondition = new Panel[1];

private void Add_Panel(Panel pnlTemp)
{
try
{
foreach (Control item in pnlTemp.Controls)
{
if (item is CheckBox)
{
(item as CheckBox).CheckedChanged += new EventHandler(cboxAge_CheckedChanged);
}
if (item is RadioButtonList)
{
(item as RadioButtonList).SelectedIndexChanged += new EventHandler(rblInformationType_SelectedIndexChanged);
}
if (item is Button)
{
(item as Button).Click += new EventHandler(btnCancel_Click);
}
if (item is UpdatePanel)
{
(item as UpdatePanel).Unload += new EventHandler(UpdatePanel_Unload);
}
}
pnlMainConditions.Controls.Add(pnlTemp);
pnlMainConditions.Controls.Add(new LiteralControl("<br>"));
}
catch (Exception)
{
throw;
}
}

private Panel GenerateControls(string strtemp)
{
Panel pnlCondition = null;
RadioButtonList rblInformationType =null;
TextBox txtPercentage=null;
TextBox txtFixedAmount=null;
TextBox txtMinimumAmount=null;
TextBox txtMaximumAmount=null;
TextBox txtFormula = null;
TextBox txtStartAge = null;
TextBox txtEndAge = null;
CheckBox cboxAge = null;
Label lblAmountType = null;
Label lblMinimumAount = null;
Label lblMaximumAmount = null;
Label lblStartAge = null;
Label lblEndAge = null;
Button btnCancel = null;
UpdatePanel uppnlTemp = null;
try
{
iValue++;
pnlCondition = new Panel();
rblInformationType = new RadioButtonList();
txtPercentage = new TextBox();
txtFixedAmount = new TextBox();
txtFormula = new TextBox();
txtMinimumAmount = new TextBox();
txtMaximumAmount = new TextBox();
txtStartAge = new TextBox();
txtEndAge = new TextBox();
cboxAge = new CheckBox();
lblAmountType = new Label();
lblMinimumAount = new Label();
lblMaximumAmount = new Label();
lblStartAge = new Label();
lblEndAge = new Label();
btnCancel = new Button();
uppnlTemp = new UpdatePanel();

rblInformationType.RepeatDirection = RepeatDirection.Horizontal;
rblInformationType.Items.Add("Percentage");
rblInformationType.Items.Add("Fixed Amount");
rblInformationType.Items.Add("Formula");
rblInformationType.ClientIDMode = ClientIDMode.Static;
rblInformationType.ID = "rblInformationType_" + Convert.ToString(iValue);
rblInformationType.SelectedIndexChanged += new EventHandler(rblInformationType_SelectedIndexChanged);
rblInformationType.AutoPostBack = true;

txtPercentage.ClientIDMode = ClientIDMode.Static;
txtPercentage.ID = "txtPercentage_" + Convert.ToString(iValue);
txtPercentage.Width = 240;
txtPercentage.CssClass = "inputs";

txtFixedAmount.ClientIDMode = ClientIDMode.Static;
txtFixedAmount.ID = "txtFixedAmount_" + Convert.ToString(iValue);
txtFixedAmount.Width = 240;
txtFixedAmount.CssClass = "inputs";

txtFormula.ClientIDMode = ClientIDMode.Static;
txtFormula.ID = "txtFormula_" + Convert.ToString(iValue);
txtFormula.Width = 240;
txtFormula.CssClass = "inputs";

txtMinimumAmount.ClientIDMode = ClientIDMode.Static;
txtMinimumAmount.ID = "txtMinimumAmount_" + Convert.ToString(iValue);
txtMinimumAmount.Width = 240;
txtMinimumAmount.CssClass = "inputs";

txtMaximumAmount.ClientIDMode = ClientIDMode.Static;
txtMaximumAmount.ID = "txtMaximumAmount_" + Convert.ToString(iValue);
txtMaximumAmount.Width = 240;
txtMaximumAmount.CssClass = "inputs";

txtStartAge.ClientIDMode = ClientIDMode.Static;
txtStartAge.ID = "txtStartAge_" + Convert.ToString(iValue);
txtStartAge.Width = 240;
txtStartAge.CssClass = "inputs";

txtEndAge.ClientIDMode = ClientIDMode.Static;
txtEndAge.ID = "txtEndAge_" + Convert.ToString(iValue);
txtEndAge.Width = 240;
txtEndAge.CssClass = "inputs";

cboxAge.ClientIDMode = ClientIDMode.Static;
cboxAge.ID = "cboxAge_" + Convert.ToString(iValue);
cboxAge.Text = "Is Age Factor applicable for this condition";
cboxAge.CheckedChanged += new EventHandler(cboxAge_CheckedChanged);
cboxAge.AutoPostBack = true;

lblAmountType.Text = string.Empty;
lblAmountType.ID = "lblAmountType_" + Convert.ToString(iValue);
lblAmountType.ClientIDMode = ClientIDMode.Static;

lblMinimumAount.Text = "Minimum Amount";
lblMinimumAount.ID = "lblMinimumAmount_" + Convert.ToString(iValue);
lblMinimumAount.ClientIDMode = ClientIDMode.Static;

lblMaximumAmount.Text = "Maximum Amount";
lblMaximumAmount.ID = "lblMaximumAmount_" + Convert.ToString(iValue);
lblMaximumAmount.ClientIDMode = ClientIDMode.Static;

lblStartAge.Text = "Age From";
lblStartAge.ID = "lblStartAge_" + Convert.ToString(iValue);
lblStartAge.ClientIDMode = ClientIDMode.Static;

lblEndAge.Text = "Age Upto";
lblEndAge.ID = "lblEndAge_" + Convert.ToString(iValue);
lblEndAge.ClientIDMode = ClientIDMode.Static;

btnCancel.Text = "Cancel";
btnCancel.ClientIDMode = ClientIDMode.Static;
btnCancel.ID = "btnCancel_" + Convert.ToString(iValue);
btnCancel.CssClass = "bluebtn";
btnCancel.Click += new EventHandler(btnCancel_Click);

uppnlTemp.ID = "uppnlTemp_" + Convert.ToString(iValue);
uppnlTemp.ClientIDMode = ClientIDMode.Static;
uppnlTemp.UpdateMode = UpdatePanelUpdateMode.Conditional;
//uppnlTemp.Unload += new EventHandler(UpdatePanel_Unload);
uppnlTemp.ContentTemplateContainer.Controls.Add(rblInformationType);

pnlCondition.ClientIDMode = ClientIDMode.Static;
pnlCondition.ID = "pnlCondition_" + Convert.ToString(iValue);
pnlCondition.BorderColor = Color.Gray;
pnlCondition.BorderWidth = 5;
pnlCondition.ToolTip = strtemp;

pnlCondition.Controls.Add(new LiteralControl("<table><tr><td> Select Any One </td><td>"));
pnlCondition.Controls.Add(uppnlTemp);
pnlCondition.Controls.Add(new LiteralControl("</td></tr>"));
pnlCondition.Controls.Add(new LiteralControl("<br/>"));
pnlCondition.Controls.Add(new LiteralControl("<tr><td>"));
pnlCondition.Controls.Add( lblAmountType );
pnlCondition.Controls.Add(new LiteralControl("</td><td>"));
pnlCondition.Controls.Add(txtPercentage);
pnlCondition.Controls.Add(txtFixedAmount);
pnlCondition.Controls.Add(txtFormula);
pnlCondition.Controls.Add(new LiteralControl("</td></tr>"));
pnlCondition.Controls.Add(new LiteralControl("<tr><td>"));
pnlCondition.Controls.Add(lblMinimumAount);
pnlCondition.Controls.Add(new LiteralControl("</td><td>"));
pnlCondition.Controls.Add(txtMinimumAmount);
pnlCondition.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
pnlCondition.Controls.Add(lblMaximumAmount);
pnlCondition.Controls.Add(new LiteralControl("</td><td>"));
pnlCondition.Controls.Add(txtMaximumAmount);
pnlCondition.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
pnlCondition.Controls.Add(cboxAge);
pnlCondition.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
pnlCondition.Controls.Add(lblStartAge);
pnlCondition.Controls.Add(new LiteralControl("</td><td>"));
pnlCondition.Controls.Add(txtStartAge);
pnlCondition.Controls.Add(new LiteralControl("</td></tr><tr><td>"));
pnlCondition.Controls.Add(lblEndAge);
pnlCondition.Controls.Add(new LiteralControl("</td><td>"));
pnlCondition.Controls.Add(txtEndAge);
pnlCondition.Controls.Add(new LiteralControl("</td></tr><tr></tr><tr><td></td><td>"));
pnlCondition.Controls.Add(btnCancel);
pnlCondition.Controls.Add(new LiteralControl("</td></tr></table>"));

rblInformationType.SelectedIndex = 0;
txtFixedAmount.Visible = false;
txtFormula.Visible = false;

cboxAge.Checked = false;
lblStartAge.Visible = false;
txtStartAge.Visible = false;
lblEndAge.Visible = false;
txtEndAge.Visible = false;

lblAmountType.Text = "Set Percentage";

return pnlCondition;
}
catch (Exception ex)
{

throw ex;
}
}

public void RegisterUpdatePanel(UpdatePanel panel)
{

MethodInfo m =(from methods in typeof(ScriptManager).GetMethods(BindingFlags.NonPublic | BindingFlags.Instance)

where methods.Name.Equals("System.Web.UI.IScriptManagerInternal.RegisterUpdatePanel")

select methods).First<MethodInfo>();

m.Invoke(ScriptManager.GetCurrent(Page), new object[] { panel });

}

protected void Page_Load(object sender, EventArgs e)
{
try
{
if (!IsPostBack)
{
iValue = 0;
pnlAttactCondition = new Panel[1];
}
}
catch (Exception ex)
{
throw ex;
}
}

protected override void OnInit(EventArgs e)
{
if (pnlAttactCondition[0] is Panel)
{
foreach (Panel pTemp in pnlAttactCondition)
{
if (pTemp != null)
{
Add_Panel(pTemp);
}
}
}

//Page page = HttpContext.Current.Handler as Page;
//page.Init += delegate(object sender, EventArgs e_Init)
//{
// if (ScriptManager.GetCurrent(page) == null)
// {
// ScriptManager sMgr = new ScriptManager();
// page.Form.Controls.AddAt(0, sMgr);
// }
//};
//base.OnInit(e);
}

protected void UpdatePanel_Unload(object sender, EventArgs e)
{
try
{
RegisterUpdatePanel(sender as UpdatePanel);
}
catch (Exception ex)
{
throw ex;
}
}


protected void btnAddCondition_Click(object sender, EventArgs e)
{
int iIndex = iValue;
try
{
if (pnlAttactCondition.Length <= iValue)
{
Array.Resize(ref pnlAttactCondition, iValue + 1);
}
pnlAttactCondition[iIndex] = new Panel();
pnlAttactCondition[iIndex] = GenerateControls(iIndex.ToString());
Add_Panel(pnlAttactCondition[iIndex]);
}
catch (Exception ex)
{
throw ex;
}
}

---code end here