0

Ok, so I've got some HTML that's generated dynamically. Part of it

StringBuilder.Append("<input type=\"button\" onclick=\"Email_Clicked\" value=\"Email\" class=\"button\" runat=\"server\" />");

I need this to call C# method Email_Clicked

How can I accomplish this without asp:button control?

2
  • possible duplicate of Call ASP.NET Function From Javascript? Commented Mar 29, 2011 at 14:41
  • @Stuart Dunkeld - That question is similar, but this is not an exact dup. I dont care if the solution is AJAX, as long as it works. Commented Mar 29, 2011 at 14:45

5 Answers 5

1

What about doing an ajax call to a method in your aspx page with jQuery? Take a look at this post about Using jQuery to directly call ASP.NET AJAX page methods

Sign up to request clarification or add additional context in comments.

Comments

1

As mentioned before, Ajax is easy to implement and will do the trick. Put this attribute on your method:

<Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)> _
        Public Function DoSomething() As Integer
            'handle stuff
        End Function

you can call the method from javascript by using this:

Declaration.DoSomething()

don't forget to register your ajax call object in for example the pageload:

Ajax.Utility.RegisterTypeForAjax(GetType(Declaration))

If you don't register it, calling Declaration.DoSomething() won't work.

2 Comments

This looks like a good solution. Is the "Ajax.AjaxMethod()" assembly available as a seperate download somewhere?
i'm not sure where my company got it, but i thought you could download it, like discussed in this post: forums.asp.net/t/1383384.aspx
0

You can "manually" force a postback by calling the __doPostBack method in JavaScript. The following web page contains a short tutorial on how to do that:

Comments

0

You can't.. directly.

You can use Page Methods or plain AJAX for this though.

Here is relevant question here combining both:
Calling an ASP.NET server side method via jQuery

Comments

0

use

onserverclick="Email_Clicked" 

1 Comment

I tried this before I posted on SO. I could not get it to call the method, never hit breakpoint.

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.