[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

URGEN:Add a placeholder dynamically.

Posted on 2004-08-14
10
Medium Priority
?
396 Views
Last Modified: 2006-11-17
How do I dyanmiccaly add a placeholder to a table. I am adding a text box like this

 For i = 0 To 25
            Dim tr As TableRow = New TableRow

            'make name cell with validator
            Dim tc1 As TableCell = New TableCell

            Dim txtName As TextBox = New TextBox
            txtName.ID = "txtName" + i.ToString()

            tc1.Controls.Add(txtName)
            tr.Cells.Add(tc1)

            Dim tcInet As TableCell = New TableCell
            Dim txtDial As RadioButton = New RadioButton
            Dim txtBroadband As RadioButton = New RadioButton

            txtDial.ID = "txtINet" + i.ToString()
            txtBroadband.ID = "txtINet" + i.ToString()

            tcINet.Controls.Add(txtDial)
            tcINet.Controls.Add(txtBroadband)

            tr.Cells.Add(tcINet)
     


            Table1.Rows.Add(tr)
 
I would like to add a placeholder after the INet control to later add text there.
0
Comment
Question by:jui2ce
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 20

Expert Comment

by:ihenry
ID: 11803094
Try this

.....
.....
Dim ctl As New PlaceHolder()
ctl.Controls.Add( txtDial )
tcINet.Controls.Add(ctl)
.....
.....
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11803101
Is there a reason you cannot add a PlaceHolder control in exactly the same way you're currently adding the TextBox?  You can then reference it by ID via the Page's FindControl method (http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwebuicontrolclassfindcontroltopic1.asp), in order to modify its contents, etc.
0
 

Author Comment

by:jui2ce
ID: 11803200
I have tried adding it like this. However it does not find the control PlaceHolder3

Dim EtcLname As TableCell = New TableCell
Dim PLtxtname As PlaceHolder = New PlaceHolder
                                    
PLtxtname.ID = "PlaceHolder3"
EtcLname.Controls.Add(PLtxtname)
Etr.Cells.Add(EtcLname)

   Table1.Rows.Add(Etr)
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 15

Expert Comment

by:Thogek
ID: 11803221
What do you do to try to "find the control PlaceHolder3"?

Something like
    Dim crtl As Control = Me.FindControl("PlaceHolder3")
    Dim place As PlaceHolder = CType(ctrl, System.Web.UI.WebControls.PlaceHolder)
    place.Controls.Add(new LiteralControl("Boo!"))
might work.

Or, if you're just looking to add text, you might wanna use a LiteralControl instead of a PlaceHolder, and just set the LiteralControl's Text property.
0
 

Author Comment

by:jui2ce
ID: 11803244
That would work if I manually add the placeholder

<asp:PlaceHolder id="PlaceHolder3"  runat="server"/>

That is the problem I need to add that dynamically.
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11803251

Please post more code might help us to identify your problem.
0
 

Author Comment

by:jui2ce
ID: 11803334
<Script Runat="Server">
       Private Sub Page_Load()
            Dim i As Integer

            For i = 1 To 25
                        Dim tr As TableRow = New TableRow
                        Dim tc1 As TableCell = New TableCell
                        Dim Etcname As TableCell = New TableCell
                        
                        
                        Dim PLtxtname As PlaceHolder = New PlaceHolder
                        Dim txtName As TextBox = New TextBox
                        
                        
                        txtName.ID = "txtName" + i.ToString()
                        PLtxtname.ID = "PlaceHolder"  + i.ToString()
                        
                        tc1.Controls.Add(txtName)
                        Etcname.Controls.Add(PLtxtname)
                        
                        tr.Cells.Add(tc1)
                        tr.Cells.Add(Etcname)
                        
                        Table1.Rows.Add(tr)
            Next      
            End Sub
            
      Sub Button_Click( s As Object, e As EventArgs )
       Dim i As Integer

                        For i = 1 To 25
                              dim txtName As TextBox
                              Dim PLtxtname As PlaceHolder
                              
                              Dim ctxtName
                              dim ePlaceHolder
                              
                              ctxtName="txtName" + i.ToString()
                              ePlaceHolder="PlaceHolder" + i.ToString()
                              
            txtName = CType(Me.Page.FindControl(ctxtName),TextBox)
            PLtxtname = CType(Me.Page.FindControl(ePlaceHolder),PlaceHolder)
                              
            if not txtName is Nothing then
                         
              ePlaceHolder.Controls.Add(new LiteralControl("test"))
                                        
                              end if          
                        Next
                  End Sub
                  
                                                
</Script>

<form Runat="Server">

<asp:Table id="Table1"
                       BackImageUrl="url"
                       Border="0"
                       CellSpacing="1"
                       CellPadding="1"
                       GridLines="Both"
                       HorizontalAlign="Center"
                       runat="server">
                  
                    
                  </asp:Table>
                        <asp:Button Text="Add" OnClick="Button_Click" Runat="Server" />
</form>            
0
 
LVL 20

Expert Comment

by:ihenry
ID: 11803455

I copied and pasted your code to my vs.net and got this exception,

=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Public member 'Controls' on type 'String' not found.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.MissingMemberException: Public member 'Controls' on type 'String' not found.

Source Error:

Line 68:             If Not txtName Is Nothing Then
Line 69:
Line 70:                 ePlaceHolder.Controls.Add(New LiteralControl("test"))
Line 71:
Line 72:             End If
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

And I modified the Button_Click event
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
        Dim i As Integer

        For i = 1 To 25
            Dim txtName As TextBox
            Dim PLtxtname As PlaceHolder

            Dim ctxtName As String
            Dim ePlaceHolder As String

            ctxtName = "txtName" + i.ToString()
            ePlaceHolder = "PlaceHolder" + i.ToString()

            txtName = CType(Me.Page.FindControl(ctxtName), TextBox)
            PLtxtname = CType(Me.Page.FindControl(ePlaceHolder), PlaceHolder)

            If Not txtName Is Nothing Then

                PLtxtname.Controls.Add(New LiteralControl("test"))

            End If
        Next
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Now it seems the code is working without any problem. Can you try that?
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11804857
> That would work if I manually add the placeholder
> That is the problem I need to add that dynamically.

It should work either way.

However, you're code example above appears to be using ePlaceHolder (a string) and PLtxtname (a PlaceHolder) interchangably, which won't work -- my example code adds the LiteralControl to a PlaceHolder's Controls collection, not to a string's, which has no such collection -- and explains the error ihenry is seeing.  ihenry's code example seems to fix that immediate problem.
0
 
LVL 10

Accepted Solution

by:
jnhorst earned 1000 total points
ID: 11827348
From your code, you seem to be looking for the placeholder in the Controls collection of the Page object, which will not work.  You added it to the Controls collection of the table cell.  You need to first resolve the correct table cell control and then use:

Dim ctl as PlaceHolder
ctl = CType(tableCell.FindControl("placholderID"), PlaceHolder)

John

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question