problem with onclick/onserverclick with hjtmlbutton

Discussion in 'ASP .Net Web Controls' started by phil, Jun 5, 2006.

  1. phil

    phil Guest

    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
     
    phil, Jun 5, 2006
    #1
    1. Advertising

  2. phil

    mnichols Guest

    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


    phil wrote:
    > 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
    >
    >
     
    mnichols, Jun 6, 2006
    #2
    1. Advertising

  3. phil

    phil Guest

    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 ...


    "mnichols" <> wrote in message
    news:...
    > 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
    >
    >
    > phil wrote:
    > > 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
    > >
    > >
     
    phil, Jun 6, 2006
    #3
  4. phil

    mnichols Guest

    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

    phil wrote:
    > 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 ...
    >
    >
    > "mnichols" <> wrote in message
    > news:...
    >> 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
    >>
    >>
    >> phil wrote:
    >>> 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
    >>>
    >>>

    >
    >
     
    mnichols, Jun 6, 2006
    #4
  5. phil

    phil Guest

    Thanks again..

    "mnichols" <> wrote in message
    news:...
    > 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
    >
    > phil wrote:
    > > 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 ...
    > >
    > >
    > > "mnichols" <> wrote in message
    > > news:...
    > >> 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
    > >>
    > >>
    > >> phil wrote:
    > >>> 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
    > >>>
    > >>>

    > >
    > >
     
    phil, Jun 6, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krishna
    Replies:
    1
    Views:
    7,431
    Alvin Bruney
    Nov 23, 2003
  2. =?Utf-8?B?QnJ5YW4=?=

    Sending a Asp Repeater Hyperlink to a OnServerClick

    =?Utf-8?B?QnJ5YW4=?=, May 19, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    873
    =?Utf-8?B?QnJ5YW4=?=
    May 19, 2004
  3. phil
    Replies:
    4
    Views:
    4,448
  4. bob
    Replies:
    3
    Views:
    189
  5. Replies:
    2
    Views:
    261
Loading...

Share This Page