Asp.Net how to create seperate event handler for programmatically generated controls

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samsol
    New Member
    • May 2014
    • 1

    Asp.Net how to create seperate event handler for programmatically generated controls

    In my asp.net web application, i have created controls programmaticall y, when i try to get the Id assigned to the current lblId control it returns the last id assigned to lblid control.

    The code is given below.

    Code:
    Private Sub LoadProducts(ByVal grpId As Integer)
                        Dim img1 As Integer = 50
                Dim PropertyFromTop As Integer = 130
                Dim PriceFromTop As Integer = 150
                Dim DescFromTop As Integer = 170
                Dim QuantityFromTop As Integer = 190
                Dim linkfromtop As Integer = 210
                Dim IdfromTop As Integer = 210
                Dim imgCount = (From s In db.TblPosts Where s.TblSubCategory2.SubCategory2Id = grpId Select s)
                For Each s In imgCount
                    Dim imge1 As Image = New Image()
                    Dim l1 As LinkButton = New LinkButton()
                    Dim lblProperty As Label = New Label
                    Dim lblPrice As Label = New Label
                    Dim lblDesc As Label = New Label
                    Dim lblQuantity As Label = New Label
                    lblId = New Label
                    l1.ID = "lnk"
                    l1.Style("Position") = "Absolute"
                    l1.Style("Top") = linkfromtop.ToString() & "px"
                    l1.Style("Left") = "200px"
                    l1.ForeColor = Drawing.Color.Blue
                    imge1.Style("Position") = "Absolute"
                    imge1.Style("Top") = img1.ToString() & "px"
                    imge1.Style("Left") = "10px"
                    imge1.Width = 180
                    imge1.Height = 200
                    lblProperty.Style("Position") = "Absolute"
                    lblProperty.Style("Top") = PropertyFromTop.ToString() & "px"
                    lblProperty.Style("Left") = "200px"
                    lblProperty.ForeColor = Drawing.Color.Black
                    lblPrice.Style("Position") = "Absolute"
                    lblPrice.Style("Top") = PriceFromTop.ToString() & "px"
                    lblPrice.Style("Left") = "200px"
                    lblPrice.ForeColor = Drawing.Color.Black
                    lblDesc.Style("Position") = "Absolute"
                    lblDesc.Style("Top") = DescFromTop.ToString() & "px"
                    lblDesc.Style("Left") = "200px"
                    lblDesc.ForeColor = Drawing.Color.Black
                    lblQuantity.Style("Position") = "Absolute"
                    lblQuantity.Style("Top") = QuantityFromTop.ToString() & "px"
                    lblQuantity.Style("Left") = "200px"
                    lblQuantity.ForeColor = Drawing.Color.Black
                    lblId.Style("Position") = "Absolute"
                    lblId.Style("Top") = IdfromTop.ToString() & "px"
                    lblId.Style("Left") = "280px"
                    lblId.ForeColor = Drawing.Color.Black
                    l1.Text = "Add to Cart"
                    lblId.Text = s.PostId
                    lblProperty.Text = "Name:  " & s.PropertyName
                    lblPrice.Text = "Price:  " & s.Price
                    lblDesc.Text = "Description:  " & s.Description
                    lblQuantity.Text = "Quantity:  " & s.Quantity
                    imge1.ImageUrl = "~/ImageHandler.ashx?Id=" + Convert.ToString(s.PostId)
    
                    AddHandler l1.Click, AddressOf Me.LinkButon_Click
                    Pnlprofile.Controls.Add(lblId)
                    Pnlprofile.Controls.Add(l1)
                    Pnlprofile.Controls.Add(imge1)
                    Pnlprofile.Controls.Add(lblProperty)
                    Pnlprofile.Controls.Add(lblPrice)
                    Pnlprofile.Controls.Add(lblDesc)
                    Pnlprofile.Controls.Add(lblQuantity)
                    img1 = Convert.ToInt32(img1)
                    img1 = img1 + 220
                    IdfromTop = Convert.ToInt32(IdfromTop)
                    IdfromTop = IdfromTop + 220
                    linkfromtop = Convert.ToInt32(linkfromtop)
                    linkfromtop = linkfromtop + 220
                    PropertyFromTop = Convert.ToInt32(PropertyFromTop)
                    PropertyFromTop = PropertyFromTop + 220
                    PriceFromTop = Convert.ToInt32(PriceFromTop)
                    PriceFromTop = PriceFromTop + 220
                    DescFromTop = Convert.ToInt32(DescFromTop)
                    DescFromTop = DescFromTop + 220
                    QuantityFromTop = Convert.ToInt32(QuantityFromTop)
                    QuantityFromTop = QuantityFromTop + 220
                Next
        
        End Sub
    
     Private Sub LinkButon_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            MsgBox(lblId.Text)
        End Sub
    Last edited by Rabbit; May 24 '14, 05:44 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
  • iam_clint
    Recognized Expert Top Contributor
    • Jul 2006
    • 1207

    #2
    i'm probably a little late here but sender is what you are looking for
    Code:
      Private Sub LinkButon_Click(ByVal sender As LinkButton, ByVal e As System.EventArgs)
            MsgBox(sender.Text)
        End Sub

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Dynamically added controls are a bit tricky to do in ASP.NET because you have to keep the life cycle in mind when you use them.

      Events for controls are created early in the life cycle when the ViewState is loaded. If the controls don't exist during this stage, then the events won't get created and your code will never be executed.


      This means that your dynamic controls have to be created during the Page_Init event.

      Check out this article about How to use dynamic controls in ASP.NET for more information and a simple example.

      I recommend that you avoid using dynamic controls and place the ASP.NET code in the appropriate design file instead. It will be a lot easier for you to design the page, a lot easier for you to use the controls and it will keep your UI code out of your Server/Back-end code (clean)

      Also, please note that an VB.NET code that you implement is executed On The Server.

      Therefore, the code MsgBox(lblId.Te xt) will not appear in a browser that is not running on the same computer as the web server.



      -Frinny
      Last edited by Frinavale; Oct 7 '14, 01:22 PM.

      Comment

      Working...