Link to home
Start Free TrialLog in
Avatar of seanfurlong
seanfurlong

asked on

image button not calling function

I have the following code on my page: This page is called from a server.transfer

  <asp:ImageButton id="LogBtn" ImageURL="images/search_p.gif" OnClick="AddPersonsBtn_Click" runat="server" />      

But it doesn't seem to do anything!!
Please help
Cheers
Sean.      
                                          
Avatar of Tacobell777
Tacobell777

Do you want to submit a form?

OnClick="document.form name here.submit()"
Avatar of seanfurlong

ASKER

No, eventually the button will be populated with code to create a textfile with information from the page.. need insperation.
Are you trying to call an ASP script from a Javascript Method?

If so, you might want to create a separate page with your ASP code and then open it like so:

Onclick="javascript: location.href='addpersons.asp'".

In fact, it doesn't need to be an image button for this. You can add an OnClick method for a normal image.
It would be helpful to indentify the problem if you could paste the html part that is generated at the clients browser.
something like...:

<%@ Page Langauge="vb" %>
<script runat="server">
Public Sub AddPersonsBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddPersonsBtn.Click
Response.Write("AddPersonsBtn clicked!")
End Sub
</script>
<asp:ImageButton id="AddPersonsBtn" ImageURL="images/search_p.gif" OnClick="AddPersonsBtn_Click" runat="server" />
If you want something to happen in the browser, at the client, you can't use runat=server.
what i understnd from ur code is u r using ASP.net and put a imagebutton on ur .aspx page .. so now what do u want to do? buttonclick event to be executed on
1) server
or
2)client


1) to execute on the server .. double click on the image button and u will be taken to the code behind (assumed ur code behind is VB and not C#) where u can write ur rewuired code in the onclick event
it will look soemthing like this
------------------------------------
Private Sub LogBtn_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles LogBtn.Click

End Sub
-------------------------------------


2) now assuming u want to call a client side script when the imagebutton is clicked then u have to add the following code in the page_load event
---------------------------------
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack() Then
            LogBtn.Attributes.Add("Language", "javascript")
            LogBtn.Attributes.Add("onclick", "ForImageClickEvent()")
        End If
    End Sub
-------------------------------

and then in ur .aspx page add this java script tag within ur <head></head> tag
---------------------------
            <script language =javascript >
            function ForImageClickEvent()
            {
                  alert("image clicked");
            }
            </script>

---------------------------------

I think you're trying to call the procedure() using several conflicting methods. Try ONE of the following and see what it does:
-----------------------------------------------
<script runat="server">
Public Sub AddPersonsBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddPersonsBtn.Click
Response.Write("AddPersonsBtn clicked!")
End Sub
</script>
<asp:ImageButton id="AddPersonsBtn" ImageURL="images/search_p.gif" runat="server" />
-----------------------------------------------
<script runat="server">
Public Sub AddPersonsBtn(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddPersonsBtn.Click
Response.Write("AddPersonsBtn clicked!")
End Sub
</script>
<asp:ImageButton id="AddPersonsBtn" ImageURL="images/search_p.gif" OnClick="AddPersonsBtn" runat="server" />
-----------------------------------------------
<script runat="server" for="AddPersonsBtn" event="onClick">
Public Sub AddPersonsFcn(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddPersonsBtn.Click
Response.Write("AddPersonsBtn clicked!")
End Sub
</script>
<asp:ImageButton id="AddPersonsBtn" ImageURL="images/search_p.gif" runat="server" />
-----------------------------------------------
put () after AddPersonsBtn_Click
TheKenman had it correct - you are issuing the event handler twice - once in the ASPX code with the ONCLICK then again with the HANDLES keyword.  Use only one of them, preferable HANDLES.

Do not confuse Server side code with Client side code as a lot of people do.  Also try to put as much of your code in the codebehind and not into the ASPX via <script runat="server">.   The CLR and JIT compiler work much faster with codebehind.

Lee
where is it mentioned that he is using the onlcick event twice
<<<asp:ImageButton id="LogBtn" ImageURL="images/search_p.gif" OnClick="AddPersonsBtn_Click" runat="server" />     >>
... that is y my explanation assumes both and gives solutions accordingly ;-) .. i think TheKenman also assumed the same thing .. anyway the author has not come back with his side of the story as to what exactly he wants .. so iam waiting ...
gam3r_3xtr3m3's comment - with all the various posts I jumped (too quickly) to thinking he was the original poster and did not check.  Based on the original posting it really looks like he (the original poster) was trying to write server-side code (naming convention and all - I admit an assumption but an educated one).   My response though really was from personal experience with seeing this occur - possibly not the right one but it could not hurt to check (few things worse than extra code for no reason).

Your solution concerns client and server solutions.  My comment was about setting up the server-side event improperly (in the ASPX and the codebehind) and nothing to do with client side code at all.

Lee
Instead of:

Public Sub AddPersonsBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddPersonsBtn.Click

try:

Public Sub AddPersonsBtn_Click(sender As Object, e As ImageClickEventArgs) Handles AddPersonsBtn.Click
You need to have asp.net installed on the server and you must name the file with an .aspx extension.
save this code below as button.aspx and try it (change the image URL to an image file on your server).

<html>
<head>
<script language="JScript" runat="server">
function Image_OnClick(sender:Object, e:ImageClickEventArgs) : void
{
   Label1.Text="You clicked the image button";
}
</script>
</head>
<body>
<h3>ImageButton Example</h3>
<form runat=server>
<asp:ImageButton id=Button1 ImageUrl="Thumbs-up.gif" BorderWidth="2px" onclick="Image_OnClick" runat="server"/>
&nbsp;&nbsp;
<asp:Label id=Label1 runat=server />
</form>
</body>
</html>

Regards
Peter
Is this function AddPersonsBtn_Click on code behind or defined on the same aspx page?

Cheers
hey seanfurlong, any response? =p
You cannot use System.EventArgs for this - your event must have signature like sender As Object, e As ImageClickEventArgs.  This is because the ImageClickEventArgs give you mouse x,y coordinates.

I also suggest turning trace on for this page and seeing the events that are stepped through, which tracing will show you.  You can also add to tracing with your own comments - ie Trace.WriteLine("in my function!!").  You can even leave this code in if you like, because once you turn tracing off it is never dealt with again - and does not effect processing of the page.
Take a look at:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebuiwebcontrolsimagebuttonclasscommandnametopic.asp

for a good example.  Note both the event as well as the OnCommand property in the example.
should we not stop at some place and wait for the author's reponse ???? he/she does not seem to be interested and most of us have already made alot of assumptions ... so pls. hold on for the author to get back with what his exact requirements are or whether it has already been fullfilled ...

seanfurlong, we would all appreciate if u could give a feedback ..
<asp:ImageButton id="LogBtn" ImageURL="images/search_p.gif" OnClick="AddPersonsBtn_Click" runat="server" />    


By default asp:ImageButton generates a postback so you can put your click event handler in the .aspx.vb file directly and you can handle all your server side handling there. You need not specify the handler name as you did.

However if you need to do some client side handling before the postback then you need put a function in the .aspx file. And you need to specify explicitly that you need a client side handling before postback. This is done by putting the following code in the Page Load event handler.

LogBtn.Attributes.Add("OnClick", "return AddPersonsBtn_Click( )")
Note:- Dont forget to put this declaration on the top of the aspx.vb file :
        Protected WithEvents LogBtn As System.Web.UI.WebControls.ImageButton

From the "AddPersonsBtn_Click" you can return false if do not want a postback or return true if need a postback after the client side handling.
Write the below procedure in your code behind file (or server script block of the page)

Protected Sub AddPersonsBtn_Click(ByVal sender As Object, ByVal e As  System.Web.UI.ImageClickEventArgs)
 '// your code goes here
 '// eg: if ypu have label lable1 -> Me.Label1.Text = "test"
End Sub

If the procedure is not there , Usually it gives an error saying "AddPersonsBtn_Click method not found in <yourpage>.aspx"




and the story goes on, but "seanfurlong" refuses to respond ..... come on, break ur silence ;-)
I give up :D
It seems that nobody's home!


Best regards.

Thanks for the clean-up work mplungjan!
ASKER CERTIFIED SOLUTION
Avatar of GhostMod
GhostMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial