ofir2471
ofir2471

Reputation: 87

HTML tag:label with onclick asp.net method.

I have this form:

    <form id="form21" runat="server" method="post" onsubmit="return send()">
          <asp:Button runat="server"  ID="submit" name="submit" type="submit" class="btn btn-send" Text="עדכן" onclick="submit_Click" />
          <label ID="other" name="other" runat="server" class="btn btn-send" onclick="other_Click" Visible="False">נהל משתמש אחר</label>
    </form>

When I click on the label this error occur: "JavaScript runtime error: 'other_Click' is undefined".

How can I combine between asp.net method and HTML tag?

Upvotes: 0

Views: 1450

Answers (2)

Enrique YC
Enrique YC

Reputation: 86

you should define what do you want to do, the click is for show or execute one task or do you want execute to the server side..

For execute one task with the click and not call to the server, you should define the function javascript and call

For execute call to the server with the event click , you should use wcf ajax for call the server..

or maybe this can help you

http://www.morgantechspace.com/2014/01/Call-Server-Side-function-from-JavaScript-in-ASP-NET.html

Upvotes: 0

Andrei
Andrei

Reputation: 56688

From server perspective, this:

<label ID="other" name="other" runat="server" ...

is an instance of HtmlGenericControl class. This class and most of its subclasses does not have server-side click event. So the onclick you defined is not treated as server-side subscription and therefore rendered into the HTML as is. So on the final page is this just a JavaScript function call. And since you do not have such function on the client side, you get the error.

To resolve it you might want to switch to some sever-side control which has serve-side Click event. For example LinkButton, though it will require some css to look like label. Other option is to generate postback yourself on the client, but this is a bit unusual path.

Upvotes: 2

Related Questions