Command event for dynamically created linkbuttons not firing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Hamayun Khan
    New Member
    • Aug 2007
    • 106

    Command event for dynamically created linkbuttons not firing

    Hi I have created linkbuttons dynamically using the below code




    Code:
    Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32)
    
    Dim i = 0
    
    Dim panel1 As Panel = New Panel
    
    panel1.CssClass = "boxx"
    
    pane.Controls.Add(panel1)
    
    Dim lbButton As LinkButton = New LinkButton
    
    lbButton.Text = commandtext
    
    lbButton.CommandName = commandtext
    
    lbButton.CommandArgument = Cmdarg
    
    AddHandler lbButton.Command, AddressOf Me.LinkButton_Command
    
    panel1.Controls.Add(lbButton)
    
    End Sub
    
    
    
    Protected Sub LinkButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
    
    If e.CommandArgument.ToString = "Region" Then
    
    Session("Region") = e.CommandName
    
    End If
    
    If e.CommandArgument.ToString = "LEA" Then
    
    Session("LEA") = e.CommandName
    
    End If
    
    If e.CommandArgument.ToString = "Subject" Then
    
    Session("Subject") = e.CommandName
    
    End If
    
    If e.CommandArgument.ToString = "Position" Then
    
    Session("Position") = e.CommandName
    
    End If
    
    If e.CommandArgument.ToString = "SchoolPhase" Then
    
    Session("SchoolPhase") = e.CommandName
    
    End If
    
    Response.Write("<script language=""javascript"">alert(""" & Session("Region") & """)</script>")
    
    populate()
    
    End Sub





    Linkbuttons are successfully generated. When i click one of the linkbutton. The command event doesn't fired. While I have Add command event handler to the linkbuttons.

    What is wrong in my code

    Thanks
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Where are you calling your "createlinkbutt on" method?
    In the Page Init event?

    If not then your event will never fire. The link button will post back to the server, but the server will not load the ViewState of the link button, and so the event for the link button will not be loaded...theref ore it will not fire.

    -Frinny

    Comment

    • Hamayun Khan
      New Member
      • Aug 2007
      • 106

      #3
      Originally posted by Frinavale
      Where are you calling your "createlinkbutt on" method?
      In the Page Init event?

      If not then your event will never fire. The link button will post back to the server, but the server will not load the ViewState of the link button, and so the event for the link button will not be loaded...theref ore it will not fire.

      -Frinny
      Thanks for your reply.

      But The problem is very strange. Please look into code problem once again.

      Function to generate linkbuttons.
      Code:
      Sub createlinkbutton(ByVal commandtext As String, ByVal Cmdarg As String, ByVal pane As Panel, ByVal count As Int32)
              Dim panela As Panel = New Panel
              panela.CssClass = "boxx"
              pane.Controls.Add(panela)
              Dim lbButton As LinkButton = New LinkButton
              lbButton.Text = commandtext
              lbButton.CommandName = commandtext
              lbButton.CommandArgument = Cmdarg
              AddHandler lbButton.Command, AddressOf Me.LinkButton_Command
              AddHandler lbButton.Click, AddressOf Me.LinkButton_Click
              panela.Controls.Add(lbButton)
          End Sub
      And the command event is
      Code:
       Protected Sub LinkButton_Command(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
              If e.CommandArgument.ToString = "Region" Then
                  Session("Region") = e.CommandName
              End If
              If e.CommandArgument.ToString = "LEA" Then
                  Session("LEA") = e.CommandName
              End If
              If e.CommandArgument.ToString = "Subject" Then
                  Session("Subject") = e.CommandName
              End If
              If e.CommandArgument.ToString = "Position" Then
                  Session("Position") = e.CommandName
              End If
              If e.CommandArgument.ToString = "SchoolPhase" Then
                  Session("SchoolPhase") = e.CommandName
              End If
              Response.Write("<script language=""javascript"">alert(""" & Session("Region") & """)</script>")
              populate()
              Response.Write("<script language='javascript'>alert('hello')</script>")
          End Sub
      Another funciton for some other functionality
      Code:
      Public Sub loadRegion()
      For k = 0 To DT.Rows.Count
            createlinkbutton(DT.Rows(k)("Region"), DT.Rows(k)("BtnArg"), Pane1, DT.Rows(k)("num"))
      Next
      
              'createlinkbutton("London", "Region", Pane, 12)
              'createlinkbutton("aLondon", "Region", Pane, 12)
              'createlinkbutton("Lsondon", "Region", Pane, 12)
              'createlinkbutton("Lodndon", "Region", Pane, 12)
              'createlinkbutton("Londdon", "Region", Pane, 12)
              'createlinkbutton("Londfon", "Region", Pane, 12)
              'createlinkbutton("Londofn", "Region", Pane, 12)
              'createlinkbutton("fLondon", "Region", Pane, 12)
              'createlinkbutton("Lcondon", "Region", Pane, 12)
              'createlinkbutton("Lovndon", "Region", Pane, 12)
              'createlinkbutton("Lon don", "Region", Pane, 12)
              'createlinkbutton("Loncdon", "Region", Pane1, 12)
              'createlinkbutton("Loncdon", "Region", Pane1, 12)
              'createlinkbutton("Londxon", "Region", Pane1, 12)
              'createlinkbutton("Londosn", "Region", Pane1, 12)
              'createlinkbutton("Londons", "Region", Pane1, 12)
              'createlinkbutton("kLondon", "Region", Pane1, 12)
              'createlinkbutton("Ljondon", "Region", Pane, 12)
              'createlinkbutton("Lonndon", "Region", Pane, 12)
              'createlinkbutton("Lonndon", "Region", Pane, 12)
              'createlinkbutton("Londnon", "Region", Pane1, 12)
              'createlinkbutton("Londonn", "Region", Pane1, 12)
      End Sub
      Now if I call the LoadRegion function from any where the button generated don't fires the command event. If i comment the For loop portion and uncomment the
      commented portion then the button generated works fine. i.e when i click one of the button command event fires successfully.

      Instead of using if i use while loop buttons are created successfully but event doesn't work.

      This is very urgent.
      Waiting for quick reply.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Is DT.Rows.Count = 0?
        Does your loop actually happen?
        Have you tried stepping through this?

        You never answered my original question.
        Are you calling this in your Page Init event?

        Comment

        • Hamayun Khan
          New Member
          • Aug 2007
          • 106

          #5
          Originally posted by Frinavale
          Is DT.Rows.Count = 0?
          Does your loop actually happen?
          Have you tried stepping through this?

          You never answered my original question.
          Are you calling this in your Page Init event?
          NO DT.Rows.Count is not equals 0 but the loop happen successfully.

          And I m calling this from page load event.

          Thanks

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Move it to the Page Init event.
            For more information on why please see this article on how to use dynamic controls in asp.net.

            Comment

            • Hamayun Khan
              New Member
              • Aug 2007
              • 106

              #7
              Originally posted by Frinavale
              Move it to the Page Init event.
              For more information on why please see this article on how to use dynamic controls in asp.net.

              Thanks

              If from the page load event I call to button create function like below then the command event works fine.
              Code:
              createlinkbutton("Londosn", "Region", Pane1, 12)
              but if i place the same code in for or while loop then the command event doesn't work.

              e.g
              Code:
              for i=1 to 5
              createlinkbutton("Londosn", "Region", Pane1, 12) 
              Next
              Here the command event does not work.

              Comment

              • Frinavale
                Recognized Expert Expert
                • Oct 2006
                • 9749

                #8
                Well,one obvious thing is that you are creating a button with the command name and text "Londosn" in Panel 1, five times.

                Code:
                for i=1 to 5
                createlinkbutton("Londosn", "Region", Pane1, 12) 
                Next

                Have you considered providing unique values (like you do by hand when you aren't looping) to see if it works?

                For example:
                Code:
                for i=1 to 5
                createlinkbutton("Londosn_"+i.ToString, "Region", Pane1, 12) 
                Next

                Comment

                • Hamayun Khan
                  New Member
                  • Aug 2007
                  • 106

                  #9
                  Originally posted by Frinavale
                  Well,one obvious thing is that you are creating a button with the command name and text "Londosn" in Panel 1, five times.

                  Code:
                  for i=1 to 5
                  createlinkbutton("Londosn", "Region", Pane1, 12) 
                  Next

                  Have you considered providing unique values (like you do by hand when you aren't looping) to see if it works?

                  For example:
                  Code:
                  for i=1 to 5
                  createlinkbutton("Londosn_"+i.ToString, "Region", Pane1, 12) 
                  Next

                  Thanks for Reply. I have done the job little different.
                  Code:
                  <asp:DataList ID="DataList2" runat="server" DataSourceID="SqlDataSource1" RepeatDirection="horizontal"
                                      RepeatLayout="Flow">
                                      <ItemTemplate>
                                          <asp:LinkButton OnCommand="Command"  CommandName="Region" CommandArgument='<%# Eval("Region") %>' runat="server"><%# Eval("Region") %></asp:LinkButton>                        
                                          |
                                      </ItemTemplate>
                                  </asp:DataList>
                                  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbase_DataConnectionString6 %>"
                                      SelectCommand="SELECT  [Region] FROM [tblregion]"></asp:SqlDataSource>
                  It is now working fine. thanks.

                  Comment

                  Working...