Link to home
Start Free TrialLog in
Avatar of johndenny
johndenny

asked on

Generate asp:button using routine

I am trying to generate the output of my query using sub routine which response.write(strHTML) after that.
Something like this:

Sub Generate_Output()
   Dim strHTML as String

    .........
   strHTML &= "<asp:button id=btnAdd text=add onClick=btnAdd_Click runat=server/>"
    .........

    and so on ...

   Response.Write(strHTML)
End Sub

Of course, I have Sub btnAdd_Click.

When this sub is called, it is displaying whatever result with the buttons but it does not work.
It did not invoke btnAdd_Click at all. But if I generate this asp:button not inside the sub, it works fine.
Anything wrong with it?


Avatar of Timbo87
Timbo87

Add a PlaceHolder object where you want to put the button.

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

And place this code in the page load event handler:

Dim myButton As New Button
myButton.Text = "Click me"
myButton.Id = "btnAdd"
' other properties...
placeHolder1.Controls.Add(myButton)
Avatar of johndenny

ASKER

I run it and get:

Name 'placeHolderButton' is not declared.

where I have put the placeholder like this:

Sub Generate_Output()
   Dim strHTML as String

    .........
   strHTML &= "<asp:PlaceHolder ID=PlaceHolderButton Runat=server/>"
    .........

    and so on ...

   Response.Write(strHTML)
End Sub

The thing is when initialized the button and relate it to the placeholder, the placeholder not even created yet.

Any idea?
What event triggers Generate_Output? Can you paste the code of the caller?
This won't work:

strHTML &= "<asp:PlaceHolder ID=PlaceHolderButton Runat=server/>"

asp:control-type syntax is used declaratively and parsed by the server. What you end up doing is delivering the literal text "<asp:PlaceHolder ID=PlaceHolderButton Runat=server/>" to the client browser, which can not, of course, create the server control.

You instead need to declare the contol programatically and add it to some collection (HtmlTableCell, PlaceHolder, Panel, etc. )

Dim myButton As New Button
myButton.Text = "Click me"
myButton.Id = "btnAdd"
' other properties...
placeHolder1.Controls.Add(myButton)

and I would add

'attachs event handler to your programmatically created button
AddHandler myButton.click, AddressOf Button_Click

'where you have this sub in the code-behind:
Private Sub Button_Click( ByVal sender As System.Object, ByVal e As System.EventArgs)
    'do stuff
End Sub
ASKER CERTIFIED SOLUTION
Avatar of ihenry
ihenry

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
HI guys, I increase the point (I will divide them if needed), I have one more issue related to this issue where I am trying to have remove buttons in my shopping cart on every line item, like this:

1  ABCDE     $15      REMOVE (button)
2  CDEFG     $20      REMOVE (button)
3  HIJKLM    $20      REMOVE (button)


How I am going to approach it, I can generate this display in HTML rather than inside the routine we are talking about above, but I need what approach I have to take since every time user clicks it, I need to post back and pass the product id to be removed.
I tried using Javascript but not working, using asp:button do not know how to pass product id, same for input runat=server.

Any idea?

Thanks
I'll move the last question as a new question.

Thanks