JQuery + AJAX + ASP.Net + WebForms

I have some jQuery AJAX POSTing data for my C # WebForm backend. These are POSTs to the static WebForm method of the string, which returns a value; jQuery uses this value to change the image URL in html. Everything is alright and dandy.

However, I would like to extend the functionality of the existing code (although I do not close it for complete rewriting) to allow me to manipulate the ASP controls on the front end with the C # backend, which I cannot execute to the specified static string method, acting as my webform.

Does anyone have any ideas to help my predicament?

Backend

    [System.Web.Services.WebMethod]
    public static string ImageLoad(string address)
    {
        //if fail
        return "/Unavailable.bmp";

        //if succeed
        return "myimage.jpg";
        //third option
        else
        return "myotherimage.jpg";
    }

JQuery / AJAX

function scriptImageLoad() {
   var address = $("#txtAddress").val();
       $.ajax({
           type: "POST",
           url: "myPage.aspx/ImageLoad",
           data: "{'address':'" + address.toString() + "'}",
           contentType: "application/json; charset=utf-8",
           dataType: "text",
           success: function (output) {
                $('#imgImage').attr('src', output);
           }
       });
   }
});
+3
source share
3

. , , , - . JQuery , .

function scriptImageLoad() {
   var address = $("#txtAddress").val();
      $.ajax({
         type: "POST",
         url: "MyForm.aspx/MyMethod",
         data: "{'address':'" + address.toString() + "'}",
         contentType: "application/json; charset=utf-8",
         dataType: "JSON",

         success: function (output) {
            $.each(output, function (index, value) {
               place value in form
               return (index != 0);
            })
            $.each(output, function (index, value) {
               place value in form
               return (index != 1);
            })
            $.each(output, function (index, value) {
               place value in form
               return (index != 2);
            })
            $.each(output, function (index, value) {
               $('#imgImage').attr('src', this);
               return (index != 3);
            })
         }

    public struct TestStruct
    {
        public string value1;
        public string value2;
        public string value3;
        public string value4;

        public TestStruct(string value1, string value2, string value3, string value4)
        {
            this.value1 = value1;
            this.value2 = value2;
            this.value3 = value3;
            this.value4 = value4;
        }
    }

    [System.Web.Services.WebMethod]
    public static string[] MyMethod(string address)
    {
        string[] returnarray = new string[4];
        TestStruct struct;
        struct.value1 = "";
        struct.value2 = address;
        struct.value3 = "";
        struct.value4 = "/Unavailable.bmp";

        //if fail, return default values
                returnstring.SetValue(struct.value1, 0);
                returnstring.SetValue(struct.value2, 1);
                returnstring.SetValue(struct.value3, 2);
                returnstring.SetValue(struct.value4, 3);
                return returnstring;

        //if succeed
                struct.values = processed values;
                set the values on returnstring
                return returnstring;
        //else
                struct.values = other processed values;
                set the values on returnstring
                return returnstring;
    }

, , - JQuery .

0

WebService. jQuery .

function scriptImageLoad() {
   var address = $("#txtAddress").val();
       $.ajax({
           type: "POST",
           url: "MyService.asmx/ImageLoad",
           data: "{'address':'" + address.toString() + "'}",
           contentType: "application/json; charset=utf-8",
           dataType: "text",
           success: function (output) {
                $('#imgImage').attr('src', output);
           }
       });
   }
});

[WebService, ScriptService]
public class MyService : WebService
{
    [ScriptMethod]
    public static string ImageLoad(string address)
    {
        //if fail
        return "/Unavailable.bmp";

        //if succeed
        return "myimage.jpg";
        //third option
        else
        return "myotherimage.jpg";
    }
}
+4

- .

- asp.net .

- . .

+2

Source: https://habr.com/ru/post/1747043/


All Articles