Browse DevX
Sign up for e-mail newsletters from DevX


Dynamically Adding Controls : Page 5

Adding controls at runtime provides the flexibility to design user interfaces that can be different things to different users. By the end of this article you'll be using multiple Placeholder controls to build a fexible template page.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Adding Body User Controls
When the page loads for the first time, the Body query string variable will be blank and will result in neither of the body user controls being added (see Figure 11). In this situation you could load a "home" user control to display the initial content. I'll leave that as an exercise for you.

So, how does the Body query string variable get set? The links in the Navigation user control hold the answer to this question.

<asp:LinkButton id="LinkButton1" runat="server" CommandArgument="1">Body #1</asp:LinkButton> <asp:LinkButton id="Linkbutton2" runat="server" CommandArgument="2">Body #2</asp:LinkButton> <asp:LinkButton id="Linkbutton2" runat="server" CommandArgument="2">Body #2</asp:LinkButton>

Setting the CommandArgument for each LinkButton to 1, 2, or 3 determines which Body user control to display. In addition, each LinkButton has its Click event handled by the FilterLetter_Click method.

Private Sub FilterLetter_Click(_ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles LinkButton1.Click, _ Linkbutton2.Click, _ Linkbutton3.Click If sender.CommandArgument = 1 Then Response.Redirect("Formtemplate.aspx?Body=1") ElseIf sender.CommandArgument = 2 Then Response.Redirect("Formtemplate.aspx?Body=2") ElseIf sender.CommandArgument = 3 Then Response.Redirect("Formtemplate.aspx?Body=3") End If End Sub

In this code the FormTemplate page is posted to and the Body query string gets passed into it.

Contained in the Page_Load is where the Body query string is captured and subsequently passed into the BodySetup method.

Dim strBodyCode As String = _ Request.QueryString("Body") Me.HeaderSetup() Me.FooterSetup() Me.NavigationSetup() Me.BodySetup(strBodyCode)

Let's walk through a typical user action. Clicking on the Body #3 LinkButton on the Navigation bar causes a "3" to be posted back to the FormTemplate page. This in turn causes a "3" to be passed into the BodySetup method causing the Body3.ascx user control to be loaded into the plhBody placeholder. The resulting page is in Figure 12.

Figure 11: The FormTemplate Page. This page displays when a Body query string variable is not passed in.
Figure 12: The FormTemplate Page. This page displays when the Body query string variable is set to 3.
Obviously the template you created here is just one of an infinite number of templates you can design and build yourself for your ASP.NET Web sites.

Well that wraps up this article. I'm always interested to hear your feedback about the material covered here. As you can see, working with the Placeholder control and adding controls at runtime provides you with the flexibility to design generic templates and the power to leverage them over and over again.

Jim Duffy is founder and president of TakeNote Technologies, an award winning training and software development company. He has a BS degree in Computer and Information Systems and over 20 years of programming and training experience. He is an energetic trainer, skilled developer, and has been published in leading developer-oriented publications.Jim, a Microsoft MVP, is a popular speaker at regional user groups and developer conferences.Jim is also the author of TakeNote's popular VFP database auditing product, FoxAudit.Additional information about Jim and TakeNote Technologies can be found at www.takenote.com. Reach him here.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date