By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,956 Members | 1,722 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,956 IT Pros & Developers. It's quick & easy.

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

P: 1
In my asp.net web application, i have created controls programmatically, 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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub LoadProducts(ByVal grpId As Integer)
  2.                     Dim img1 As Integer = 50
  3.             Dim PropertyFromTop As Integer = 130
  4.             Dim PriceFromTop As Integer = 150
  5.             Dim DescFromTop As Integer = 170
  6.             Dim QuantityFromTop As Integer = 190
  7.             Dim linkfromtop As Integer = 210
  8.             Dim IdfromTop As Integer = 210
  9.             Dim imgCount = (From s In db.TblPosts Where s.TblSubCategory2.SubCategory2Id = grpId Select s)
  10.             For Each s In imgCount
  11.                 Dim imge1 As Image = New Image()
  12.                 Dim l1 As LinkButton = New LinkButton()
  13.                 Dim lblProperty As Label = New Label
  14.                 Dim lblPrice As Label = New Label
  15.                 Dim lblDesc As Label = New Label
  16.                 Dim lblQuantity As Label = New Label
  17.                 lblId = New Label
  18.                 l1.ID = "lnk"
  19.                 l1.Style("Position") = "Absolute"
  20.                 l1.Style("Top") = linkfromtop.ToString() & "px"
  21.                 l1.Style("Left") = "200px"
  22.                 l1.ForeColor = Drawing.Color.Blue
  23.                 imge1.Style("Position") = "Absolute"
  24.                 imge1.Style("Top") = img1.ToString() & "px"
  25.                 imge1.Style("Left") = "10px"
  26.                 imge1.Width = 180
  27.                 imge1.Height = 200
  28.                 lblProperty.Style("Position") = "Absolute"
  29.                 lblProperty.Style("Top") = PropertyFromTop.ToString() & "px"
  30.                 lblProperty.Style("Left") = "200px"
  31.                 lblProperty.ForeColor = Drawing.Color.Black
  32.                 lblPrice.Style("Position") = "Absolute"
  33.                 lblPrice.Style("Top") = PriceFromTop.ToString() & "px"
  34.                 lblPrice.Style("Left") = "200px"
  35.                 lblPrice.ForeColor = Drawing.Color.Black
  36.                 lblDesc.Style("Position") = "Absolute"
  37.                 lblDesc.Style("Top") = DescFromTop.ToString() & "px"
  38.                 lblDesc.Style("Left") = "200px"
  39.                 lblDesc.ForeColor = Drawing.Color.Black
  40.                 lblQuantity.Style("Position") = "Absolute"
  41.                 lblQuantity.Style("Top") = QuantityFromTop.ToString() & "px"
  42.                 lblQuantity.Style("Left") = "200px"
  43.                 lblQuantity.ForeColor = Drawing.Color.Black
  44.                 lblId.Style("Position") = "Absolute"
  45.                 lblId.Style("Top") = IdfromTop.ToString() & "px"
  46.                 lblId.Style("Left") = "280px"
  47.                 lblId.ForeColor = Drawing.Color.Black
  48.                 l1.Text = "Add to Cart"
  49.                 lblId.Text = s.PostId
  50.                 lblProperty.Text = "Name:  " & s.PropertyName
  51.                 lblPrice.Text = "Price:  " & s.Price
  52.                 lblDesc.Text = "Description:  " & s.Description
  53.                 lblQuantity.Text = "Quantity:  " & s.Quantity
  54.                 imge1.ImageUrl = "~/ImageHandler.ashx?Id=" + Convert.ToString(s.PostId)
  55.  
  56.                 AddHandler l1.Click, AddressOf Me.LinkButon_Click
  57.                 Pnlprofile.Controls.Add(lblId)
  58.                 Pnlprofile.Controls.Add(l1)
  59.                 Pnlprofile.Controls.Add(imge1)
  60.                 Pnlprofile.Controls.Add(lblProperty)
  61.                 Pnlprofile.Controls.Add(lblPrice)
  62.                 Pnlprofile.Controls.Add(lblDesc)
  63.                 Pnlprofile.Controls.Add(lblQuantity)
  64.                 img1 = Convert.ToInt32(img1)
  65.                 img1 = img1 + 220
  66.                 IdfromTop = Convert.ToInt32(IdfromTop)
  67.                 IdfromTop = IdfromTop + 220
  68.                 linkfromtop = Convert.ToInt32(linkfromtop)
  69.                 linkfromtop = linkfromtop + 220
  70.                 PropertyFromTop = Convert.ToInt32(PropertyFromTop)
  71.                 PropertyFromTop = PropertyFromTop + 220
  72.                 PriceFromTop = Convert.ToInt32(PriceFromTop)
  73.                 PriceFromTop = PriceFromTop + 220
  74.                 DescFromTop = Convert.ToInt32(DescFromTop)
  75.                 DescFromTop = DescFromTop + 220
  76.                 QuantityFromTop = Convert.ToInt32(QuantityFromTop)
  77.                 QuantityFromTop = QuantityFromTop + 220
  78.             Next
  79.  
  80.     End Sub
  81.  
  82.  Private Sub LinkButon_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  83.         MsgBox(lblId.Text)
  84.     End Sub
May 24 '14 #1
Share this Question
Share on Google+
2 Replies


iam_clint
Expert 100+
P: 1,208
i'm probably a little late here but sender is what you are looking for
Expand|Select|Wrap|Line Numbers
  1.   Private Sub LinkButon_Click(ByVal sender As LinkButton, ByVal e As System.EventArgs)
  2.         MsgBox(sender.Text)
  3.     End Sub
  4.  
Oct 5 '14 #2

Frinavale
Expert Mod 5K+
P: 9,731
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.Text) will not appear in a browser that is not running on the same computer as the web server.



-Frinny
Oct 7 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.