473,395 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
2 1502
iam_clint
1,208 Expert 1GB
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
9,735 Expert Mod 8TB
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

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

Similar topics

9
by: Marek Mand | last post by:
How to use generated keyboard events? What I am trying here to do is in onkeyup event handler http://www.hot.ee/idaliiga/braggart/createEventTest.htm generate a (shift)TAB keydown so the...
8
by: frankcvc | last post by:
I created a control array at runtime using the following code. I also wanted to code the controlarray click event. My code can respond to click event but cannot identify the specific control...
2
by: Nikki | last post by:
Hi, I m developing a Windows application in which i want to create a context menu, having variable number of submenus depending on the the list in the file. I m able to create these submenus at...
3
by: JohnR | last post by:
I have a form with a number of text boxes, comboboxes etc. What I would like to do is create an event handler for the "mouseenter" event for each of the controls whereby I display information...
3
by: akki | last post by:
I create link buttons dynamically in the code and give them IDs through which I can distinguish them.I want to know how to create a single event handler for all the link buttons that I create The...
0
by: samuelberthelot | last post by:
Hi, I add an event handler to my dropdownlist in the Load event of my page. Note that the dropdownlists are created at run-time and are part of a datagrid. It works fine the first time, then I'm...
1
by: amitsaxena1981 | last post by:
Hi, I am using asp.net with vb.net and creating the user control .My user control generate the table at runtime in which each column has button so i want to create the event handler for each run...
1
by: amitsaxena1981 | last post by:
Hi, i am using asp.net with vb.net and creating the user control right now i am creating the user control at runtime. my user control generate the table at runtime and each column of table...
3
by: news.rz.uni-karlsruhe.de | last post by:
I wrote an event handler with Sys.UI.DomEvent.addHandler($get("MyButton"),"click",myfunc); function myfunc(event) { alert("Hi!"); }
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.