dynamically creating imagebutton at run time


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 2 of 2

Thread: dynamically creating imagebutton at run time

  1. #1
    Clint Bates Guest

    dynamically creating imagebutton at run time


    I am creating an <asp:ImageButton> on the fly in vb.net. The problem I am
    having is I can not call a server side event when the imagebutton is clicked.
    Does anyone know how to do this?

    VB code behind.

    Dim cellImage As New ImageButton()

    cellImage.ID = "ImageButton"
    cellImage.ImageUrl = "images/images/1001.gif"
    cellImage.Height = Unit.Pixel(67)
    cellImage.Width = Unit.Pixel(100)

    'here where I run into trouble...
    'this line will create an onclick client side...I would like the onclick
    to occur server side.
    cellImage.Attributes("onclick") = "getImage_Clicked('images/1001.gif')"


    This is easy to do when you put the tag on the aspx page...but how do you
    call an event on the server if you build the control dynamically???



  2. #2
    Russell Jones Guest

    Re: dynamically creating imagebutton at run time

    You're looking for the AddHandler method. Here's an example Web Form that
    creates a Button control, adds it to the default server-side Form's Controls
    collection, and assigns the clickMe method to the new Button's Click event.
    To test the code, create a new Web Form, and then paste this code into it,
    replacing ALL the default code in the Web Form. I'm using RC1.

    Public Class addButtonDynamically
    Inherits System.Web.UI.Page

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeComponent()
    End Sub

    #End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    Dim button As New System.Web.UI.WebControls.Button()
    button.Text = "Click me"
    button.Visible = True
    button.Style.Item("left") = "100px"
    button.Style.Item("top") = "100px"
    button.Style.Item("position") = "absolute"
    Me.FindControl("Form1").Controls.Add(button)
    AddHandler button.Click, AddressOf Me.clickMe
    End Sub

    Sub clickMe(ByVal sender As Object, ByVal e As System.EventArgs)
    Response.Write("The button was clicked")
    End Sub
    End Class

    "Clint Bates" <clint.bates@xwave.com> wrote in message
    news:3c3c8aa0$1@147.208.176.211...
    >
    > I am creating an <asp:ImageButton> on the fly in vb.net. The problem I am
    > having is I can not call a server side event when the imagebutton is

    clicked.
    > Does anyone know how to do this?
    >
    > VB code behind.
    >
    > Dim cellImage As New ImageButton()
    >
    > cellImage.ID = "ImageButton"
    > cellImage.ImageUrl = "images/images/1001.gif"
    > cellImage.Height = Unit.Pixel(67)
    > cellImage.Width = Unit.Pixel(100)
    >
    > 'here where I run into trouble...
    > 'this line will create an onclick client side...I would like the

    onclick
    > to occur server side.
    > cellImage.Attributes("onclick") = "getImage_Clicked('images/1001.gif')"
    >
    >
    > This is easy to do when you put the tag on the aspx page...but how do you
    > call an event on the server if you build the control dynamically???
    >
    >




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center