views:

410

answers:

2

I have a function that creates multiple controls. Mainly input fields and an imagebutton with dynamic data for an Amazon payment button.

How can I add these controls to a placeholder so they are rendered on the page?

I tried creating a placeholder inside the function, adding controls and returning the placeholder.

plhCart = returnedPlaceHolder()

The controls did not render however when do a plhCart.Controls.Count the value was correct.

This is the function creating the controls and adding them to a placeholder object:

    Protected Function GetPayNowWidgetForm(ByVal formHiddenInputs As SortedDictionary(Of [String], [String])) As PlaceHolder
    Dim payButton As New ImageButton
    Dim plhForm As New PlaceHolder
    Dim counter As Integer = 1

    payButton.ID = "imgPayButton"
    payButton.PostBackUrl = "https://authorize.payments-sandbox.amazon.com/pba/paypipeline"
    payButton.ImageUrl = "https://authorize.payments-sandbox.amazon.com/pba/images/GMPayNowWithAmazon.png"

    plhForm.Controls.Add(payButton)

    ' Add the Key/Value pairs from formHiddenInputs
    For Each kvp As KeyValuePair(Of [String], [String]) In formHiddenInputs
        Dim hiddenField As HtmlControl = New HtmlControls.HtmlInputHidden
        ' Dim hiddenField As New HiddenField
        hiddenField.Attributes.Add("name", kvp.Key)
        hiddenField.Attributes.Add("value", kvp.Value)

        plhForm.Controls.Add(hiddenField)


        counter += 1
    Next

    Return plhForm
End Function

I am now using Steve Temple's suggestion and adding this returned placeholder to the placeholder on my page using

plhCart.Controls.Add(GetPayNowWidgetForm(...))

This is working fine, and probably better than what I came up with last night. My original solution was to create the controls and returning them as a system.array. Then looping through the array to add each control to the placeholder.

A: 

ASP.NET Page lifecycle is tricky.

Darin Dimitrov
+1  A: 

It's hard to know exactly what the problem is without knowing a little more.

Add the controls in the Page_Init method like so:

plhCart.Controls.Add(returnedPlaceHolder());

And that should add them and render them in the page

Steve Temple
Thanks, for whatever reason I didn't think to add my returned PlaceHolder as a control to the placeholder on the page.
Gthompson83