473,473 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Programmatically create buttons from user control

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 20 '05 #1
2 1533
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 20 '05 #2
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 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David J. Berman | last post by:
Thanks for any help...! My error is: Object reference not set to an instance of an object. > public int DisplayOrder { > get { >>>>>> return (int) ViewState; > }
1
by: Martine | last post by:
Hi there! I have a problem with programmatically adding user controls to my mobile webforms. If I load my usercontrol programmatically (in the Page_Load), the object is instantiated, I have...
5
by: Jonah Olsson | last post by:
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...
3
by: Neil | last post by:
I'm not sure where I should be asking this question but this seemed as good a starting point as any. I will shortly be building a asp.net application using v2 of the .net framework. I want to...
15
by: Oleg Subachev | last post by:
I need to programmatically invoke from other class Click event of the Button on my Form. Button.OnClick method is protected, not public. How to perform this ? Oleg Subachev
3
by: Ken Fine | last post by:
I'm interested in programmatically manipulating groups of ASP.NET controls by type. Can someone suggest code for the following? Loop through, say, all label controls on a page, and assigning a...
2
by: ChrisCicc | last post by:
Hi All, I got a real doozy here. I have read hundreds upon hundreds of forum posts and found numerous others who have replicated this problem, but have yet to find a solution. Through testing I have...
3
by: =?Utf-8?B?QW5nZWw=?= | last post by:
I seem to understand how the control works as long as I mated to SqlDataSource or ObjDataSource it works fine. But I do not want to flatten my design in this manner. I am interested in how I can...
3
by: Ken Fine | last post by:
I am using ASP.NET's CreateUserWizard control. I want to force the visitor to use a username and e-mail address that I am providing in programming, and I do not want the visitor to be able to edit...
2
by: Tina | last post by:
How can I programmatically navigate a website just as a user would? click on buttons, hyperlinks, etc. I know how to read nodes using XPath but I don't know what classes to use to navigate. I...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.