Programmatically create buttons from user control

  • Thread starter Thread starter Jonah Olsson
  • Start date Start date
J

Jonah Olsson

Hello guys,

I have an application which is built upon several user controls. That is, I
have a default template (default.aspx) that I load a user control into
(using placeholders in the template).

Now, the template also contains a placeholder for buttons used by each user
control. The current solution needs each user control to create its buttons,
like;

Dim button As New Button
button.Text = "buttontext"
button.CssClass = "button"
button.ID = "button_id"

CType(Page.FindControl("buttonHolder"),
PlaceHolder).Controls.Add(button)

AddHandler button.Click, AddressOf button_click

But instead I would like a more sophisticated method to simplify this and
reduce code. Maybe using classes? I would like to just call a function like;
CreateButton(buttonTitle As String, buttonId As String, addOnClickHandler As
String)

This function would also create AddHandlers and all that I currently need to
add within each user control "by hand".
Since the button placeholder mostly contains more than one button for each
user control, maybe CreateButton should add the button to a array and then
add that one to the placeholder through another function?

Has anyone any experience in this? I'm very thankful for any feedback or
ideas..
Thanks.

Regards,
Jonah Olsson
 
Hello Jonah,

Jonah said:
Now, the template also contains a placeholder for buttons used by each
user control. The current solution needs each user control to create its
buttons, like;

Dim button As New Button
button.Text = "buttontext"
button.CssClass = "button"
button.ID = "button_id"

CType(Page.FindControl("buttonHolder"),
PlaceHolder).Controls.Add(button)

AddHandler button.Click, AddressOf button_click

But instead I would like a more sophisticated method to simplify this and
reduce code. Maybe using classes? I would like to just call a function
like; CreateButton(buttonTitle As String, buttonId As String,
addOnClickHandler As String)

If you want a method available in all your user controls that
dynamically adds a button to a placeholder on the mainpage,
then creating a base class for your user controls is a
good way to get that. Just derive a class from
System.Web.UI.UserControl and put your method in it.

Then change the base class of all your existing user controls
to the one you've just created and from now on use it as base
class for all new user controls and voila, you have the method
available in all your user controls.
Since the button placeholder mostly contains more than one button for each
user control, maybe CreateButton should add the button to a array and then
add that one to the placeholder through another function?

I don't see any reason for that, I think just calling the method,
that you created in the custom base class, multiple times will do fine.

Best regards,

Eric
 
Hello Eric,
and thank you for your help!

I made some changes to my code based upon your suggestions, please see
below. I suppose it's impossible to move the AddHandler into the
BaseControlTemplate, but I can't get it work as I have it now. Object
referencing are missing - why? I've added the reference cmdLogin in my
ascx-code (Protected WithEvents cmdLogin As Button).

Best regards,
Jonah

' // From login.ascx in Page_Load (inherits BaseControlTemplate)
createButton("Login", "cmdLogin", "cmdLogin_click")
AddHandler cmdLogin.Click, AddressOf cmdLogin_click

' // BaseControlTemplate
Imports System
Imports System.Web.UI.WebControls

Public Class BaseControlTemplate
Inherits Web.UI.UserControl

Private buttonHolder As New PlaceHolder

Public Sub createButton(ByVal text As String, ByVal id As String, ByVal
handler As String)

Dim newButton As New CreateButton(text, True, id)

buttonHolder = CType(Me.Page.FindControl("buttonHolder"),
PlaceHolder)
buttonHolder.Controls.Add(newButton)

End Sub

End Class

Public Class CreateButton
Inherits Web.UI.WebControls.Button

Public Sub New(ByVal _title As String, ByVal _causesValidation As
Boolean, ByVal _ID As String)
Try
Me.Text = _title
Me.CssClass = "button"
Me.EnableViewState = True
Me.ID = _ID
Me.CausesValidation = _causesValidation
Catch

End Try
End Sub

End Class
 
Back
Top