Home All Groups Group Topic Archive Search About

Adding Button Programatically - have an answer???

Author
14 Mar 2006 5:55 PM
Neo Geshel
I have strip-mined, strip-searched, and completely exhausted the
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]","<b>").Replace("[/b]","</b>").Replace("[i]","<i>").Replace("[/i]","</i>").Replace("</p>"
& vbCrLf & "    <p>[list]</p>" & vbCrLf & "    <p>[item]","</p>" &
vbCrLf & "      <ul>" & vbCrLf & "        <li>").Replace("</p>" & vbCrLf
& "    <p>[item]","</li>" & vbCrLf & "        <li>").Replace("</p>" &
vbCrLf & "    <p>[/list]","</li>" & vbCrLf & "      </ul>" & vbCrLf)
End Function
Function RepChar(sItem) as String
  RepChar=sItem.Replace("'","’").Replace(" & ", " &amp;
").Replace("<","&lt;").Replace(">","&gt;")
End Function

End Class

Author
14 Mar 2006 6:07 PM
Patrice
I believe I already suggested for this problem, to investigate a scenario
such as (and it looks like your code confirms this) :
- LoadIntro is called only when you don't postback
- remember that the page is created *each* time the HTTP requests hits your
server

As a result the button doesn't have any more its handler (and is perhaps not
even recreated at all). Make sure that the control are always created (with
their handler). Also I don't have the exact info but double check also the
control lifecycle to make sure that the control is created before events
wiring is done (else the ASP.NET infrastructure might well not automatically
call the server side click event if the button is not created yet). From the
top of my head, init would be the preferred event for dynamically created
controls...

If all else fails, try to a simple page that just always creates the
control. It should work, and from this test bed, you should easily be able
to experiment to find out the possible remaining differences and the best
strategy...

Good luck.

--
Patrice

"Neo Geshel" <got***@geshel.org> a écrit dans le message de
news:OsDRf.143774$B94.71430@pd7tw3no...
I have strip-mined, strip-searched, and completely exhausted the
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]","<b>").Replace("[/b]","</b>").Replace("[i]","<i>").Repla
ce("[/i]","</i>").Replace("</p>"
& vbCrLf & "    <p>[list]</p>" & vbCrLf & "    <p>[item]","</p>" &
vbCrLf & "      <ul>" & vbCrLf & "        <li>").Replace("</p>" & vbCrLf
& "    <p>[item]","</li>" & vbCrLf & "        <li>").Replace("</p>" &
vbCrLf & "    <p>[/list]","</li>" & vbCrLf & "      </ul>" & vbCrLf)
End Function
Function RepChar(sItem) as String
  RepChar=sItem.Replace("'","'").Replace(" & ", " &amp;
").Replace("<","&lt;").Replace(">","&gt;")
End Function

End Class
Author
14 Mar 2006 6:07 PM
tfsmag
wow what a mess!

but to be more helpful... I couldn't see where your <form> tag was at
on that page...

asp.net controls have to be contained within a form tag. example:

<form id="form1" runat="server">
-- your asp.net controls and content--
</form>

I couldn't see a form tag in there anywhere, it seems kind of odd to do
things that way, any particular reason why you didn't just render the
html in the aspx page instead of the vb page?
Author
14 Mar 2006 6:12 PM
tfsmag
nevermind, i see it now... it's the "myform" control. Sorry...
Author
14 Mar 2006 6:24 PM
Cor Ligthert [MVP]
Neo,

I have the idea that somebody is hacking the newsgroups at the moment.
A search on Herfried in this newsgroups gives 69 results, I am sure that it
is not the correct amount of messages Herfried has done.

Button gives me 4 results, I saw this starting on sunday.

I am sure that this sample of mine has been more times in these dotnet
newsgroups.

\\\\
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
        Dim mybutton As Button
        Dim i As Integer
        For i = 0 To New Date().DaysInMonth _
        (New Date().Year, New Date().Month) - 1
            mybutton = New Button
            mybutton.BackColor = Drawing.Color.White
            mybutton.Text = (i + 1).ToString
            mybutton.Width = New Unit(30)
            Me.Panel1.Controls.Add(mybutton)
            AddHandler mybutton.Click, AddressOf mybutton_Click
            If (i + 1) Mod 5 = 0 Then
                Me.Panel1.Controls.Add(New LiteralControl("<BR>"))
            End If
        Next
End Sub
Private Sub mybutton_Click _
    (ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mylabel As New Label
        Me.Panel1.Controls.Add(New LiteralControl("<BR><BR>"))
        Me.Panel1.Controls.Add(mylabel)
        mylabel.Text = "The day is: " & DirectCast(sender, Button).Text
End Sub
///

I will place it tomorrow on our website.


I hope this helps,

Cor
Author
14 Mar 2006 9:03 PM
Neo Geshel
Cor Ligthert [MVP] wrote:
Show quoteHide quote
> Neo,
>
> I have the idea that somebody is hacking the newsgroups at the moment.
> A search on Herfried in this newsgroups gives 69 results, I am sure that it
> is not the correct amount of messages Herfried has done.
>
> Button gives me 4 results, I saw this starting on sunday.
>
> I am sure that this sample of mine has been more times in these dotnet
> newsgroups.
>
> \\\\
>  Private Sub Page_Load(ByVal sender As System.Object, _
>  ByVal e As System.EventArgs) Handles MyBase.Load
>         Dim mybutton As Button
>         Dim i As Integer
>         For i = 0 To New Date().DaysInMonth _
>         (New Date().Year, New Date().Month) - 1
>             mybutton = New Button
>             mybutton.BackColor = Drawing.Color.White
>             mybutton.Text = (i + 1).ToString
>             mybutton.Width = New Unit(30)
>             Me.Panel1.Controls.Add(mybutton)
>             AddHandler mybutton.Click, AddressOf mybutton_Click
>             If (i + 1) Mod 5 = 0 Then
>                 Me.Panel1.Controls.Add(New LiteralControl("<BR>"))
>             End If
>         Next
> End Sub
> Private Sub mybutton_Click _
>     (ByVal sender As Object, ByVal e As System.EventArgs)
>         Dim mylabel As New Label
>         Me.Panel1.Controls.Add(New LiteralControl("<BR><BR>"))
>         Me.Panel1.Controls.Add(mylabel)
>         mylabel.Text = "The day is: " & DirectCast(sender, Button).Text
> End Sub
> ///
>
> I will place it tomorrow on our website.
>
>
> I hope this helps,
>
> Cor
>
>

Adding Handles MyBase.Load to the Page_Load causes the contents of the
page to appear twice. That is, the entire contents of the Page_Load is
rendered twice... so when I do a view source in the browser, I see two
copies of the correct source code, one after another.

...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org.                                 *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer   *
*  god than you do. When you understand why you dismiss all the other *
*  possible gods, you will understand why I dismiss yours.”           *
*  - Stephen F. Roberts                                               *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationism”) is just  *
*  as ignorant, irrational and ill-educated as someone who believes   *
*  that the world is a flat disc, that the Sun circles the Earth or   *
*  that there really is a tooth fairy. Darwinism has an overwhelming  *
*  foundation of evidence that can be tested and reproduced.          *
*                                      *
* “Intelligent Design, on the other hand, has no evidence at all; not *
*  one single shred of testable proof. As such, Intelligent Design is *
*  Religious Mythology, and has no right whatsoever to be in our      *
*  Science classrooms.” - 99.99+% of Scientists                  *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
Author
14 Mar 2006 8:23 PM
Neo Geshel
I have created a dramatically cut-down version of the example I wish to
demonstrate. This example fails to function as it should. As I look
through the code, I fail to see what might be going wrong, but the
evidence speaks for itself: the submit button fails to properly call the
  UpdateIntro() sub.

Main Page:

<%@ page inherits="TeamMetz" src="test1.vb" language="VB"
contenttype="text/html" responseencoding="utf-8" %>

Code-Behind (test1.vb):

Imports System
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 myForm As New HTMLForm
   Dim textarea As New TextBox
   Dim submit As New Button

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     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>Test</title>" & vbCrLf & "</head>" &
vbCrLf & "  <body>" & vbCrLf & "    <div>"))
     If Not IsPostBack Then
       LoadIntro()
     End If
     Controls.Add(New LiteralControl( vbCrLf & "    </div>" & vbCrLf & "
  </body>" & vbCrLf & "</html>"))
   End Sub

   Private Sub LoadIntro()
     myForm.ID = "myForm"
     Controls.Add(myForm)
     textarea.id = "IntroComment"
     textarea.Rows = "8"
     textarea.Columns = "80"
     textarea.TextMode = TextBoxMode.MultiLine
     textarea.Wrap = true
     myForm.Controls.Add(textarea)
     myForm.Controls.Add(New LiteralControl("<br />" & vbCrLf & "      "))
     AddHandler submit.Click, AddressOf UpdateIntro
     submit.id = "submit"
     submit.Text = "Update Intro"
     myForm.Controls.Add(submit)
   End Sub
   Private Sub UpdateIntro(sender As Object, e As EventArgs)
     Controls.Add(New LiteralControl("<p>" &
Request.Form("IntroComment") & "</p>"))
   End Sub
End Class



TIA.
...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org.                                 *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer   *
*  god than you do. When you understand why you dismiss all the other *
*  possible gods, you will understand why I dismiss yours.”          *
*  - Stephen F. Roberts                                               *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationism”) is just  *
*  as ignorant, irrational and ill-educated as someone who believes   *
*  that the world is a flat disc, that the Sun circles the Earth or   *
*  that there really is a tooth fairy. Darwinism has an overwhelming  *
*  foundation of evidence that can be tested and reproduced.          *
*                                      *
* “Intelligent Design, on the other hand, has no evidence at all;not *
*  one single shred of testable proof. As such, Intelligent Design is *
*  Religious Mythology, and has no right whatsoever to be in our      *
*  Science classrooms.” - 99.99+% of Scientists                  *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
Author
14 Mar 2006 8:33 PM
Marina Levit [MVP]
As others have said - you need to add all the dynamic controsl *EVERY* time
the page loads.

Remove the 'If Not IsPostback Then' statement. This means LoadInto will
always get called.

"Neo Geshel" <got***@geshel.org> wrote in message
news:umDqoU6RGHA.5808@TK2MSFTNGP12.phx.gbl...
I have created a dramatically cut-down version of the example I wish to
demonstrate. This example fails to function as it should. As I look
through the code, I fail to see what might be going wrong, but the
evidence speaks for itself: the submit button fails to properly call the
  UpdateIntro() sub.

Main Page:

<%@ page inherits="TeamMetz" src="test1.vb" language="VB"
contenttype="text/html" responseencoding="utf-8" %>

Code-Behind (test1.vb):

Imports System
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 myForm As New HTMLForm
   Dim textarea As New TextBox
   Dim submit As New Button

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     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>Test</title>" & vbCrLf & "</head>" &
vbCrLf & "  <body>" & vbCrLf & "    <div>"))
     If Not IsPostBack Then
       LoadIntro()
     End If
     Controls.Add(New LiteralControl( vbCrLf & "    </div>" & vbCrLf & "
  </body>" & vbCrLf & "</html>"))
   End Sub

   Private Sub LoadIntro()
     myForm.ID = "myForm"
     Controls.Add(myForm)
     textarea.id = "IntroComment"
     textarea.Rows = "8"
     textarea.Columns = "80"
     textarea.TextMode = TextBoxMode.MultiLine
     textarea.Wrap = true
     myForm.Controls.Add(textarea)
     myForm.Controls.Add(New LiteralControl("<br />" & vbCrLf & "   "))
     AddHandler submit.Click, AddressOf UpdateIntro
     submit.id = "submit"
     submit.Text = "Update Intro"
     myForm.Controls.Add(submit)
   End Sub
   Private Sub UpdateIntro(sender As Object, e As EventArgs)
     Controls.Add(New LiteralControl("<p>" &
Request.Form("IntroComment") & "</p>"))
   End Sub
End Class



TIA.
....Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org.                                 *
***********************************************************************
* "I contend that we are both atheists. I just believe in one fewer   *
*  god than you do. When you understand why you dismiss all the other *
*  possible gods, you will understand why I dismiss yours."          *
*  - Stephen F. Roberts                                               *
***********************************************************************
* "Anyone who believes in Intelligent Design ("creationism") is just  *
*  as ignorant, irrational and ill-educated as someone who believes   *
*  that the world is a flat disc, that the Sun circles the Earth or   *
*  that there really is a tooth fairy. Darwinism has an overwhelming  *
*  foundation of evidence that can be tested and reproduced.          *
*       *
* "Intelligent Design, on the other hand, has no evidence at all;not *
*  one single shred of testable proof. As such, Intelligent Design is *
*  Religious Mythology, and has no right whatsoever to be in our      *
*  Science classrooms." - 99.99+% of Scientists       *
***********************************************************************
Mignon McLaughlin once said that "A nymphomaniac is a woman [who is] as
obsessed with sex as the average man." Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
Author
14 Mar 2006 8:46 PM
Neo Geshel
Marina Levit [MVP] wrote:
> As others have said - you need to add all the dynamic controsl *EVERY* time
> the page loads.
>
> Remove the 'If Not IsPostback Then' statement. This means LoadInto will
> always get called.

But that is also the problem... I only want loadintro() loaded on the
first load. In this test example, I don't want it loaded on the
postback. How do I overcome that?

TIA
...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org.                                 *
***********************************************************************
* “I contend that we are both atheists. I just believe in one fewer   *
*  god than you do. When you understand why you dismiss all the other *
*  possible gods, you will understand why I dismiss yours.”           *
*  - Stephen F. Roberts                                               *
***********************************************************************
* “Anyone who believes in Intelligent Design (“creationism”) is just  *
*  as ignorant, irrational and ill-educated as someone who believes   *
*  that the world is a flat disc, that the Sun circles the Earth or   *
*  that there really is a tooth fairy. Darwinism has an overwhelming  *
*  foundation of evidence that can be tested and reproduced.          *
*                                      *
* “Intelligent Design, on the other hand, has no evidence at all; not *
*  one single shred of testable proof. As such, Intelligent Design is *
*  Religious Mythology, and has no right whatsoever to be in our      *
*  Science classrooms.” - 99.99+% of Scientists                  *
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.” Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
Author
14 Mar 2006 9:51 PM
Marina Levit [MVP]
I didn't read through the code carefully enough to see what else is going on
in that code. Maybe you need to split that method out into 2 methods.

All I know is that you *MUST* re-add the button and its event handler after
the postback. You need to do this every time the page loads - not just the
first time.

So maybe you need to restructure your code, but one way or another the
button and its handler need to be always added if you expect your code to
work.

"Neo Geshel" <got***@geshel.org> wrote in message
news:OMiYPh6RGHA.440@TK2MSFTNGP10.phx.gbl...
Marina Levit [MVP] wrote:
> As others have said - you need to add all the dynamic controsl *EVERY*
> time the page loads.
>
> Remove the 'If Not IsPostback Then' statement. This means LoadInto will
> always get called.

But that is also the problem... I only want loadintro() loaded on the
first load. In this test example, I don't want it loaded on the
postback. How do I overcome that?

TIA
....Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org.                                 *
***********************************************************************
* "I contend that we are both atheists. I just believe in one fewer   *
*  god than you do. When you understand why you dismiss all the other *
*  possible gods, you will understand why I dismiss yours."           *
*  - Stephen F. Roberts                                               *
***********************************************************************
* "Anyone who believes in Intelligent Design ("creationism") is just  *
*  as ignorant, irrational and ill-educated as someone who believes   *
*  that the world is a flat disc, that the Sun circles the Earth or   *
*  that there really is a tooth fairy. Darwinism has an overwhelming  *
*  foundation of evidence that can be tested and reproduced.          *
*       *
* "Intelligent Design, on the other hand, has no evidence at all; not *
*  one single shred of testable proof. As such, Intelligent Design is *
*  Religious Mythology, and has no right whatsoever to be in our      *
*  Science classrooms." - 99.99+% of Scientists       *
***********************************************************************
Mignon McLaughlin once said that "A nymphomaniac is a woman [who is] as
obsessed with sex as the average man." Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************