Internet (up to the 30th page on Google, with 100 results per page!!),
all without finding an answer to my question AS TO WHY IT IS IMPOSSIBLE
TO PROGRAMMATICALLY ADD A BUTTON TO A DYNAMICALLY CREATED PAGE.
Or, to be more precise, why it is impossible to have an onClick sub
respond to that button’s Click event.
My main page has only one line:
<%@ page inherits="TeamMetz" src="default.vb" language="VB"
contenttype="text/html" responseencoding="utf-8" %>
That’s it. Nothing more. Now that we have that out of the way, let’s
look at my code-behind.
I have created it so that the Page On_Load creates the header HTML (from
<html> to <body>) and the footer HTML (from </body> to </html>). Inside
these two parts sits the [If Not IsPostBack] portion that allows a
“first load” to proceed smoothly. This first load calls the LoadIntro(),
which pulls data from the db, and populates both a preview section as
well as a form (that is, the form is populated with the db contents, so
that they can be edited). When the button is pressed, what is *supposed*
to happen, is that a sub (the UpdateIntro() sub) *should* be called. The
UpdateIntro() sub is *supposed* to update the db with any changed
contents, and then re-call the LoadIntro() sub to re-populate the page.
What I have discovered is that no matter what I do, IT IS IMPOSSIBLE TO
SET A DYNAMICALLY CREATED BUTTON TO CALL A SUB. That is, I have tried
every trick in the book to have the button call the UpdateIntro() sub,
without success. I have tried [AddHandler submit.Click, AddressOf
UpdateIntro] when creating the button, I have also tried [Handles
submit.Click] when setting the UpdateIntro() sub.
NOTHING WORKS, as the UpdateIntro() sub FAILS TO BE SUCCESSFULLY CALLED.
Here is my Code-behind:
Imports System
Imports System.Configuration
Imports System.Data
Imports System.Data.OleDb
'Imports System.Drawing
'Imports System.Drawing.Imaging
'Imports System.IO
'Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HTMLControls
Public Class TeamMetz
Inherits Page
Dim vbCrLf As String = Microsoft.VisualBasic.vbCrLf
Dim strYear As String = DateTime.Now.Year.ToString()
Dim strWarning As String = vbCrLf & " <p>Have you <a
href=""/rules.aspx"">read the rules</a> for adding content? Do you know
how to <a href=""/formatting.aspx"">properly format text</a>?</p>" & vbCrLf
Dim myForm As New HTMLForm
Dim myConn As New
OleDbConnection(ConfigurationSettings.AppSettings( "strConn"))
Protected Sub Page_Load(sender As Object, e As EventArgs)
Dim strURL as String = Request.RawURL.ToString()
Dim strDO as String = Request.QueryString("do")
Dim intID as Integer = Request.QueryString("id")
Dim strP as String = Request.QueryString("parent")
Dim strC as String = Request.QueryString("child")
Dim strG as String = Request.QueryString("gchild")
Dim intE as Integer = Request.QueryString("entry")
Controls.Add(New LiteralControl("<?xml version=""1.0""
encoding=""UTF-8""?>" & vbCrLf & "<!DOCTYPE html PUBLIC ""-//W3C//DTD
XHTML 1.1//EN""" & vbCrLf & "
""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"">" & vbCrLf & "<html
xmlns=""http://www.w3.org/1999/xhtml"" xml:lang=""en"">" & vbCrLf & "
<head>" & vbCrLf & " <title>Team Metz Admin</title>" & vbCrLf & "
<meta name=""robots"" content=""none"" />" & vbCrLf & " <meta
name=""author"" content=""René Kabis, Kabis.NET - Technology Solutions
Consulting"" />" & vbCrLf & " <meta name=""generator"" content=""A
Text Editor - The only way to code!"" />" & vbCrLf & " <meta
name=""copyright"" content=""1998 - " & strYear & ", Larry Metz and Team
Metz Real Estate."" />" & vbCrLf & " <meta
name=""MSSmartTagsPreventParsing"" content=""true"" />" & vbCrLf & "
<meta http-equiv=""imagetoolbar"" content=""no"" />" & vbCrLf & "
<meta http-equiv=""Content-language"" content=""en"" />" & vbCrLf & "
<meta http-equiv=""Content-type"" content=""text/html; charset=utf-8""
/>" & vbCrLf & " <meta http-equiv=""Content-Script-Type""
content=""text/javascript"" />" & vbCrLf & " <meta
http-equiv=""Content-Style-Type"" content=""text/css"" />" & vbCrLf &
" <link type=""text/css"" rel=""stylesheet"" media=""all""
href=""/css/global.css"" />" & vbCrLf & " <link type=""text/css""
rel=""stylesheet"" media=""all"" href=""/css/calendar-win2k-1.css""
title=""win2k-1"" />" & vbCrLf & " <script type=""text/javascript""
src=""/js/popup.js""><" & "/script>" & vbCrLf & " <script
type=""text/javascript"" src=""/js/countdown.js""><" & "/script>" &
vbCrLf & "<!-- Web Standards Compliance Patch for Microsoft Browsers
-->" & vbCrLf & "<!--[if lt IE 7]>" & vbCrLf & " <script
src=""/js/IE7_0_9/ie7-standard-p.js"" type=""text/javascript""><" &
"/script>" & vbCrLf & "<![endif]-->" & vbCrLf & " </head>" & vbCrLf &
" <body>" & vbCrLf & " <div id=""body"">" & vbCrLf & " <div
id=""head"">" & vbCrLf & " <h1><a href=""/default.aspx""
title=""return to the main index"" rel=""home"">Team Metz
Admin</a></h1>" & vbCrLf & " </div>" & vbCrLf & " <div
id=""content"">" & vbCrLf))
If Not IsPostBack Then
Select Case strDo
Case "add"
Case "edit"
Case "delete"
Case "intro"
LoadIntro()
Case Else
Controls.Add(New LiteralControl(" <h2>Welcome</h2>" &
strWarning & " <p>Please choose a task from the following list:</p>"
& vbCrLf & " <ul>" & vbCrLf & " <li><a
href=""/default.aspx?do=add"">Add Data</a> to the database</li>" &
vbCrLf & " <li><a href=""/default.aspx?do=edit"">Edit Data</a> in
the database</li>" & vbCrLf & " <li><a
href=""/default.aspx?do=intro"">Edit Intro</a> on the front page</li>" &
vbCrLf & " </ul>"))
End Select
End If
Controls.Add(New LiteralControl(vbCrLf & " </div>" & vbCrLf &
" <div id=""foot"">" & vbCrLf & " <address>All Contents are
Copyright © 1998 - " & strYear & " Larry Metz and Team Metz</address>" &
vbCrLf & " <div>" & vbCrLf & " <a
href=""http://validator.w3.org/check/referer/"" rel=""external""><img
src=""/images/xhtml.png"" class=""footimg"" alt=""Valid XHTML 1.1""
/></a>" & vbCrLf & " <a
href=""http://jigsaw.w3.org/css-validator/validator?uri=http://admin.getmetz.com/css/global.css""
rel=""external""><img src=""/images/css.png"" class=""footimg""
alt=""Valid CSS 2.0"" /></a>" & vbCrLf & " <a
href=""http://dean.edwards.name/IE7/intro/"" rel=""external""><img
src=""/images/ie7.png"" class=""footimg"" alt=""Standards Compliance
Plugin for Internet Explorer"" /></a>" & vbCrLf & " <a
href=""http://www.mozilla.com/"" rel=""external""><img
src=""/images/firefox.png"" class=""footimg"" alt=""Get Firefox!""
/></a>" & vbCrLf & " <a href=""http://www.mozilla.com/""
rel=""external""><img src=""/images/thunderbird.png"" class=""footimg""
alt=""Get Thunderbird!"" /></a>" & vbCrLf & " </div>" & vbCrLf &
" </div>" & vbCrLf & " </div>" & vbCrLf & " </body>" & vbCrLf &
"</html>"))
End Sub
Sub LoadIntro()
Controls.Add(New LiteralControl(" <h2>Edit Intro</h2>" & strWarning))
myForm.ID = "myForm"
Controls.Add(myForm)
Dim myCmd as New OleDbCommand("SELECT [Comment] FROM [tblIntro]",
myConn)
myConn.Open()
myForm.Controls.Add(New LiteralControl(" <p>This is what is
currently in the Database:</p>" & vbCrLf & " <blockquote>" & vbCrLf &
" <p>" & FormatData(myCmd.ExecuteScalar()) & "</p>" & vbCrLf & "
</blockquote>" & vbCrLf & " <p>Use the form below to edit the
contents of the database and submit the changes:</p>" & vbCrLf & "
<div class=""center"">" & vbCrLf & " "))
Dim textarea As New TextBox
textarea.id = "IntroComment"
textarea.Text = myCmd.ExecuteScalar()
textarea.Rows = "8"
textarea.Columns = "80"
textarea.TextMode = TextBoxMode.MultiLine
textarea.Wrap = true
myForm.Controls.Add(textarea)
myForm.Controls.Add(New LiteralControl("<br />" & vbCrLf & " "))
Dim submit As New Button
AddHandler submit.Click, AddressOf UpdateIntro
submit.id = "submit"
submit.Text = "Update Intro"
myForm.Controls.Add(submit)
myForm.Controls.Add(New LiteralControl(vbCrLf & " </div>" & vbCrLf))
myConn.Close()
End Sub
Private Sub UpdateIntro(sender As Object, e As System.EventArgs)
Dim myCmd as New OleDbCommand("UPDATE tblIntro SET
[Comment]=@Comment", myConn)
myConn.Open()
myCmd.CommandType = CommandType.Text
myCmd.Parameters.Add("@Comment", OleDbType.LongVarWChar).Value =
RepChar(Request.Form("IntroComment"))
myCmd.ExecuteNonQuery()
myConn.Close()
LoadIntro()
End Sub
Function FormatDate(sItem as Object) as String
Return String.Format("<p class=""small"">Posted: <i>{0:dddd, dd MMMMM,
yyyy}</i></p>", sItem)
End Function
Function FormatData(sItem) as String
FormatData=sItem.Replace(vbcrlf,"</p>" & vbCrLf & "
<p>").Replace("","<b>").Replace("","</b>").Replace("","<i>").Replace("","</i>").Replace("</p>"
& vbCrLf & " <p>
- </p>" & vbCrLf & " <p>[item]","</p>" &
vbCrLf & " <ul>" & vbCrLf & " <li>").Replace("</p>" & vbCrLf
& " <p>[item]","</li>" & vbCrLf & " <li>").Replace("</p>" &
vbCrLf & " <p>
End Function
Function RepChar(sItem) as String
RepChar=sItem.Replace("'","’").Replace(" & ", " &
").Replace("<","<").Replace(">",">")
End Function
End Class