Login    
 
 
 
 
Text/HTML
  
You are here :: Blogs Saturday, May 19, 2012

Search
Note: This uses the internal blog search engine. The Google search engine is also available at the top of the page.
  
Disclaimer

Please review the site disclaimer before downloading or using content found on this site

  
Categories
  
DEVSHED Blog
As always, I welcome your comments!
Jul 6

Written by: Steve Gray
7/6/2011 1:58 PM  RssIcon

I’ve placed some controls onto a page dynamically, and was having trouble accessing them in the code behind. There were several complications – they were inside a place holder, inside a master page. Not my favorite technique, but business needs dictated it.

After a while I stumbled across the code to make it work, and I wanted to record it.

HTML:

  1. <%@ Page Title="" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="WebForm2.aspx.vb" Inherits="WebApplication1.WebForm2" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  5. <div>
  6.         <asp:PlaceHolder ID="Placeholder1" runat="server"></asp:PlaceHolder><br />
  7.         <asp:Button ID="btnSubmit" runat="server" Text="Submit" />
  8. </div>
  9. </asp:Content>

and the code behind:

  1. Public Class WebForm2
  2.     Inherits System.Web.UI.Page
  3.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  4.  
  5.         'add a textbox directly to the Placeholder
  6.         Dim tb As New TextBox
  7.         tb.ID = "txtTemp"
  8.         Me.Placeholder1.Controls.Add(tb)
  9.  
  10.  
  11.         'add a text box that is nested inside a table
  12.         Dim t As New HtmlTable
  13.         Dim r As New HtmlTableRow
  14.         Dim c As New HtmlTableCell
  15.  
  16.         tb = New TextBox
  17.         tb.ID = "txtTemp2"
  18.         c.Controls.Add(tb)
  19.         r.Controls.Add(c)
  20.         t.Controls.Add(r)
  21.         Placeholder1.Controls.Add(t)
  22.  
  23.  
  24.     End Sub
  25.  
  26.     Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
  27.         'this works for the first textbox
  28.         Dim tb As New TextBox
  29.         tb = Me.FindControl("ctl00$ContentPlaceHolder1$txtTemp")
  30.  
  31.         'and this works for the textbox inside the table. Note the ',0'
  32.         Dim tb2 As New TextBox
  33.         tb2 = Me.FindControl("ctl00$ContentPlaceHolder1$txtTemp2", 0)
  34.  
  35.     End Sub
  36. End Class

Tags:
Categories:
As always, I welcome your comments!
  
 
 
Home | Products | Blogs | Contact Us | Links | God's Plan
Privacy Statement | Terms Of Use
 
Copyright 2011 by Devshed.us