Very nice article. Indeed of much help !
I would like to add a bit of my learning here.
I noticed that, the dynamically added control was not retaining the values on the Very First Postback. For example, if you are debugging, then when the page gets loaded for the first time, you may see that the Page' Load or Init event does not get executed,
on the server.
This happens, because, by default, the page is cached by the browser and the browser serves this Page's HTML directly from its cache. So to avoid that, just add the following ASP Code in your ASPX file:-
<%@ OutputCache Location="None" NoStore="true" %>
And you would get the correct Behaviour even on the first Page Load.
private void CreateDynamicAlphabetLinks()
{
// clear the placeholder first - in case something else was dynamically loaded
PlaceHolder1.Controls.Clear();
private void CreateDynamicNumberButtons()
{
// clear the placeholder first - in case something else was dynamically loaded
PlaceHolder1.Controls.Clear();
// dynamically create a series of linkbuttons
for (int number = 0; number < 25; number++)
{
LinkButton lnk = new LinkButton();
lnk.ID = "number_" + number;
lnk.Text = number.ToString();
lnk.CommandName = "NUMBER";
lnk.CommandArgument = number.ToString();
Recently i hav developed a Project with dynamic Controls.I can see you the demo of that project actually that project is of my Company but developed by me...It is totally dynamic..Please add me on gmail my gmail address is mangesh.gangwar@gmail.com.I can
send you the video of that project.I am using XML and sql server for databse to managing dynamic controls.There are also dynamic javascript validation for dynamic controls.
I am using mainly text box and dropdown list with javascript validation...but my controls are displaying in GridView.All the things in my project are dynamic and i am using xml.I am managing controls and validation throgh xml file.user can create template
according to his/her requirements.
vaibhavdp
Member
2 Points
1 Post
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Mar 12, 2010 05:54 AM|LINK
Very nice article. Indeed of much help !
I would like to add a bit of my learning here.
I noticed that, the dynamically added control was not retaining the values on the Very First Postback. For example, if you are debugging, then when the page gets loaded for the first time, you may see that the Page' Load or Init event does not get executed, on the server.
This happens, because, by default, the page is cached by the browser and the browser serves this Page's HTML directly from its cache. So to avoid that, just add the following ASP Code in your ASPX file:-
And you would get the correct Behaviour even on the first Page Load.
Thanks,
Vaibhav P.
MCP - ASP.NET
ASP.NET dynamically added controls
jofre
Member
2 Points
3 Posts
C# for FAQ: Why do dynamic controls disappear on postback and not raise events?
Mar 25, 2010 12:15 PM|LINK
namespace DynamicButton
{
public partial class _Default : System.Web.UI.Page
{
private const string ALPHABET_SELECTION = "ALPHABET";
private const string NUMBER_SELECTION = "NUMBERS";
private const string VIEWSTATEKEY_DYNCONTROL = "DynamicControlSelection";
[Bindable(true)]
[Category("Appearance")]
[DefaultValue("")]
[Localizable(true)]
public string DynamicControlSelection
{
get
{
String s = (String)ViewState["VIEWSTATEKEY_DYNCONTROL"];
return ((s == null) ? String.Empty : s);
}
set
{
ViewState["VIEWSTATEKEY_DYNCONTROL"] = value;
}
}
protected void Page_Load(object sender, EventArgs e)
{
switch (DynamicControlSelection)
{
case ALPHABET_SELECTION:
CreateDynamicAlphabetLinks();
break;
case NUMBER_SELECTION:
CreateDynamicNumberButtons();
break;
default:
break;
}
}
private void CreateDynamicAlphabetLinks()
{
// clear the placeholder first - in case something else was dynamically loaded
PlaceHolder1.Controls.Clear();
// dynamically create a series of linkbuttons
for (int keycode = 65; keycode < 90; keycode++)
{
LinkButton lnk = new LinkButton();
lnk.ID = "alpha_" + keycode;
lnk.Text = ((char)keycode).ToString();
lnk.CommandName = "ALPHABET";
lnk.CommandArgument = ((char) keycode).ToString();
lnk.Click += OnClick;
PlaceHolder1.Controls.Add(lnk);
PlaceHolder1.Controls.Add(new LiteralControl(" "));
}
DynamicControlSelection = ALPHABET_SELECTION;
}
private void CreateDynamicNumberButtons()
{
// clear the placeholder first - in case something else was dynamically loaded
PlaceHolder1.Controls.Clear();
// dynamically create a series of linkbuttons
for (int number = 0; number < 25; number++)
{
LinkButton lnk = new LinkButton();
lnk.ID = "number_" + number;
lnk.Text = number.ToString();
lnk.CommandName = "NUMBER";
lnk.CommandArgument = number.ToString();
lnk.Click += OnClick;
PlaceHolder1.Controls.Add(lnk);
PlaceHolder1.Controls.Add(new LiteralControl(" "));
}
DynamicControlSelection = NUMBER_SELECTION;
}
void OnClick(Object sender, EventArgs e)
{
IButtonControl btn = (IButtonControl) sender;
lblClickResult.Text = String.Format("You clicked - CommandName: {0} CommandArgument: {1}",
btn.CommandName, btn.CommandArgument);
}
protected void cmdAlphabet_Click(Object sender, EventArgs e)
{
CreateDynamicAlphabetLinks();
}
protected void cmdNumbers_Click(Object sender, EventArgs e)
{
CreateDynamicNumberButtons();
}
}
}
mangesh.sing...
Member
10 Points
15 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Jun 04, 2010 10:18 AM|LINK
Recently i hav developed a Project with dynamic Controls.I can see you the demo of that project actually that project is of my Company but developed by me...It is totally dynamic..Please add me on gmail my gmail address is mangesh.gangwar@gmail.com.I can send you the video of that project.I am using XML and sql server for databse to managing dynamic controls.There are also dynamic javascript validation for dynamic controls.
mangesh.sing...
Member
10 Points
15 Posts
Re: FAQ: Why do dynamic controls disappear on postback and not raise events?
Jun 04, 2010 07:01 PM|LINK
I am using mainly text box and dropdown list with javascript validation...but my controls are displaying in GridView.All the things in my project are dynamic and i am using xml.I am managing controls and validation throgh xml file.user can create template according to his/her requirements.