|
-
May 16th, 2005, 04:49 AM
#1
Thread Starter
Fanatic Member
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...
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. 
-
May 16th, 2005, 06:04 AM
#2
Frenzied Member
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!
-
May 16th, 2005, 06:16 AM
#3
Re: ImageButton Control - OnCommand
How about .Attributes.Add()?
-
May 16th, 2005, 06:19 AM
#4
Thread Starter
Fanatic Member
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. 
-
May 16th, 2005, 06:23 AM
#5
Frenzied Member
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!
-
May 16th, 2005, 06:28 AM
#6
Thread Starter
Fanatic Member
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:
ibPOD.Attributes.Add("OnCommand", "lnkViewJob_Command")
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
May 16th, 2005, 06:34 AM
#7
Frenzied Member
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!
-
May 16th, 2005, 06:47 AM
#8
Thread Starter
Fanatic Member
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. 
-
May 16th, 2005, 06:51 AM
#9
Frenzied Member
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!
-
May 16th, 2005, 06:57 AM
#10
Thread Starter
Fanatic Member
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
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
May 16th, 2005, 08:27 AM
#11
Frenzied Member
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!
-
May 16th, 2005, 08:31 AM
#12
Frenzied Member
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!
-
May 16th, 2005, 09:07 AM
#13
Thread Starter
Fanatic Member
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....
Everything I say is either loose interpretation of dubious facts or idle speculation rooted in irrational sentiment. 
-
May 16th, 2005, 09:11 AM
#14
Frenzied Member
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!
-
May 16th, 2005, 10:22 AM
#15
Thread Starter
Fanatic Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|