简体   繁体   English

如何在ASP.NET C#中使用jquery绑定ul标签

[英]How to Bind ul tag using jquery in asp.net C#

I am working on an asp.net application where i have to bind the ul tag using the jquery. 我正在一个asp.net应用程序上,我必须使用jquery绑定ul标签。 Inside the ul tag i want to bind images coming from the database. 在ul标签内,我想绑定来自数据库的图像。 Here is my ul tag in the application 这是我在应用程序中的ul标签

<ul class="qv_carousel d_inline_middle" id="ulProductImages">
</ul>

Here is my code in jquery to bind the ul tag 这是我在jquery中绑定ul标签的代码

function BindImage(id) {

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Default.aspx/SelectProductImages",
        data: "{'UID':'" + id + "'}",
        dataType: "json",
        async: true,
        success: OnImageSuccess,
        error: OnImageError,
        failure: function (response) {
            alert('Fail');
        }
    });
    function OnImageSuccess(response) {

        var ulimage = document.getElementById("ulProductImages");

        $.each(response.d, function (key, value) {                
            ulimage.append("<li></li>").append("<img src=" + value.ProductImage + " style=width:50px height:50px >");
        })

    }
    function OnImageError(response) {
        alert(response.d);
    }
}

this is the webmethod i have used call the data from the database 这是我用来从数据库调用数据的网络方法

[System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod(enableSession: true)]
    public static ProductImages[] SelectProductImages(string UID)
    {
        DataTable dt = new DataTable();
        List<ProductImages> details = new List<ProductImages>();

        //HtmlAnchor a = new HtmlAnchor();
        //a.InnerHtml

        dt = new ProductImages().SelectByProduct(Convert.ToInt64(UID));

        foreach (DataRow dtrow in dt.Rows)
        {
            ProductImages objProductImage = new ProductImages();
            objProductImage.ProductImage = dtrow["ProductImage"].ToString();
            details.Add(objProductImage);
        }
        return details.ToArray();
    }

When i have placed a debugger around the above webmethod then the controller goes there and fetch data from data from data from successfully. 当我在上面的web方法周围放置了调试器后,控制器将转到那里并从成功的数据中获取数据。 And then it goes to OnImageSuccess method inside jquery but the ulProductImages tag is getting bind. 然后转到jquery内的OnImageSuccess方法,但ulProductImages标记已绑定。 Plese help me someone here. 请在这里帮助我。

First of all jQuery append method works on jQuery object but you are returning a raw Javascript object by document.getElementById("ulProductImages") . 首先,jQuery append方法适用于jQuery对象,但是您将通过document.getElementById("ulProductImages")返回原始Javascript对象。

So first you should fetch that with jQuery instead: 因此,首先您应该使用jQuery来获取它:

var ulimage = $("#ulProductImages");

Next, you should have your image between the li tags: 接下来,您应该将图像放在li标签之间:

ulimage.append("<li><img src=" + value.ProductImage + " style=width:50px height:50px ></li>");

The problem is with your .append method which appends img contents to #ulProductImages when you say .append('<li></li>').append('..') . 问题在于您的.append方法,当您说.append('<li></li>').append('..')时,该方法会将img内容附加到#ulProductImages Also try to convert element you got from document.getElementById to jquery object. 还尝试将您从document.getElementById获得的元素转换为jquery对象。 So as an alternative you can do it as below: 因此,您也可以按照以下步骤进行操作:

function OnImageSuccess(response) {
   var ulimage = document.getElementById("ulProductImages");
   $.each(response.d, function (key, value) {                
       var li=$("<li/>").append("<img src=" + value.ProductImage + " style=width:50px height:50px >"); 
       //assign it to li variable
       $(ulimage).append(li); //Convert to jquery object and append
   });
}

In this line you are assigning a plain javascript object to a variable - 在这一行中,您将普通的javascript对象分配给变量-

var ulimage = document.getElementById("ulProductImages");

However in this line you are trying to use that variable as if it were a jQuery object - 但是,在这一行中,您尝试使用该变量,就像它是jQuery对象一样-

ulimage.append("<li></li>").append("<img src=" + value.ProductImage + " style=width:50px height:50px >");

Change your variable declaration to the following to correct that - 将变量声明更改为以下内容以更正该错误-

var ulimage = $("#ulProductImages")

However as Rahul Singh points out your list is still malformed. 但是,正如Rahul Singh指出的那样,您的列表仍然格式错误。 I personally would change the second line to - 我个人将第二行更改为-

ulimage.append($("<li>").append("<img>", {
   src : value.ProductImage,
   style : "width:50px height:50px"
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM