Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Runtime Creation Of Button+Adding Events+Sourabh

Runtime Creation Of Button+Adding Events+Sourabh

Scheduled Pinned Locked Moved ASP.NET
graphicsquestion
15 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P pmarfleet

    Dot Net Jantu wrote:

    How do we do that? Like this? AddHandler btnSave.Click, btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) this code give me error as "expression expected" by underlining the ByVal And where do we do that?

    Did you read the article? The sample code shows you how to dynamically assign an event handler to an event. You're code doesn't look anything like that in the article so it's not surprising that it doesn't work. I suggest that you implement your code in line with the article instead of just making up something that isn't even syntactically correct.

    Dot Net Jantu wrote:

    Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSave.Click Response.Write("clicked") End Sub

    The Handles keyword is unnecessary if the event handler is only being used for controls that are created dynamically. However, if you have a control called btnSave defined in your ASPX page then you would want to use it.

    Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

    D Offline
    D Offline
    Dot Net Jantu
    wrote on last edited by
    #5

    Thanks, its working now.. I have one more problem, in the page load i have written this Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname) but in the event: Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) ' when i type "txtn" and try for intelisence, it dosent show the control. End Sub Regards

    Thanks and Regards,

    P 1 Reply Last reply
    0
    • D Dot Net Jantu

      Thanks, its working now.. I have one more problem, in the page load i have written this Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname) but in the event: Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) ' when i type "txtn" and try for intelisence, it dosent show the control. End Sub Regards

      Thanks and Regards,

      P Offline
      P Offline
      pmarfleet
      wrote on last edited by
      #6

      The variable txtname has been defined locally in another procedure. It doesn't have scope in your event handler procedure so it won't appear in intellisense. To get a reference to the TextBox control that has raised the event, you need to call FindControl on the control that contains the TextBoxes. You should define some naming convention for each TextBox you dynamically create so that you have a means of obtaining a reference to each one by name.

      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

      D 1 Reply Last reply
      0
      • P pmarfleet

        The variable txtname has been defined locally in another procedure. It doesn't have scope in your event handler procedure so it won't appear in intellisense. To get a reference to the TextBox control that has raised the event, you need to call FindControl on the control that contains the TextBoxes. You should define some naming convention for each TextBox you dynamically create so that you have a means of obtaining a reference to each one by name.

        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

        D Offline
        D Offline
        Dot Net Jantu
        wrote on last edited by
        #7

        I am doing it like this: but its wrong. I didt understand what you wanted to say, please correct me. dssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.

        Thanks and Regards,

        P 1 Reply Last reply
        0
        • D Dot Net Jantu

          I am doing it like this: but its wrong. I didt understand what you wanted to say, please correct me. dssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.

          Thanks and Regards,

          P Offline
          P Offline
          pmarfleet
          wrote on last edited by
          #8

          Dot Net Jantu wrote:

          ssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.

          Yes, it will. To know why, you need to read the MSDN documentation to understand how the FindControl method works. Have you done this?

          Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

          D 1 Reply Last reply
          0
          • P pmarfleet

            Dot Net Jantu wrote:

            ssave.Tables("dtUsers").Rows(0)("UserName") = nameHolder.FindControl(txtname.Text.Trim) it gives an error. Object reference not set to an instance of an object.

            Yes, it will. To know why, you need to read the MSDN documentation to understand how the FindControl method works. Have you done this?

            Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

            D Offline
            D Offline
            Dot Net Jantu
            wrote on last edited by
            #9

            as per the msdn documentation, I have done this.. Dim name As TextBox = nameHolder.FindControl("txtname") also I had done this If (Not name Is Nothing) Then ' Get name's parent. Dim myControl2 As Control = name.Parent Response.Write("Parent of the text box is : " & myControl2.ID) Else Response.Write("Control not found.....") End If and its showing as control not found..

            Thanks and Regards,

            P 1 Reply Last reply
            0
            • D Dot Net Jantu

              as per the msdn documentation, I have done this.. Dim name As TextBox = nameHolder.FindControl("txtname") also I had done this If (Not name Is Nothing) Then ' Get name's parent. Dim myControl2 As Control = name.Parent Response.Write("Parent of the text box is : " & myControl2.ID) Else Response.Write("Control not found.....") End If and its showing as control not found..

              Thanks and Regards,

              P Offline
              P Offline
              pmarfleet
              wrote on last edited by
              #10

              Dot Net Jantu wrote:

              and its showing as control not found..

              Have you actually added a control to the Controls collection of nameHolder with the ID 'txtname'?

              Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

              D 1 Reply Last reply
              0
              • P pmarfleet

                Dot Net Jantu wrote:

                and its showing as control not found..

                Have you actually added a control to the Controls collection of nameHolder with the ID 'txtname'?

                Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                D Offline
                D Offline
                Dot Net Jantu
                wrote on last edited by
                #11

                if this is what you mean then yes. Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)

                Thanks and Regards,

                P 1 Reply Last reply
                0
                • D Dot Net Jantu

                  if this is what you mean then yes. Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)

                  Thanks and Regards,

                  P Offline
                  P Offline
                  pmarfleet
                  wrote on last edited by
                  #12

                  Dot Net Jantu wrote:

                  Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)

                  The description of the Control.FindControl which you said you had read, is as follows: Searches the current naming container for a server control with the specified id parameter. You haven't set the ID property for your control. The fact that you have declared a variable called txtname and set it with a reference to your dynamically created TextBox object is not relevent.

                  Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                  D 1 Reply Last reply
                  0
                  • D Dot Net Jantu

                    Hi All, I have created a button at runtime on form load: Dim btnSave As New Button btnSave.BackColor = Drawing.Color.Maroon btnSave.ForeColor = Drawing.Color.White btnSave.Text = "Save" ButtonHolder.Controls.Add(btnSave) I Have also added a code to it as : Public Sub btnhandler_click(ByVal sender As Object, ByVal e As EventArgs) Handles btnhandler.Click Response.Write("clicked") End Sub but this second event code does not execute when i click on the button. Reason???

                    Thanks and Regards,

                    C Offline
                    C Offline
                    Christian Graus
                    wrote on last edited by
                    #13

                    Apart from what you've already been told, the button needs to be created in the LoadViewState function so that it exists early enough in the page lifecycle for the viewstate to be restored and the event to fire.

                    Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )

                    1 Reply Last reply
                    0
                    • P pmarfleet

                      Dot Net Jantu wrote:

                      Dim txtname As TextBox txtname = New TextBox txtname.AutoPostBack = False txtname.BackColor = Drawing.Color.AliceBlue txtname.BorderColor = Drawing.Color.Black nameHolder.Controls.Add(txtname)

                      The description of the Control.FindControl which you said you had read, is as follows: Searches the current naming container for a server control with the specified id parameter. You haven't set the ID property for your control. The fact that you have declared a variable called txtname and set it with a reference to your dynamically created TextBox object is not relevent.

                      Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                      D Offline
                      D Offline
                      Dot Net Jantu
                      wrote on last edited by
                      #14

                      hey thanks it worked. I would like to know that do i need to assign a id for the button also? And one more thing will i have to use that find control method in each and every procedure to retrieve the vbalue of the control?

                      Thanks and Regards,

                      P 1 Reply Last reply
                      0
                      • D Dot Net Jantu

                        hey thanks it worked. I would like to know that do i need to assign a id for the button also? And one more thing will i have to use that find control method in each and every procedure to retrieve the vbalue of the control?

                        Thanks and Regards,

                        P Offline
                        P Offline
                        pmarfleet
                        wrote on last edited by
                        #15

                        Dot Net Jantu wrote:

                        I would like to know that do i need to assign a id for the button also?

                        You probably should do. You will need to if you want to locate instances of the Button control calling FindControl on the container object.

                        Dot Net Jantu wrote:

                        And one more thing will i have to use that find control method in each and every procedure to retrieve the value of the control?

                        Yes, if you don't already have a reference to one of the controls you will need to call the FindControl method on the container object, passing in its ID to find it.

                        Paul Marfleet "No, his mind is not for rent To any God or government" Tom Sawyer - Rush

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups