1

I am creating a asp.net web application and I want to know how to call, code behind method in javascript. Following Java script shows that getting the values of multiple textboxes with same name in to array.

using code behind method, I try to pass the values of an array ,but it didn't works. when I am using alert box, it display the textbox values .

function JavaScriptFunction() {
        var arr = $("input[name='multiple[]']");
        $.each(arr, function (i, item) {
            alert($(item).val());             
        });
    }

When I am call code behind method before alert box method, then it won't display any message box. (Testing code behind method)

function JavaScriptFunction() {
        var arr = $("input[name='multiple[]']");
        $.each(arr, function (i, item) {
            PageMethods.setemail("Paul Hayman");
            alert($(item).val());               
        });
    } 

This is my testing code behind method. TextBox1 I used just for testing.

  [WebMethod]
    public void setemail(string p)
    {
        TextBox1.Text = p;
    }

Then finally I import webService reference.

  using System.Web.Services;

This is I used webforms for button

 <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="JavaScriptFunction()" OnClick="Button1_Click"  />
nade
  • 153
  • 1
  • 1
  • 8
  • shouldn't it be `public static void setemail(...)` – Dave Becker Feb 17 '15 at 14:43
  • @DaveBecker If he does that, he can't touch the textbox. But interacting directly with the textbox from `WebMethod` isn't possible anyways. – mason Feb 17 '15 at 14:50
  • when I use static keyword. then textbox1 get underlined and giving an error that textbox1 is non static field. – nade Feb 17 '15 at 14:52
  • I'll rephrase - it has to be `static`. Why interact with the `TextBox` server side anyway, what will that accomplish? worth checking this out: http://stackoverflow.com/questions/6928533/calling-a-webmethod-with-jquery-in-asp-net-webforms – Dave Becker Feb 17 '15 at 14:54
  • @nade you cannot interact with Page controls server side through WebMethods – Dave Becker Feb 17 '15 at 14:55
  • I think you are looking for _doPostBack. Check this http://stackoverflow.com/questions/3591634/how-to-use-dopostback – Ahuman Feb 17 '15 at 14:58

1 Answers1

3

WebMethod can't interact directly with the DOM. WebMethods are outside the scope of the normal page model.

In fact, WebMethod is no longer supported by MS and you shouldn't use it. Instead, if you're on .NET 4.5 you can create a Web API and call into it with AJAX (jQuery has great helper functions for this, I see you tagged it). The Web API will return the string, and then you can use JavaScript to set the text of the textbox.

Web API

public class TestController : ApiController
{
    public string GetHello(string name)
    {
         return String.Format("Hello, {0}!", name);
    }
}

Then the jQuery AJAX code:

$.ajax({
    url: '/api/test/GetHello?name=Nade',
    type: 'GET'
})
.success(function(output){
    $('#TextBox1').val(output);
});

If you're not on .NET 4.5, upgrade! If upgrading isn't possible, then you could just set up a generic handler (.ashx) and write to the response.

mason
  • 31,774
  • 10
  • 77
  • 121
  • I am new in Java script. can you give me some example. – nade Feb 17 '15 at 14:55
  • can you do this in WebForms, I've only ever used `ApiController` in MVC? – Dave Becker Feb 17 '15 at 15:01
  • @DaveBecker Web API is perfectly usable with Web Forms on .NET 4.5. The reason you can mix things is part of the [One ASP.NET](http://www.hanselman.com/blog/OneASPNETSneakPeekElegantWebFormsAndSnowballsInHell.aspx) initiative. And as I stated, if you can't upgrade to .NET 4.5 it would be better to set up a generic handler to handle the incoming requests. – mason Feb 17 '15 at 15:02
  • @mason I never knew that, I've got a ton of `IHttpHandler` classes kicking about in an upgraded v4.0 app, maybe I'll convert them.... – Dave Becker Feb 17 '15 at 15:05