Link to home
Start Free TrialLog in
Avatar of nmachin
nmachin

asked on

Dynamically Build a Server Side Button Object

I've built an application that gets information from a database and dynamically displays controls
in asp.net pages.  The application is used to build surveys.  The users create their surveys using objects such as textboxes, dropdowns, radio buttons.  I'm using an HTMLGenericControl with a divcontainer to hold the objects.  When the app runs the divcontainer displays the survey.  Everything works so far but I'm having a problem building a server side button object that will work the same way.  The only button object I've been able to create so far has an OnClientClick method but no OnClick method.  Does anyone know how to build a server side button object?
Avatar of MaxOvrdrv2
MaxOvrdrv2

you need to assign a handler function to it when you create it... example... hope this helps:


Dim chkage As New CheckBox
chkage.ID = "chkAgeDep" + ControlID.ToString
chkage.AutoPostBack = True
AddHandler chkage.CheckedChanged, AddressOf chkageDep_ButtonHandler
 
 
Public Sub chkageDep_ButtonHandler(ByVal sender As Object, ByVal e As EventArgs)
 
End Sub

Open in new window

Avatar of nmachin

ASKER

I've already successfully created other objects such as checkboxes, radiobuttons, textboxes but I can't seem to create a button object the same way.  Any suggestions for button objects specifically?
the button should be called the exact same way... example:


Dim btnage As New Button
btnage.ID = "btnAgeDep" + ControlID.ToString
btnage.AutoPostBack = True
AddHandler btnage.Click, AddressOf btnageDep_ButtonHandler
 
 
Public Sub btnageDep_ButtonHandler(ByVal sender As Object, ByVal e As EventArgs)
 
End Sub
 
 
this should work perfectly... i have a version working in front of me right now... 

Open in new window

As MaxOvrdrv2 mentions you can create button dyanmically but for using the functionality of server-side click event, you have to attach it to a Sub Routine using AddHandler.
Please see the attached code snippet. Hope this helps.

Dim btn As New Button()
btn.ID = "btn_" & SomeRandomNumber.ToString()
AddHandler btn.Click, AddressOf ButtonClickEventHandler
 
Private Sub ButtonClickEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)
'Code here for handling click event of dynamically generated button
 
'use this object for recognizing which button has been pressed using various properties as set in earlier code
Dim btn As Button = CType(sender, Button)
 
End Sub

Open in new window

Avatar of nmachin

ASKER

I'm trying to use your example but intellisense is not giving me "ButtonClickEventHandler" as an option.  Do I need to include something else?
no no no... use my example since his example is the exact same as mine... and intellisense won't give you anything because "ButtonClickEventHandler" is the NAME OF THE FUNCTION YOU WILL USE TO HANDLE THE CLICK...
Avatar of nmachin

ASKER

I know.  I saw how dumb my question was after I submitted it.  I'm trying out your solutions right now and will give points once I'm done.  Thanks.
no one said dumb... the way he wrote it would've made me think of inherent function too... althought there's little way to call it differently... maybe in -=text=- would've made it clearer... anyway... no problems, take your time.
I suggested the ButtonClickEventHandler to let you know that you can name anything to this routine and it is not necessarily mandatory to use the style which VS2008 automatically adds for design-time created objects.
Avatar of nmachin

ASKER

Ok, I have my code like MaxOvrdrv2 except it doesn't give me the option of autopostback.  When the button is clicked, it refreshes the page but doesn't hit the event handler.

Dim btnEdit As New Button
btnEdit.ID = "btnEditQ"
btnEdit.Text = "Edit"
AddHandler btnEdit.Click, AddressOf ButtonClickEventHandler

Private Sub ButtonClickEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)
      Response.Redirect("EditQuestion.aspx?surveyid='" & SurveyID.Value & "'&questionid='" & QuestionID.Value & "'")
End Sub
You must add the code to create dynamic controls in Page_Init event so each time page is initialized the control(s) could be created again so they might get appropriately wired with the events they are being hooked with using AddHandler keyword.
Please use the code provided below.

Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
 
	Dim btnEdit As New Button()
	btnEdit.ID = "btnEditQ"
	btnEdit.Text = "Edit"
	btnEdit.CausesValidation = True
 
	AddHandler btnEdit.Click, AddressOf ButtonClickEventHandler
 
	Form.Controls.Add(btnEdit)
 
End Sub
 
Protected Sub ButtonClickEventHandler(ByVal sender As Object, ByVal e As System.EventArgs)
 
	Response.Write("Dynamic Button Click Event fired. Control's ID = " & CType(sender, Button).ID)
 
End Sub

Open in new window

Avatar of nmachin

ASKER

As I said before I'm building my pages dynamically and can't just add the button to the form.  The placement needs to be specific as in the code below.  If the question is a textbox, this code will run and place the button next to the textbox.  Each case will be different as to where the button will be placed.  I'm a beginner so either I don't understand what you guys are showing me or maybe it works for you but not for what I'm doing.

Case 1
                        
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl(strQuestion))
divContainer.Controls.Add(New LiteralControl("<br/>"))
Dim box As New TextBox
divContainer.Controls.Add(box)
divContainer.Controls.Add(btnEdit)
divContainer.Controls.Add(new LiteralControl("<br/>"))
divContainer.Controls.Add(new LiteralControl("<br/>"))                        
ASKER CERTIFIED SOLUTION
Avatar of MaxOvrdrv2
MaxOvrdrv2

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
SOLUTION
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