OnClick event question

E

EMW

My datagrid has four template columns in which an ImageButton is created.
I use the following to add the OnClick event to the button.

img.Attributes.Add("OnClick", "AllButtons_Click")

The AllButtons_Click sub is:

Sub AllButtons_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Btn As System.Web.UI.WebControls.Button = sender
Response.Write("You clicked button " & Btn.CommandArgument)
End Sub

But this sub is not run, I get an error telling me AllButtons_Click is not
defined.
Can anyone help me with this?

thanks,
Eric
 
M

Marina

You are trying to run a server side function on the client. Web pages don't
work that way.

You need to add a 'onlick' attribute, and have the function be a client side
function written in client side javascript.

If you want to add a server side event handler, then you have to use the
AddHandler statement - as it has nothing to do with the attributes rendered
on the client.

You just can't mix client side and server side events like this.
 
G

George Durzi

Instead of adding it as an attribute, who don't you just do
OnClick="AllButtons_Click"
Note that for an ImageButton click event you should pass
System.Web.UI.ImageClickEventArgs e instead of System.EventArgs
 
A

ashelley

My datagrid has four template columns in which an ImageButton is created.
I use the following to add the OnClick event to the button.

img.Attributes.Add("OnClick", "AllButtons_Click")

The AllButtons_Click sub is:

Sub AllButtons_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim Btn As System.Web.UI.WebControls.Button = sender
Response.Write("You clicked button " & Btn.CommandArgument)
End Sub

But this sub is not run, I get an error telling me AllButtons_Click is not
defined.
Can anyone help me with this?

thanks,
Eric

Attributes.add is for adding to the html element. This has nothing to
do with the .net event model. You will have to add an event handler
to handle the click event.

-Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top