onclick button event throws compiler error

W

wardy1975

Hi all....looking for a bit of help here....I'm using an asp.net server
side button, and trying to add an onclick event on the button to call a
javascript function. However, I am trying to enter the onclick syntax
directly in the html instead of via the attributes.add functionality
(this is required due to the dynamic nature of how the buttons are
added to the screen). The problem is, I get a compilation error when I
try to load the page that the first word I have in the onclick
definition "is not a member of " and my page name.

So, if I had the code below in a webform called Webform4.aspx:

<asp:button id="btnClearSubmit" onclick="testfn();" runat="server"
commandname="submit" cssclass="test"
text="Clear List">

I get the compliation error:

Compiler Error Message: BC30456: 'testfn' is not a member of
'ASP.WebForm4_aspx'.

Anyone ever run into this before? Am I doing something wrong? In my
mind I would have thought that entering an onclick definition directly
in the html should still work for server side buttons.

Thanks
 
M

Marina Levit [MVP]

That onclick in the server side tag refers to the server side method to run
when the button postsback to the server. It's a declarative way of
attaching a server side event handler. It is not the client side event to
run.

You need still add the client side one via Attributes.Add.
 
A

Anthony Merante

Yea like Marina said: Specifically something like this will work for you:
in the pageload of the form


btnClearSubmit.Add.Attributes("Onclick", "testfn();");

try that

HTH,
Tony
 
W

wardy1975

Hi all....thanks for the replies. The problem with the use of the
Add.Attributes syntax is that we do not know at design time how many
buttons and the names of the buttons as we allow for the dynamic
addition of server side controls in our rendering engine, so we cannot
do this in the codebehind. So, we need to be able to do this in the
html directly. The strange thing is, we have this working in other
parts of the application where the onlick is simply written into the
html, and it has no problem compiling or rendering or calling the
javascript function then doing the postback. The code that works is as
follows:

<asp:button runat="server" id="btnSubmit" text="Save"
commandname="submit" onClick="return ValidateForm(document.forms[0]);"
cssclass="AddToBasket"></asp:button>

Any thoughts on the discrepancy?
 
Joined
Jun 2, 2010
Messages
1
Reaction score
0
Try using OnClientClick

I know this thread is old, but I had the same problem today. Have you tried using OnClientClick instead of OnClick? This was worked to fix it for me.
 

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