problem with onclick/onserverclick with hjtmlbutton

P

phil

Hi,

I defined a Html button but added the option 'runat="server" ' like this:
<form id="form1" runat="server">
<input id="Button1" type="button" value="button" runat="server"
onclick="hfd()" />
</form>
<script language="javascript" type="text/javascript">
function hfd()
{alert("ok")}
</script>

This works.

But if i add the function 'button1_click' in the code-behind like this:
Protected Sub Button1_ServerClick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.ServerClick
Response.Write("ok from server")
End Sub

I expect first the Alert and then the response.write message, but I get the
javascript error: expected: ';' and nothing is shown.

Is it not possible to do a client-event and a server-event together?
Thanks for help
Phil
 
M

mnichols

Hi,

I tried your scenario:

the input I added had this:

onclick="alert(1);"

when rendered by the server it had this:

onclick="alert(1); __doPostBack('Button1','')"

It did show the alert and then did the postback. So yes it is possible.

I think you probably have a different javascript error somewhere else in
your page.

Is the error, javascript error: expected: ';' giving you a line number?
It should. You might have to turn on more of your browser's debugging
features to see it.

I would do a view source on the rendered page and try to find the real
javascript error.

mnichols
 
P

phil

Yes, you're right.
My fault was that i forgot to put ';' in onclick="hfd()".
Thanks
The Javascript error message mentioned line 35, but there is only 21 lines
in the aspx file ..So it's hard to find the error ...
 
M

mnichols

The line number (35) refers to the rendered page not the original source
So you have to view the page in the browser, then view the rendered
source from the browser and then find line number 35 in it.

Also, normally onclick="alert(1)" would be fine, but because ASP.NET is
adding code to the event onclick="alert(1);" is required. (One might
think that ASP.NET would have inserted the semi-colon for you just to
play it safe since two semi-colons would be harmless.)

mnichols
 
P

phil

Thanks again..

mnichols said:
The line number (35) refers to the rendered page not the original source
So you have to view the page in the browser, then view the rendered
source from the browser and then find line number 35 in it.

Also, normally onclick="alert(1)" would be fine, but because ASP.NET is
adding code to the event onclick="alert(1);" is required. (One might
think that ASP.NET would have inserted the semi-colon for you just to
play it safe since two semi-colons would be harmless.)

mnichols
 

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

Top