jQuery的AJAX + + ASP.Net +的WebForms [英] Jquery + AJAX + ASP.Net + WebForms

查看:110
本文介绍了jQuery的AJAX + + ASP.Net +的WebForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些jQuery AJAX发布数据,以我的后端C#web窗体。该职位返回一个值的静态字符串的WebForm法,jQuery使用该值来改变图像的URL中的HTML。一切都很好,很正常。

不过,我想扩大现有code中的功能(虽然我不是拒之门外干脆重写的话)让我来操作前端ASP从C#后端,我可以控制由于该静态字符串方法充当我的web窗体不会做。

没有人有任何想法,以帮助我的predicament?

后端

  [System.Web.Services.WebMethod]
    公共静态字符串ImageLoad(串地址)
    {
        //如果失败
        返回/Unavailable.bmp;

        //如果成功
        返回myImage.jpg这个参数;
        //第三个选项
        其他
        返回myotherimage.jpg;
    }
 

JQuery的/ AJAX

 函数scriptImageLoad(){
   。VAR地址= $(#txtAddress)VAL();
       $阿贾克斯({
           键入:POST,
           网址:myPage.aspx / ImageLoad
           数据:{地址:+ address.toString()+'},
           的contentType:应用/ JSON的;字符集= UTF-8,
           数据类型:文本,
           成功:函数(输出){
                $('#imgImage)ATTR('src'中,输出)。
           }
       });
   }
});
 

解决方案

与同事的帮助下,我制定了不同的解决方案。的结构保持输出数据我的愿望,是则转换为一个字符串数组,然后通过静态字符串Web方法返回。一旦返回到Jquery的,该数据被推到需要的地方去的阵列的不同的索引

前端

 函数scriptImageLoad(){
   。VAR地址= $(#txtAddress)VAL();
      $阿贾克斯({
         键入:POST,
         网址:MyForm.aspx /的MyMethod
         数据:{地址:+ address.toString()+'},
         的contentType:应用/ JSON的;字符集= UTF-8,
         数据类型:JSON,

         成功:函数(输出){
            $每个(输出功能(指数值){
               在形式上位值
               回报(指数!= 0);
            })
            $每个(输出功能(指数值){
               在形式上位值
               回报(指数= 1!);
            })
            $每个(输出功能(指数值){
               在形式上位值
               回报(指数= 2!);
            })
            $每个(输出功能(指数值){
               $('#imgImage)ATTR('src'中,这一点)。
               回报(指数= 3!);
            })
         }
 

后端

 公共结构TestStruct
    {
        公共字符串值1;
        公共字符串值2;
        公共字符串值3;
        公共字符串值4;

        公共TestStruct(字符串值1,字符串值2,字符串值3,字符串值4)
        {
            this.value1 =值;
            this.value2 =值;
            this.value3 =值3;
            this.value4 =值4;
        }
    }

    [System.Web.Services.WebMethod]
    公共静态字符串[]的MyMethod(串地址)
    {
        字符串[] returnarray =新的字符串[4];
        TestStruct结构;
        struct.value1 =;
        struct.value2 =地址;
        struct.value3 =;
        struct.value4 =/Unavailable.bmp;

        //如果失败,返回默认值
                returnstring.SetValue(struct.value1,0);
                returnstring.SetValue(struct.value2,1);
                returnstring.SetValue(struct.value3,2);
                returnstring.SetValue(struct.value4,3);
                返回returnstring;

        //如果成功
                struct.values​​ =处理后的值;
                设置returnstring值
                返回returnstring;
        //其他
                struct.values​​ =其他处理的值;
                设置returnstring值
                返回returnstring;
    }
 

现在我唯一的问题是获得jQuery来正确显示数据到客户端。

I have some JQuery AJAX POSTing data to my backend C# WebForm. It POSTs to a static string WebForm method which returns a value, the JQuery uses that value to change an image url in the html. All is fine and dandy.

However, I would like to expand the functionality of the existing code (though I'm not shut out to rewriting it altogether) to allow me to manipulate the front end ASP controls from the C# backend, which I can not do due to the said 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);
           }
       });
   }
});

解决方案

With the help of a colleague, I developed a different solution. A structure holds the output data I desire, it is then transfered into a string array, then returned via the static string web method. Once returned to the Jquery, the data is pushed to where it needs to go by the different indexes of the array.

Front end

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);
            })
         }

Back end

    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;
    }

Now the only issues I have is getting the Jquery to properly display the data to the client.

这篇关于jQuery的AJAX + + ASP.Net +的WebForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆