Handle Event of button at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ria12
    New Member
    • Feb 2009
    • 26

    Handle Event of button at runtime

    Hello.....I want to generate a button at runtime..and set the style like Top,Left,Right, Height,Width of a button also have to handle click event and when button is clicked it should redirect to another page.Plz help me.
  • kunal pawar
    Contributor
    • Oct 2007
    • 297

    #2
    which type of button you want to generate at runtime, "server" side or HTML control.

    If you want to generate html button then using
    var btn = document.create Element("input" );
    btn.setAttribut e("type","Butto n");
    btn.className ="css class name";

    For server control

    Button btn = new Button();

    Comment

    • Ria12
      New Member
      • Feb 2009
      • 26

      #3
      hi....I want to generate server button controls in Vb.net and also want to handle the click Event of that button.any help is appricated....

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Please take a look at the article about how to use dynamic controls in ASP.NET.


        You need to declare your Button at the page level (so that it is accessible to all of the page's methods). You need to declare this Button "WithEvents " in VB.NET

        You need to instantiate the Button in the Page Init event (otherwise you will not be able to handle the click event).


        For example:
        Code:
        Private WithEvents myButton As Button
        
        Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            myButton = new Button
            myButton.Height = 60
            myButton.Width = 100
            myButton.Style.Add("color","blue")
        End Sub
        
        Private Sub myButton _Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles myButton .Click
        
        End Sub

        -Frinny

        Comment

        • Ria12
          New Member
          • Feb 2009
          • 26

          #5
          I used your code...but actually i want generate button on click of other button....

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Well what do you have so far?
            The example in the article explains how to show more TextBoxes depending on a DropDownList. This same concept would be applied to your button click...

            Comment

            • Ria12
              New Member
              • Feb 2009
              • 26

              #7
              hi.......I 'm able to generate buttons on click of another botton in Vb.net but not able to handle button click event that is generated at runtime...

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                You have most likely instantiated the dynamic button during the OnClick event of the non-dynamic button. In this case the OnClick event for they dynamic button will not fire.

                The reason is pretty simple, ASP.NET uses the ViewState to determine what event(s) caused the page to post back to the server (and to determine what state your Objects are in...like selected indexes of DropDownList etc). If your Object does not exist (is not instantiated) at the time the ViewState is loaded, then your event will not be registered and all the state information for that Object will be lost.

                Therefore you need to instantiate your button during the Page Init event.

                So, you need to be able to indicate when to create the button during the Page Init event. In order to do this you need to store a Boolean value somewhere that is available during the Page Init event. Because the Page Init event occurs before the ViewState is loaded you are going to have to use something that is available through the Request Object...

                You could use a hidden field, or a cookie, or the URL...

                Comment

                • Ria12
                  New Member
                  • Feb 2009
                  • 26

                  #9
                  I understand your point with that i solve my problem of Click Event of runtime generated button not fired ....now i want to retrieve the height of rutime generated button pass to another page but it create an error

                  can not implicitly convert type unit to integer

                  my code is
                  Code:
                     Select Case hidden
                  
                                      Case "Top"
                                          id= 1
                                          For i As Integer = 0 To c - 1
                  
                                              Dim btnTop As Button = New Button()
                                              btnTop.ID = id
                                              btnTop.Text = "Top"
                                              'btnTop.Style("Top") = "200px"
                                              btnTop.CssClass = "btnTop"
                                              'btnTop.Style("Left") = "5px"
                                              btnTop.Style("Height") = h & "px"
                                              btnTop.Style("Width") = w & "px"
                                              btnTop.Style("Border") = "solid"
                                              Panel1.Controls.Add(btnTop)
                                              AddHandler btnTop.Click, AddressOf btnTop_Click
                                              id= id + 1
                  
                  
                                          Next i
                        End Select
                  Event handler of this button is
                  Code:
                   Private Sub btnTop_Click(ByVal sender As Object, ByVal e As System.EventArgs)
                          l = Integer.Parse(DirectCast(sender, Button).ID)
                          text= CType(sender, Button).Text
                          height=CType(sender,Button).Height
                          Session("ID") = l
                          Session("loc") = text
                          Session("height1")=height
                          Response.Redirect("Default2.aspx")
                  Plz help me, I urgently need help....
                  Last edited by Frinavale; Apr 17 '09, 01:23 PM. Reason: Added code tags. Please post code snippets in [code] [/code] tags.

                  Comment

                  Working...