-
Dynamic Buttons disappearing
I've created an apsx page that takes data from a XML file and loads a number of image buttons on screen. The buttons would be positioned at different places depending on co-ordinates retrieved from within the XML file. The buttons get created depending on which option the user selects which is permanently displayed on screen. . .
however, even though I've added a handler for the clicking of the image button (shown)
myimgButton.Visible = True
AddHandler myimgButton.Click, AddressOf myimgbuttonclickhandler
PlaceHolder1.Controls.Add(myimgButton)
and the code is in place to pick this up (shown below)
Public Sub myimgbuttonclickhandler(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs)
...handling code in here
End Sub
Every time I click on one of the dynamically created buttons, the page reloads and all my magically created buttons disappear! It dosnt even go anywhere near the handler I created.
Any ideas? Are there options I should be setting for the form?
Cheers. . .
-
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
Thanks Phil - the article did help in understanding the page flow and loading, but it also alerted me to the saveviewstate and loadviewstate capabilities - I didnt know about these before!! Its still amateur night at ASP.NET!!
Anyway, had a lot of fun with the viewstates and reccreating buttons and whatnot. Even able to offer assistance to another post regarding dynamic web form controls!
Cheers
-
Just as a follow up to what I was doing - managed to get the code running the way I need it to and have listed below. The form has four buttons (named Button1 - Button4), a panel named Panel1 and a label named Label1. Based it around the code within MSDN. . .
When you click on Button1 it creates 3 dynamic buttons, Button2 = 4 dynamic buttons, Button3 = 5 dynamic buttons. Button4 removes all dynamic buttons on the form. Clicking the dynamic button will display the button name in the label control.
Anyway, the code is pasted below (and could do with a good tidy up which I'm going to do right this minute!) - if anyone can see any flaws or a better way of doing this, please let me know. . . .
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
Protected WithEvents Panel1 As System.Web.UI.WebControls.Panel
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Button2 As System.Web.UI.WebControls.Button
Protected WithEvents Button3 As System.Web.UI.WebControls.Button
Protected WithEvents Button4 As System.Web.UI.WebControls.Button
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Session("controlsdisplayed") = "TRUE" Then
RemoveControls()
Session("track") = "0"
viewstate("controlsadded") = Nothing
End If
If ViewState("controlsadded") Is Nothing Then
Session("track") = "3"
Session("controlsdisplayed") = "TRUE"
AddControls(3)
End If
If Session("controlsdisplayed") = "TRUE" Then
viewstate("controlsadded") = Nothing
End If
End Sub
Protected Overrides Sub LoadViewState(ByVal savedState As Object)
MyBase.LoadViewState(savedState)
If (ViewState("controlsadded") = True And CInt(Session("track")) > 0) Or Session("controlsdisplayed") = "TRUE" Then
AddControls(CInt(Session("track")))
End If
End Sub
Private Sub AddControls(ByVal intCount As Integer)
Do While intCount > 0
Dim dynamicbutton As New Button
AddHandler dynamicbutton.Click, AddressOf dynamicbutton_Click
dynamicbutton.Text = "Dynamic Button" & CStr(intCount)
dynamicbutton.ID = "dynamicbutton" & CStr(intCount)
Panel1.Controls.Add(New LiteralControl("<BR><BR>"))
Panel1.Controls.Add(dynamicbutton)
intCount = intCount - 1
Loop
ViewState("controlsadded") = True
End Sub
Private Sub RemoveControls()
Dim ctlControl As Control
Dim intcount As Integer = Session("track")
Do Until intcount = 0
ctlControl = Panel1.FindControl("dynamicbutton" & CStr(intcount))
Panel1.Controls.Remove(ctlControl)
intcount = intcount - 1
Loop
Session("controlsdisplayed") = "FALSE"
End Sub
Private Sub dynamicbutton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Label1.Text = sender.id
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If Session("controlsdisplayed") = "TRUE" Then
RemoveControls()
Session("track") = "0"
viewstate("controlsadded") = Nothing
End If
If ViewState("controlsadded") Is Nothing Then
Session("track") = "4"
Session("controlsdisplayed") = "TRUE"
AddControls(4)
End If
If Session("controlsdisplayed") = "TRUE" Then
viewstate("controlsadded") = Nothing
End If
End Sub
Private Sub button3_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If Session("controlsdisplayed") = "TRUE" Then
RemoveControls()
Session("track") = "0"
viewstate("controlsadded") = Nothing
End If
If viewstate("controlsadded") Is Nothing Then
Session("track") = "5"
Session("controlsdisplayed") = "TRUE"
AddControls(5)
End If
If Session("controlsdisplayed") = "TRUE" Then
viewstate("controlsadded") = Nothing
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
RemoveControls()
Session("track") = "0"
viewstate("controlsadded") = Nothing
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|