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:
ibPOD = New ImageButton
ibPOD.ImageUrl = "View.ico"
ibPOD.CommandName = "ViewJob"
ibPOD.CommandArgument = e.Item.DataItem("pk_Job_in")
ibPOD.ToolTip = "View Job"
There is no "OnCommand" property... :(
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
Re: ImageButton Control - OnCommand
How about .Attributes.Add()?
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... :cry:
On this page, it says that "OnCommand" is a protected method...I wonder if that has anything to do with it? :sick:
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
Re: ImageButton Control - OnCommand
dj4uk
Quote:
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.
Quote:
...would the OnClick event not work in this case?
"OnClick" isn't available either...
mendhak
Quote:
How about .Attributes.Add()?
Thanks...but it doesn't seen to be working either. Here is how I tried:
VB Code:
ibPOD.Attributes.Add("OnCommand", "lnkViewJob_Command")
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
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...:confused:
Re: ImageButton Control - OnCommand
Could you post the code for creating the ImageButton and the Command event handler.
DJ
Re: ImageButton Control - OnCommand
Code for creating the ImageButton:
VB Code:
ibPOD = New ImageButton
ibPOD.ImageUrl = "View.ico"
ibPOD.CommandName = "ViewJob"
ibPOD.CommandArgument = e.Item.DataItem("pk_Job_in")
ibPOD.ToolTip = "View Job"
AddHandler ibPOD.Command, AddressOf lnkViewJob_Command
Code for handling the Command event:
VB Code:
Public Sub lnkViewJob_Command(ByVal sender As Object, ByVal e As CommandEventArgs)
Session("Job.ID") = e.CommandArgument
Response.Redirect("Job.aspx")
End Sub
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
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
Re: ImageButton Control - OnCommand
Thanks for the link.
Now I've tried the following and it still doesn't work:
VB Code:
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:
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....:(
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
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...