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?

P.Brian.Mackey
  • 43,228
  • 68
  • 238
  • 348
  • possible duplicate of [Call ASP.NET Function From Javascript?](http://stackoverflow.com/questions/3713/call-asp-net-function-from-javascript) – stuartd Mar 29 '11 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. – P.Brian.Mackey Mar 29 '11 at 14:45

5 Answers5

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

Sergi Papaseit
  • 15,999
  • 16
  • 67
  • 101
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.

Terry
  • 5,132
  • 4
  • 33
  • 66
  • This looks like a good solution. Is the "Ajax.AjaxMethod()" assembly available as a seperate download somewhere? – P.Brian.Mackey Mar 29 '11 at 15:02
  • i'm not sure where my company got it, but i thought you could download it, like discussed in this post: http://forums.asp.net/t/1383384.aspx – Terry Mar 29 '11 at 15:12
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

Community
  • 1
  • 1
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
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:

Heinzi
  • 167,459
  • 57
  • 363
  • 519
0

use

onserverclick="Email_Clicked" 
Morten Anderson
  • 2,301
  • 15
  • 20