Library code snippets
Adding controls to PlaceHolders dynamically
A very powerful and flexible way to program sites in ASP.NET is to one .aspx which contains a PlaceHolder control for each area of your screen. You then dynamically fill these controls during each hit of your page. This code will get you started. Notice the "FindControl" method with which you identify each control. If you want to use JavaScript on your controls, I think you have to use ClientId to access them, check it out.
<%@ Page Language="C#" %>
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
//build placeholder
for(int x = 0; x <= 10; x++) {
Label title = new Label();
title.Text = "Item " + x.ToString();
title.ID = "Item" + x.ToString();
Area1.Controls.Add(title);
Area1.Controls.Add(new LiteralControl("<br>"));
}
}
private void ButtonChange_Click(object sender, System.EventArgs e)
{
((Label)(Area1.FindControl("Item" + txtId.Text.ToString()))).Text = txtName.Text.ToString();
}
</script>
<html>
<head>
</head>
<body>
<form id="Form1" method="post" runat="server">
<P>
<asp:PlaceHolder id="Area1" runat="server"></asp:PlaceHolder></P>
<P> </P>
<P>ID:
<asp:TextBox id="txtId" runat="server" Width="51px"></asp:TextBox></P>
<P>New Name:
<asp:TextBox id="txtName" runat="server"></asp:TextBox></P>
<P>
<asp:Button id="ButtonChange" onclick="ButtonChange_Click" runat="server" Text="Change"></asp:Button></P>
<P> </P>
</form>
</body>
</html>
Related articles
Related discussion
-
Profile Class does not work after Translation
by converter2009 (1 replies)
-
what is the SQL Server Provider
by hayperaktib (1 replies)
-
Very Urgent regarding deleting the images from a folder
by Nanosteps (6 replies)
-
Java Script, File uploading on ftp server using java script code
by h_c_a_andersen (2 replies)
-
sharepoint calendar web part with events from sql table
by converter2009 (2 replies)
Related podcasts
-
StackOverflow uses ASP.NET MVC - Jeff Atwood and his technical team
Scott chats with Jeff Atwood of CodingHorror.com and most recently, StackOverflow.com. Jeff and Joel Spolsky and their technical team have created a new class of application using ASP.NET MVC. What works, what doesn't, and how did it all go down?
Events coming up
-
Mar
15
DevWeek 2010
London, United Kingdom
DevWeek is Europe’s leading independent conference for software developers, database professionals and IT architects, and features expert speakers on a wide range of topics, including .NET 4.0, Silverlight 3, WCF 4, Visual Studio 2010, REST, Windows Workflow 4, Thread Synchronization, ASP.NET 4.0, SQL Server 2008 R2, LINQ, Unit Testing, CLR & C# 4.0, .NET Patterns, WPF 4, F#, Windows Azure, ADO.NET, Entity Framework, Debugging, T-SQL Tips & Tricks, and more.
Hi have your tried loading usercontrols dynamically?
Does the state persist during postbacks? I have noticed that user controls were not loaded during postbacks. We need to load them in every post back
This thread is for discussions of Adding controls to PlaceHolders dynamically.