Adding button programatically - NON-FUNCTIONAL!!

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

    #1

    Adding button programatically - NON-FUNCTIONAL!!

    Greetings. Hopefully someone will be able to untie this Gordian Knot I
    have found myself bound up in.

    I am trying to add a submit button dynamically to a PlaceHolder. This
    button will help update a particular entry in a database.

    The button is added as thus:

    Sub LoadIntro()
    ...additional content that is dynamically loaded as well: a preview
    of the DB contents followed by a form that is pre-filled from the DB...
    Dim submit as Button = New Button()
    AddHandler submit.Click, AddressOf UpdateIntro
    submit.id = "submit"
    submit.Text = "Update Intro"
    content.Control s.Add(submit)
    End Sub

    And the event handler is set up like this:

    Sub UpdateIntro(sen der As Object, e As EventArgs)
    Dim myConn as New
    OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
    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


    The problem is that this doesn't work!!! The UpdateIntro sub simply
    doesn't get fired!!

    I'm pulling my hair out here, as I have also removed the AddHandler to
    try this:

    Private Sub UpdateIntro(ByV al sender As Object, ByVal e As EventArgs)
    Handles submit.Click

    But it also throws an error! I know that adding
    submit.onClick= "UpdateIntr o" will also throw an error, but I simply
    don't know what else to do or try!

    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.
    *************** *************** *************** *************** ***********
  • Mark Rae

    #2
    Re: Adding button programatically - NON-FUNCTIONAL!!

    "Neo Geshel" <gotcha@geshel. org> wrote in message
    news:IuzPf.1228 99$sa3.82272@pd 7tw1no...
    [color=blue]
    >but I simply don't know what else to do or try![/color]

    Save yourself a whole load of heartache and don't even try to add the button
    dynamically - just set its Visible property to true or false as required...


    Comment

    • Neo Geshel

      #3
      Re: Adding button programatically - NON-FUNCTIONAL!!

      Mark Rae wrote:[color=blue]
      > "Neo Geshel" <gotcha@geshel. org> wrote in message
      > news:IuzPf.1228 99$sa3.82272@pd 7tw1no...
      > [color=green]
      >> but I simply don't know what else to do or try![/color]
      >
      > Save yourself a whole load of heartache and don't even try to add the button
      > dynamically - just set its Visible property to true or false as required...
      > [/color]

      Not so easy. You see, I am trying to make an admin interface more
      efficient and flexible by programatically adding content as required. If
      I can solve this one single problem, I can conceivably reduce the file
      size (or, in other words, the amount of code) of the admin interface by
      as much as 30%.

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

      • Newbie

        #4
        Re: Adding button programatically - NON-FUNCTIONAL!!

        I tried this out, and it works OK

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
        System.EventArg s) Handles MyBase.Load
        'Put user code to initialize the page here
        LoadIntro()
        End Sub
        Sub LoadIntro()

        Dim submit As Button = New Button
        AddHandler submit.Click, AddressOf UpdateIntro
        submit.id = "submit"
        submit.Text = "Update Intro"
        content.Control s.Add(submit)
        End Sub

        Sub UpdateIntro(ByV al sender As Object, ByVal e As EventArgs)

        Response.Write( "FIRED")

        End Sub


        "Neo Geshel" <gotcha@geshel. org> wrote in message
        news:IuzPf.1228 99$sa3.82272@pd 7tw1no...
        Greetings. Hopefully someone will be able to untie this Gordian Knot I
        have found myself bound up in.

        I am trying to add a submit button dynamically to a PlaceHolder. This
        button will help update a particular entry in a database.

        The button is added as thus:

        Sub LoadIntro()
        ...additional content that is dynamically loaded as well: a preview
        of the DB contents followed by a form that is pre-filled from the DB...
        Dim submit as Button = New Button()
        AddHandler submit.Click, AddressOf UpdateIntro
        submit.id = "submit"
        submit.Text = "Update Intro"
        content.Control s.Add(submit)
        End Sub

        And the event handler is set up like this:

        Sub UpdateIntro(sen der As Object, e As EventArgs)
        Dim myConn as New
        OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
        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


        The problem is that this doesn't work!!! The UpdateIntro sub simply
        doesn't get fired!!

        I'm pulling my hair out here, as I have also removed the AddHandler to
        try this:

        Private Sub UpdateIntro(ByV al sender As Object, ByVal e As EventArgs)
        Handles submit.Click

        But it also throws an error! I know that adding
        submit.onClick= "UpdateIntr o" will also throw an error, but I simply
        don't know what else to do or try!

        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

        • Eliyahu Goldin

          #5
          Re: Adding button programatically - NON-FUNCTIONAL!!

          Are you trying to click the button on the first page load or after a
          postback? You have to re-create all dynamically added controls on every
          postback.

          Eliyahu

          "Neo Geshel" <gotcha@geshel. org> wrote in message
          news:IuzPf.1228 99$sa3.82272@pd 7tw1no...
          Greetings. Hopefully someone will be able to untie this Gordian Knot I
          have found myself bound up in.

          I am trying to add a submit button dynamically to a PlaceHolder. This
          button will help update a particular entry in a database.

          The button is added as thus:

          Sub LoadIntro()
          ...additional content that is dynamically loaded as well: a preview
          of the DB contents followed by a form that is pre-filled from the DB...
          Dim submit as Button = New Button()
          AddHandler submit.Click, AddressOf UpdateIntro
          submit.id = "submit"
          submit.Text = "Update Intro"
          content.Control s.Add(submit)
          End Sub

          And the event handler is set up like this:

          Sub UpdateIntro(sen der As Object, e As EventArgs)
          Dim myConn as New
          OleDbConnection (ConfigurationS ettings.AppSett ings("strConn") )
          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


          The problem is that this doesn't work!!! The UpdateIntro sub simply
          doesn't get fired!!

          I'm pulling my hair out here, as I have also removed the AddHandler to
          try this:

          Private Sub UpdateIntro(ByV al sender As Object, ByVal e As EventArgs)
          Handles submit.Click

          But it also throws an error! I know that adding
          submit.onClick= "UpdateIntr o" will also throw an error, but I simply
          don't know what else to do or try!

          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

            #6
            Re: Adding button programatically - NON-FUNCTIONAL!!

            Newbie wrote:[color=blue]
            > I tried this out, and it works OK
            >
            > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
            > System.EventArg s) Handles MyBase.Load
            > 'Put user code to initialize the page here
            > LoadIntro()
            > End Sub
            > Sub LoadIntro()
            >
            > Dim submit As Button = New Button
            > AddHandler submit.Click, AddressOf UpdateIntro
            > submit.id = "submit"
            > submit.Text = "Update Intro"
            > content.Control s.Add(submit)
            > End Sub
            >
            > Sub UpdateIntro(ByV al sender As Object, ByVal e As EventArgs)
            >
            > Response.Write( "FIRED")
            >
            > End Sub[/color]

            When I tried your method, I got the contents of LoadIntro() appearing
            twice on the web page. When I clicked submit, I get a blank page.

            What gives?

            ....Geshel

            Comment

            • Neo Geshel

              #7
              Re: Adding button programatically - NON-FUNCTIONAL!!

              Neo Geshel wrote:[color=blue]
              > Newbie wrote:[color=green]
              >> I tried this out, and it works OK
              >>
              >> Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
              >> System.EventArg s) Handles MyBase.Load
              >> 'Put user code to initialize the page here
              >> LoadIntro()
              >> End Sub
              >> Sub LoadIntro()
              >>
              >> Dim submit As Button = New Button
              >> AddHandler submit.Click, AddressOf UpdateIntro
              >> submit.id = "submit"
              >> submit.Text = "Update Intro"
              >> content.Control s.Add(submit)
              >> End Sub
              >>
              >> Sub UpdateIntro(ByV al sender As Object, ByVal e As EventArgs)
              >>
              >> Response.Write( "FIRED")
              >>
              >> End Sub[/color]
              >
              > When I tried your method, I got the contents of LoadIntro() appearing
              > twice on the web page. When I clicked submit, I get a blank page.
              >
              > What gives?
              >
              > ...Geshel[/color]

              And when I replace your "private sub" with a normal one, LoadIntro()
              appears only once on the page. But submitting the form still doesn't
              update the DB, and gives me a blank page in response.

              Stranger and stranger!

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

                #8
                Re: Adding button programatically - NON-FUNCTIONAL!!

                Eliyahu Goldin wrote:[color=blue]
                > Are you trying to click the button on the first page load or after a
                > postback? You have to re-create all dynamically added controls on every
                > postback.
                >
                > Eliyahu
                > [/color]
                The button is meant to be clicked on the first page load (technically).
                Any button clicks should fire the UpdateIntro(), at the end of which the
                LoadIntro() is called, sending the user the original form again, but
                this time populated with updated material.

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

                • Michael Hamrah

                  #9
                  Re: Adding button programatically - NON-FUNCTIONAL!!

                  Sorry to hear my approach didn't work well for you. If controls are
                  being loaded twice, then that means you're calling the function twice-
                  could this be possible? THis can happen when you add a control in the
                  Init event, then add the control again after the page is loaded. Does
                  it occur when the page is first loaded, or only after a postback?
                  Secondly, as for the page not showing up, it sounds like the postback
                  isn't being handled correctly. THis can be related to your first
                  problem.

                  I wrote some code that adds a button to the page dynamically, stores a
                  value as a hidden field, then loads the button in the init event.
                  You'll need to wire up the Page_Init event yourself via Page.Init +=
                  new EventHandler(Pa ge_Init); ANyway, you should be able to get it to
                  work and expand on the basic idea. You can replace the button with a
                  more complex usercontrol, or store a series of controls in the hidden
                  field if you want to add more than one:

                  private Button btn;
                  protected void Page_Init(objec t sender, EventArgs e)
                  {
                  string controlName;

                  controlName = Request.Form["ControlToA dd"];
                  if (controlName == "btn")
                  {
                  btn = new Button();
                  btn.ID = "btn";
                  btn.Click += new EventHandler(bt n_Click);
                  this.form1.Cont rols.Add(btn);
                  }

                  }

                  void btn_Click(objec t sender, EventArgs e)
                  {
                  string s;
                  s = "The button has been clicked";
                  }

                  protected void Page_Load(objec t sender, EventArgs e)
                  {
                  if (Page.IsPostBac k == false)
                  {
                  btn = new Button();
                  btn.ID = "btn";
                  this.form1.Cont rols.Add(btn);
                  //no need to wire up an event handler here.
                  this.RegisterHi ddenField("Cont rolToAdd", "btn");
                  }
                  }

                  Good luck!

                  Comment

                  Working...