2

net application and I want to create me a own Button with css and html and use the onclick event for execute c# code. But I get the error:

Runtimeerror in Microsoft JScript: "BtnAdd_Click" is undefine

Here is my Code:

ASPX file:

<div id="GuestListViewControls">
                <table>
                    <td><a href="#" class="GuestButtons" runat="server" onclick="BtnAdd_Click">Hinzufügen</a></td>
                </table>
               </div>

My C#:

protected void BtnAdd_Click(object sender, EventArgs e) 
        {
            Response.Write("Add");
        }

Can I use this Button with C# Code in a ASP.NET Application? :/

4
  • 1
    Your aspx is incorrect. You need to add a <tr> above <td> and close it just before </table>. Commented Oct 23, 2013 at 9:27
  • This doesn't solve my problem...and it works without <tr>, too; ;-) Commented Oct 23, 2013 at 9:30
  • That doesn't mean that you can ignore the markup. Commented Oct 23, 2013 at 9:32
  • i add the <tr> tag now ^^ Commented Oct 23, 2013 at 9:37

5 Answers 5

2

It's not a c# error, It's like a javascript error , Now You can use LinkButton for onclick event, It's look like also anchor tag

<asp:LinkButton ID="MyLink" runat="server" class="GuestButtons" OnClick="BtnAdd_Click" Text="Click Here"></asp:LinkButton>

or use onserverclick event instead of onclick event

<a href="#" runat="server"  class="GuestButtons" onserverclick="BtnAdd_Click">test</a>
Sign up to request clarification or add additional context in comments.

2 Comments

@RameshRajendran it can be done with anchor tag only.
@Rex , Yes we can use onserverclick ! But if the OP must need to Onclick event, The he can use linkbutton !
2

You are using "A" tag. This is HTML Anchor control and it does not support server side event.

Use

<asp:Button id="mybutton" Text="Submit" runat="server" onclick="BtnAdd_Click" />

Comments

2

You can do it using HTML controls only you just need to use on onserverclick

Sample Test stuff

<a href="#" runat="server" onserverclick="anchorclick">test</a>

code behind

protected void anchorclick(object sender, EventArgs e)
{
     // do stuff here
}

See Refrence

HTML Anchor Server Side Click

HTML Button Click Server Side

Comments

1

You're getting this error:

Runtimeerror in Microsoft JScript: "BtnAdd_Click" is undefined

Because you're using an onclick event on an HTML anchor tag.

You need to use an asp:Button tag instead, for which the onclick event will point to a server-side method. Something like this:

<asp:Button class="GuestButtons" runat="server" onclick="BtnAdd_Click" text="Hinzufügen"></asp:Button>

Comments

1

You are getting "Runtimeerror in Microsoft JScript: "BtnAdd_Click" is undefine" because onclick searches for the method in the Javascript.

One option would be have a javascript method on the href click and from that method you can invoke the server side method using Ajax post or get or simple .Ajax

Please refer solution 4 in the url http://www.codeproject.com/Questions/334635/how-to-call-a-csharp-function-in-a-href-onclick-ev

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.