Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Dynamically adding event handlers to a dynamically created control

Dynamically adding event handlers to a dynamically created control

Scheduled Pinned Locked Moved ASP.NET
helphtmlcomdesigntutorial
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jcrussell
    wrote on last edited by
    #1

    I've made a simple web user control that contains a placeholder and a label inside an HTML table, and I would like to make an event handler that allows me to click on any part of my control and redirect to another page. I just can't seem to get it to work. I think my problem is that I am trying to dynamically add my control to a page inside a loop (ie: there is more than one instance of my control on the page) this is my web control: Public Event Click As EventHandler Private _thumbnailPath As String Private _name As String Private _id As Long Protected Overridable Sub OnClick(ByVal e As System.EventArgs) RaiseEvent Click(Me, e) End Sub Public Sub RaisePostBackEvent(ByVal eventArgument As String) Implements IPostBackEventHandler.Raisepostbackevent OnClick(EventArgs.Empty) End Sub Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim eqImage As New System.Web.UI.WebControls.Image eqImage.ImageUrl = "~/" & _thumbnailPath eqImage.Width = New System.Web.UI.WebControls.Unit(thumbnailWidth) lblEquipmentName.Text = _name phEquipmentImage.Controls.Add(eqImage) 'add image to my placeholder End Sub then to add my control to the page I do it inside a loop: if not page.isPostBack then ' for each datarow in my dataset... For Each eqDT As DataRow In tmpEqDS.Tables(0).Rows ucEQ = Page.LoadControl("Controls\equipmentThumbnail.ascx") ' dynamically add my eventhandler to my control AddHandler ucEQ.Click, AddressOf ucEq_Click ' assign some values to my control ucEQ.EquipmentID = eqDT.Item("equipmentID") ucEQ.EquipmentName = eqDT.Item("equipmentName") ucEQ.EquipmentThumbnail = eqDT.Item("equipmentThumbPath") ' add my control to a placeholder phEquipment.Controls.Add(ucEQ) Next end if and my event handler on that page is defined as: Private Sub ucEq_Click(ByVal sender As Object, ByVal e As System.EventArgs) 'Handles ucEq.Click Response.Redirect("http://www.google.com") End Sub I am unsure of how to do this. My current code, when the user clicks on the control (any of them) it should redirect to www.google.com. this doesn't work at all. I have break points in there and it seems to assign the event to the eventhandler, but it just never gets fired. I'm unsure of where I have gone wrong so would really appreciate a bit of help on this /jason

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups