Adding Button Programatically - Doesn’t anyone have an answer???

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Neo Geshel

    #1

    Adding Button Programatically - Doesn’t anyone have an answer???

    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 PROGRAMMATICALL Y 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="TeamM etz" src="default.vb " language="VB"
    contenttype="te xt/html" responseencodin g="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.Configur ation
    Imports System.Data
    Imports System.Data.Ole Db
    'Imports System.Drawing
    'Imports System.Drawing. Imaging
    'Imports System.IO
    'Imports System.Web.Secu rity
    Imports System.Web.UI
    Imports System.Web.UI.W ebControls
    Imports System.Web.UI.H TMLControls

    Public Class TeamMetz
    Inherits Page
    Dim vbCrLf As String = Microsoft.Visua lBasic.vbCrLf
    Dim strYear As String = DateTime.Now.Ye ar.ToString()
    Dim strWarning As String = vbCrLf & " <p>Have you <a
    href=""/rules.aspx"">re ad 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 (ConfigurationS ettings.AppSett ings("strConn") )

    Protected Sub Page_Load(sende r As Object, e As EventArgs)
    Dim strURL as String = Request.RawURL. ToString()
    Dim strDO as String = Request.QuerySt ring("do")
    Dim intID as Integer = Request.QuerySt ring("id")
    Dim strP as String = Request.QuerySt ring("parent")
    Dim strC as String = Request.QuerySt ring("child")
    Dim strG as String = Request.QuerySt ring("gchild")
    Dim intE as Integer = Request.QuerySt ring("entry")
    Controls.Add(Ne w 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=""generato r"" content=""A
    Text Editor - The only way to code!"" />" & vbCrLf & " <meta
    name=""copyrigh t"" content=""1998 - " & strYear & ", Larry Metz and Team
    Metz Real Estate."" />" & vbCrLf & " <meta
    name=""MSSmartT agsPreventParsi ng"" content=""true" " />" & vbCrLf & "
    <meta http-equiv=""imageto olbar"" 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=""styleshee t"" media=""all""
    href=""/css/global.css"" />" & vbCrLf & " <link type=""text/css""
    rel=""styleshee t"" 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"">Te am 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(Ne w 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(Ne w 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.or g/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=h ttp://admin.getmetz.c om/css/global.css""
    rel=""external" "><img src=""/images/css.png"" class=""footimg ""
    alt=""Valid CSS 2.0"" /></a>" & vbCrLf & " <a
    href=""http://dean.edwards.na me/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(Ne w LiteralControl( " <h2>Edit Intro</h2>" & strWarning))
    myForm.ID = "myForm"
    Controls.Add(my Form)
    Dim myCmd as New OleDbCommand("S ELECT [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(myCm d.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 = "IntroComme nt"
    textarea.Text = myCmd.ExecuteSc alar()
    textarea.Rows = "8"
    textarea.Column s = "80"
    textarea.TextMo de = TextBoxMode.Mul tiLine
    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(sen der As Object, e As System.EventArg s)
    Dim myCmd as New OleDbCommand("U PDATE tblIntro SET
    [Comment]=@Comment", myConn)
    myConn.Open()
    myCmd.CommandTy pe = CommandType.Tex t
    myCmd.Parameter s.Add("@Comment ", OleDbType.LongV arWChar).Value =
    RepChar(Request .Form("IntroCom ment"))
    myCmd.ExecuteNo nQuery()
    myConn.Close()
    LoadIntro()
    End Sub

    Function FormatDate(sIte m as Object) as String
    Return String.Format(" <p class=""small"" >Posted: <i>{0:dddd, dd MMMMM,
    yyyy}</i></p>", sItem)
    End Function
    Function FormatData(sIte m) as String
    FormatData=sIte m.Replace(vbcrl f,"</p>" & vbCrLf & "
    <p>").Replace ("","<b>").Replac e("","</b>").Replace( "","<i>").Replac e("","</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>
    ","</li>" & vbCrLf & " </ul>" & vbCrLf)
    End Function
    Function RepChar(sItem) as String
    RepChar=sItem.R eplace("'","’ ").Replace( " & ", " &amp;
    ").Replace("<", "&lt;").Replace (">","&gt;")
    End Function

    End Class
  • tfsmag

    #2
    Re: Adding Button Programatically - Doesn't anyone have an answer???

    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?

    Comment

    • Patrice

      #3
      Re: Adding Button Programatically - Doesn't anyone have an answer???

      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" <gotcha@geshel. org> a écrit dans le message de
      news:OsDRf.1437 74$B94.71430@pd 7tw3no...
      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 PROGRAMMATICALL Y 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="TeamM etz" src="default.vb " language="VB"
      contenttype="te xt/html" responseencodin g="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.Configur ation
      Imports System.Data
      Imports System.Data.Ole Db
      'Imports System.Drawing
      'Imports System.Drawing. Imaging
      'Imports System.IO
      'Imports System.Web.Secu rity
      Imports System.Web.UI
      Imports System.Web.UI.W ebControls
      Imports System.Web.UI.H TMLControls

      Public Class TeamMetz
      Inherits Page
      Dim vbCrLf As String = Microsoft.Visua lBasic.vbCrLf
      Dim strYear As String = DateTime.Now.Ye ar.ToString()
      Dim strWarning As String = vbCrLf & " <p>Have you <a
      href=""/rules.aspx"">re ad 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 (ConfigurationS ettings.AppSett ings("strConn") )

      Protected Sub Page_Load(sende r As Object, e As EventArgs)
      Dim strURL as String = Request.RawURL. ToString()
      Dim strDO as String = Request.QuerySt ring("do")
      Dim intID as Integer = Request.QuerySt ring("id")
      Dim strP as String = Request.QuerySt ring("parent")
      Dim strC as String = Request.QuerySt ring("child")
      Dim strG as String = Request.QuerySt ring("gchild")
      Dim intE as Integer = Request.QuerySt ring("entry")
      Controls.Add(Ne w 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=""generato r"" content=""A
      Text Editor - The only way to code!"" />" & vbCrLf & " <meta
      name=""copyrigh t"" content=""1998 - " & strYear & ", Larry Metz and Team
      Metz Real Estate."" />" & vbCrLf & " <meta
      name=""MSSmartT agsPreventParsi ng"" content=""true" " />" & vbCrLf & "
      <meta http-equiv=""imageto olbar"" 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=""styleshee t"" media=""all""
      href=""/css/global.css"" />" & vbCrLf & " <link type=""text/css""
      rel=""styleshee t"" 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"">Te am 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(Ne w 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(Ne w 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.or g/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=h ttp://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.na me/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(Ne w LiteralControl( " <h2>Edit Intro</h2>" & strWarning))
      myForm.ID = "myForm"
      Controls.Add(my Form)
      Dim myCmd as New OleDbCommand("S ELECT [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(myCm d.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 = "IntroComme nt"
      textarea.Text = myCmd.ExecuteSc alar()
      textarea.Rows = "8"
      textarea.Column s = "80"
      textarea.TextMo de = TextBoxMode.Mul tiLine
      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(sen der As Object, e As System.EventArg s)
      Dim myCmd as New OleDbCommand("U PDATE tblIntro SET
      [Comment]=@Comment", myConn)
      myConn.Open()
      myCmd.CommandTy pe = CommandType.Tex t
      myCmd.Parameter s.Add("@Comment ", OleDbType.LongV arWChar).Value =
      RepChar(Request .Form("IntroCom ment"))
      myCmd.ExecuteNo nQuery()
      myConn.Close()
      LoadIntro()
      End Sub

      Function FormatDate(sIte m as Object) as String
      Return String.Format(" <p class=""small"" >Posted: <i>{0:dddd, dd MMMMM,
      yyyy}</i></p>", sItem)
      End Function
      Function FormatData(sIte m) as String
      FormatData=sIte m.Replace(vbcrl f,"</p>" & vbCrLf & "
      <p>").Replace ("","<b>").Replac e("","</b>").Replace( "","<i>").Re pla
      ce("
      ","</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>
      ","</li>" & vbCrLf & " </ul>" & vbCrLf)
      End Function
      Function RepChar(sItem) as String
      RepChar=sItem.R eplace("'","'") .Replace(" & ", " &amp;
      ").Replace("<", "&lt;").Replace (">","&gt;")
      End Function

      End Class


      Comment

      • tfsmag

        #4
        Re: Adding Button Programatically - Doesn't anyone have an answer???

        nevermind, i see it now... it's the "myform" control. Sorry...

        Comment

        • Cor Ligthert [MVP]

          #5
          Re: Adding Button Programatically - Doesn't anyone have an answer???

          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.EventArg s) Handles MyBase.Load
          Dim mybutton As Button
          Dim i As Integer
          For i = 0 To New Date().DaysInMo nth _
          (New Date().Year, New Date().Month) - 1
          mybutton = New Button
          mybutton.BackCo lor = Drawing.Color.W hite
          mybutton.Text = (i + 1).ToString
          mybutton.Width = New Unit(30)
          Me.Panel1.Contr ols.Add(mybutto n)
          AddHandler mybutton.Click, AddressOf mybutton_Click
          If (i + 1) Mod 5 = 0 Then
          Me.Panel1.Contr ols.Add(New LiteralControl( "<BR>"))
          End If
          Next
          End Sub
          Private Sub mybutton_Click _
          (ByVal sender As Object, ByVal e As System.EventArg s)
          Dim mylabel As New Label
          Me.Panel1.Contr ols.Add(New LiteralControl( "<BR><BR>") )
          Me.Panel1.Contr ols.Add(mylabel )
          mylabel.Text = "The day is: " & DirectCast(send er, Button).Text
          End Sub
          ///

          I will place it tomorrow on our website.


          I hope this helps,

          Cor


          Comment

          • Neo Geshel

            #6
            Re: Adding Button Programatically - Doesn’t anyone have an answer???

            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="TeamM etz" src="test1.vb" language="VB"
            contenttype="te xt/html" responseencodin g="utf-8" %>

            Code-Behind (test1.vb):

            Imports System
            Imports System.Web.UI
            Imports System.Web.UI.W ebControls
            Imports System.Web.UI.H TMLControls

            Public Class TeamMetz
            Inherits Page
            Dim vbCrLf As String = Microsoft.Visua lBasic.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(Ne w 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(Ne w LiteralControl( vbCrLf & " </div>" & vbCrLf & "
            </body>" & vbCrLf & "</html>"))
            End Sub

            Private Sub LoadIntro()
            myForm.ID = "myForm"
            Controls.Add(my Form)
            textarea.id = "IntroComme nt"
            textarea.Rows = "8"
            textarea.Column s = "80"
            textarea.TextMo de = TextBoxMode.Mul tiLine
            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(sen der As Object, e As EventArgs)
            Controls.Add(Ne w LiteralControl( "<p>" &
            Request.Form("I ntroComment") & "</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.
            *************** *************** *************** *************** ***********


            Comment

            • Marina Levit [MVP]

              #7
              Re: Adding Button Programatically - Doesn't anyone have an answer???

              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" <gotcha@geshel. org> wrote in message
              news:umDqoU6RGH A.5808@TK2MSFTN GP12.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="TeamM etz" src="test1.vb" language="VB"
              contenttype="te xt/html" responseencodin g="utf-8" %>

              Code-Behind (test1.vb):

              Imports System
              Imports System.Web.UI
              Imports System.Web.UI.W ebControls
              Imports System.Web.UI.H TMLControls

              Public Class TeamMetz
              Inherits Page
              Dim vbCrLf As String = Microsoft.Visua lBasic.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(Ne w 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(Ne w LiteralControl( vbCrLf & " </div>" & vbCrLf & "
              </body>" & vbCrLf & "</html>"))
              End Sub

              Private Sub LoadIntro()
              myForm.ID = "myForm"
              Controls.Add(my Form)
              textarea.id = "IntroComme nt"
              textarea.Rows = "8"
              textarea.Column s = "80"
              textarea.TextMo de = TextBoxMode.Mul tiLine
              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(sen der As Object, e As EventArgs)
              Controls.Add(Ne w LiteralControl( "<p>" &
              Request.Form("I ntroComment") & "</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 ("creationis m") 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. *
              * *
              * "Intelligen t 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.
              *************** *************** *************** *************** ***********



              Comment

              • Neo Geshel

                #8
                Re: Adding Button Programatically - Doesn't anyone have an answer???

                Marina Levit [MVP] wrote:[color=blue]
                > 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.[/color]

                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.
                *************** *************** *************** *************** ***********

                Comment

                • Neo Geshel

                  #9
                  Re: Adding Button Programatically - Doesn't anyone have an answer???

                  Cor Ligthert [MVP] wrote:[color=blue]
                  > 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.EventArg s) Handles MyBase.Load
                  > Dim mybutton As Button
                  > Dim i As Integer
                  > For i = 0 To New Date().DaysInMo nth _
                  > (New Date().Year, New Date().Month) - 1
                  > mybutton = New Button
                  > mybutton.BackCo lor = Drawing.Color.W hite
                  > mybutton.Text = (i + 1).ToString
                  > mybutton.Width = New Unit(30)
                  > Me.Panel1.Contr ols.Add(mybutto n)
                  > AddHandler mybutton.Click, AddressOf mybutton_Click
                  > If (i + 1) Mod 5 = 0 Then
                  > Me.Panel1.Contr ols.Add(New LiteralControl( "<BR>"))
                  > End If
                  > Next
                  > End Sub
                  > Private Sub mybutton_Click _
                  > (ByVal sender As Object, ByVal e As System.EventArg s)
                  > Dim mylabel As New Label
                  > Me.Panel1.Contr ols.Add(New LiteralControl( "<BR><BR>") )
                  > Me.Panel1.Contr ols.Add(mylabel )
                  > mylabel.Text = "The day is: " & DirectCast(send er, Button).Text
                  > End Sub
                  > ///
                  >
                  > I will place it tomorrow on our website.
                  >
                  >
                  > I hope this helps,
                  >
                  > Cor
                  >
                  > [/color]

                  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.
                  *************** *************** *************** *************** ***********

                  Comment

                  • Marina Levit [MVP]

                    #10
                    Re: Adding Button Programatically - Doesn't anyone have an answer???

                    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" <gotcha@geshel. org> wrote in message
                    news:OMiYPh6RGH A.440@TK2MSFTNG P10.phx.gbl...
                    Marina Levit [MVP] wrote:[color=blue]
                    > 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.[/color]

                    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 ("creationis m") 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. *
                    * *
                    * "Intelligen t 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.
                    *************** *************** *************** *************** ***********


                    Comment

                    Working...