jui2ce
asked on
URGEN:Add a placeholder dynamically.
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(txtDia l)
tcINet.Controls.Add(txtBro adband)
tr.Cells.Add(tcINet)
Table1.Rows.Add(tr)
I would like to add a placeholder after the INet control to later add text there.
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(txtDia
tcINet.Controls.Add(txtBro
tr.Cells.Add(tcINet)
Table1.Rows.Add(tr)
I would like to add a placeholder after the INet control to later add text there.
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.
ASKER
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(PLtx tname)
Etr.Cells.Add(EtcLname)
Table1.Rows.Add(Etr)
Dim EtcLname As TableCell = New TableCell
Dim PLtxtname As PlaceHolder = New PlaceHolder
PLtxtname.ID = "PlaceHolder3"
EtcLname.Controls.Add(PLtx
Etr.Cells.Add(EtcLname)
Table1.Rows.Add(Etr)
What do you do to try to "find the control PlaceHolder3"?
Something like
Dim crtl As Control = Me.FindControl("PlaceHolde r3")
Dim place As PlaceHolder = CType(ctrl, System.Web.UI.WebControls. PlaceHolde r)
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.
Something like
Dim crtl As Control = Me.FindControl("PlaceHolde
Dim place As PlaceHolder = CType(ctrl, System.Web.UI.WebControls.
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.
ASKER
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.
<asp:PlaceHolder id="PlaceHolder3" runat="server"/>
That is the problem I need to add that dynamically.
Please post more code might help us to identify your problem.
ASKER
<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(PLtxt name)
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( ePlaceHold er),PlaceH older)
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>
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(PLtxt
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"
txtName = CType(Me.Page.FindControl(
PLtxtname = CType(Me.Page.FindControl(
if not txtName is Nothing then
ePlaceHolder.Controls.Add(
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>
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.MissingMemberExcept
Source Error:
Line 68: If Not txtName Is Nothing Then
Line 69:
Line 70: ePlaceHolder.Controls.Add(
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(
PLtxtname = CType(Me.Page.FindControl(
If Not txtName Is Nothing Then
PLtxtname.Controls.Add(New
End If
Next
=-=-=-=-=-=-=-=-=-=-=-=-=-
Now it seems the code is working without any problem. Can you try that?
> 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.
> 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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
.....
.....
Dim ctl As New PlaceHolder()
ctl.Controls.Add( txtDial )
tcINet.Controls.Add(ctl)
.....
.....