By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

Programmatically create buttons from user control

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hello Jonah,

Jonah Olsson wrote:
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
Nov 18 '05 #2

P: n/a
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
"Eric Veltman" <eric@[RemoveThis]veltman.nu> skrev i meddelandet
news:vt************@corp.supernews.com...
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.

Nov 18 '05 #3

P: n/a
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
"Eric Veltman" <eric@[RemoveThis]veltman.nu> skrev i meddelandet
news:vt************@corp.supernews.com...
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.


Nov 18 '05 #4

P: n/a
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
"Eric Veltman" <eric@[RemoveThis]veltman.nu> skrev i meddelandet
news:vt************@corp.supernews.com...
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.

Nov 18 '05 #5

P: n/a
Hello Jonah,

Jonah Olsson wrote:
I suppose it's impossible to move the AddHandler into the
BaseControlTemplate, but I can't get it work as I have it now.
It is possible to also pass a reference to the event handler
to the createButton method. You can use delegates for this.
Delegates are something like type safe function pointers,
which means that you can only have them point to methods
that have the same signature ( number of arguments, argument types,
return type ) as in the delegate declaration.

You can create your own delegate types, but in this case
creating an argument of delegate type System.EventHandler
will do, because button click event handlers are of this type.

The signature for your method would become something like :
Public Sub createButton(
ByVal text As String,
ByVal id As String,
ByVal handler As System.EventHandler)

And you would call the method like :
createButton(
"Login",
"cmdLogin",
New System.EventHandler(cmdLogin_click))

[ Sorry if there are errors, I'm a C# guy ... ]
Object referencing are missing - why? I've added the reference
cmdLogin in my ascx-code (Protected WithEvents cmdLogin As Button).


I assume you get the error when calling AddHandler ?
Seems to me like cmdLogin does not reference the button object
you created. Note that though you created a new button object
with ID "cmdLogin" from your method doesn't mean that your
cmdLogin variable suddenly automatically points to it.
But anyway, now you can do the AddHandler inside createButton,
there's no need to have the cmdLogin helper variable anymore.

Best regards,

Eric
Nov 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.