Results 1 to 15 of 15

Thread: ImageButton Control - OnCommand

  1. #1

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    ImageButton Control - OnCommand

    I am adding an ImageButton control to my ASPX page dynamically in code (VB.NET) by adding it to the controls collection of a PlaceHolder control. However, "OnCommand" is not listed in the properties of that control. I need to set this to specify the name of the procedure which will act as the event handler when this control is clicked on.

    Does anyone know how I set it?

    Here is my code so far:
    VB Code:
    1. ibPOD = New ImageButton
    2. ibPOD.ImageUrl = "View.ico"
    3. ibPOD.CommandName = "ViewJob"
    4. ibPOD.CommandArgument = e.Item.DataItem("pk_Job_in")
    5. ibPOD.ToolTip = "View Job"
    There is no "OnCommand" property...
    Last edited by simonm; May 16th, 2005 at 05:09 AM.
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  2. #2
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ImageButton Control - OnCommand

    According to http://msdn.microsoft.com/library/de...ventsTopic.asp there is an OnCommand.

    If this isn't firing then try OnClick at the end of the day thats the only event that a button fires anyway.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: ImageButton Control - OnCommand

    How about .Attributes.Add()?

  4. #4

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: ImageButton Control - OnCommand

    dj4uk

    It's not that my procedure isn't firing, it's that I can't specify the procedure to fire (which I normally do by setting the "OnCommand" property to the name of the procedure).

    Normall, in HTML, I would set up an image button like so:
    Code:
    <asp:ImageButton 
    	ImageUrl="View.ico" 
    	OnCommand="lnkViewJob_Command"
    	CommandName="ViewJob" 
    	CommandArgument='<%#Container.DataItem("pk_Job_in")%>'
    	Runat="server" 
    />
    For some reason, the "OnCommand" property is not avaiable for me in VB.NET code...

    On this page, it says that "OnCommand" is a protected method...I wonder if that has anything to do with it?
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  5. #5
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ImageButton Control - OnCommand

    According to the page I posted both the Click and Command events occur when the ImageButton is Clicked - I can't see any differences between them at all - would the OnClick event not work in this case?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  6. #6

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: ImageButton Control - OnCommand

    dj4uk
    According to the page I posted both the Click and Command events occur when the ImageButton is Clicked...
    The event may well be occuring...but I need to tell my app which procedure handles the Command event...which is what I normally do by setting the "OnCommand" property.
    ...would the OnClick event not work in this case?
    "OnClick" isn't available either...

    mendhak
    How about .Attributes.Add()?
    Thanks...but it doesn't seen to be working either. Here is how I tried:
    VB Code:
    1. ibPOD.Attributes.Add("OnCommand", "lnkViewJob_Command")
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  7. #7
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ImageButton Control - OnCommand

    Do you mean:
    Code:
    ibPOD = New ImageButton
    ibPOD.ImageUrl = "View.ico"
    ibPOD.CommandName = "ViewJob"
    ibPOD.CommandArgument = e.Item.DataItem("pk_Job_in")
    ibPOD.ToolTip = "View Job"
    ibPOD.AddHandler ibPOD.Command, AddressOf ibPOD_OnCommand
    Where ibPOD_OnCommand is a method to be fired OnCommand. Is that what you are trying to do?

    I think this is the right syntax for VB.NET but I use C# mainly so it might be slightly off.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  8. #8

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: ImageButton Control - OnCommand

    dj4uk

    Hmmm...that seems to be what I'm trying to do...unfortunately, my procedure doesn't appear to be handling the event...the page just goes back to the top when I click the ImageButton...
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  9. #9
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ImageButton Control - OnCommand

    Could you post the code for creating the ImageButton and the Command event handler.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  10. #10

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: ImageButton Control - OnCommand

    Code for creating the ImageButton:
    VB Code:
    1. ibPOD = New ImageButton
    2. ibPOD.ImageUrl = "View.ico"
    3. ibPOD.CommandName = "ViewJob"
    4. ibPOD.CommandArgument = e.Item.DataItem("pk_Job_in")
    5. ibPOD.ToolTip = "View Job"
    6. AddHandler ibPOD.Command, AddressOf lnkViewJob_Command
    Code for handling the Command event:
    VB Code:
    1. Public Sub lnkViewJob_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
    2.     Session("Job.ID") = e.CommandArgument
    3.     Response.Redirect("Job.aspx")
    4. End Sub
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  11. #11
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ImageButton Control - OnCommand

    Hmmmm can't quite see why it isn't working.

    Are you adding the event handler before adding the button to the placeholder control collection?

    Perhaps try the Click event rather than the Command one - I'm not convinced this will fix it though. I'll have a search about and see what I can find.

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  12. #12
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ImageButton Control - OnCommand

    Take a look at http://msdn.microsoft.com/library/de.../dynamicui.asp - see the section on events and dynamic controls.

    I might not have had the VB.NET syntax incorrect - told you I was rusty! Give me C# any day!

    Let me know if it is still not working.

    Cheers

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  13. #13

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: ImageButton Control - OnCommand

    Thanks for the link.

    Now I've tried the following and it still doesn't work:
    VB Code:
    1. AddHandler ibPOD.Command, New CommandEventHandler(AddressOf Me.lnkViewJob_Command)
    However, the article does point out that the event handler needs to be associated on every page visit. However, my ImageButton control is added to a table in a Repeater and occurs in the following event:
    VB Code:
    1. Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
    Perhaps therin lies the problem? I don't know how to get around it though....
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

  14. #14
    Frenzied Member dj4uk's Avatar
    Join Date
    Aug 2002
    Location
    Birmingham, UK Lobotomies: 3
    Posts
    1,131

    Re: ImageButton Control - OnCommand

    This might sound like a silly question but why is the button being created dynamically within a repeater?

    Another option (depending if it fits what you are doing) is to add the ImageButton into the repeater ItemTemplate and then only display it when needed (instead of creating it just make it visible).

    Dunno if that could be a way?

    DJ

    If I have been helpful please rate my post. If I haven't tell me!

  15. #15

    Thread Starter
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Re: ImageButton Control - OnCommand

    I am creating it dynamically because I don't want it to appear on every row...only when the data in a corresponding field is a particular value.

    I suppose I could try making it visible and invisible as needed...
    Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width