简体   繁体   English

使用Ajax更新div html和内容

[英]Update div html and content with ajax

I would yo have a functionality similar to the StackExchange link on the top left of the Stack Overflow site. 我将拥有类似于Stack Overflow网站左上方的StackExchange链接的功能。

As I understand it, after the stack exchange link is clicked, the following things happen: 据我了解,单击堆栈交换链接后,会发生以下情况:

  • the hidden div container is shown. 显示了隐藏的div容器。

  • this div is populated with its html and the actual data using ajax (maybe jquery) 这个div使用ajax填充其html和实际数据(也许是jquery)

I've noticed that the html and data does not appear in the page markup, so I think it is probably fetched using javascript/jquery/ajax. 我注意到html和数据没有出现在页面标记中,因此我认为可能是使用javascript / jquery / ajax来获取的。

one note - I'm using asp.net mvc 2 and linq-to-sql. 一个注意事项-我正在使用asp.net mvc 2和linq-to-sql。

Please give me examples on how this can be acheived, or maybe links to similar examples, thanks. 请给我有关如何实现此目标的示例,或者可能链接到类似示例,谢谢。

You can achieve this with jQuery and page methods in the code behind. 您可以在后面的代码中使用jQuery和页面方法来实现此目的。

//Gets the list of requests
function getRequestList() {
    // call server-side webmethod using jQuery
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "Index.aspx/GetOrdersForApproving",
        data: "{ }", // send an empty object for calls with no parameters
        dataType: "json",
        success: displayRequests,
        failure: reportError
    });
}

//displays the requests in the ul
function displayRequests(result) {
    // ASP.NET encapsulates JSON responses in a property "d"
    if (result.hasOwnProperty("d")) { result = result.d; }
    // iterate through player list and add info to the markup
    var ul = $("#requestsForApproval");
    for (i = 0; i < result.length; i++) {

        var li = $("<li class='approvalListItem'><div>"
    + "<h3>" + result[i].OrderID + " - " + result[i].Supplier + "</h3>"
+ "</div>"
+ "<div>"
    + result[i].Description
+ "</div>"
+ "<div> "
    + "<table width='100%'>"
        + "<tr>"
            + "<td>"
                 + "Quant: " + result[i].Quantity
            + "</td>"
            + "<td>"
                + "Price: " + result[i].UnitPrice
            + "</td>"
            + "<td>"
                + "Total: " + result[i].Value
            + "</td>"
        + "</tr>"
    + "</table>"
+ "</div>"
  + " <div class='approvalButtons' style='display:none'>"
    + "<ul><li class='approveButton'>Approve</li>"
    + "<li class='rejectButton'>Reject</li></ul>"
+ "</div>"
+ "<input type='hidden' class='hiddenID' name='OrderLineID' value='" + result[i].OrderLineID + "'>"
+ "</li>");
        ul.append(li);
    }

Code Behind: 背后的代码:

/// <summary>
/// Gets a list of Request Lines
/// </summary>
/// <returns>List of order lines</returns>
[WebMethod]
public static List<iOrderLine> GetOrdersForApproving()
{
    try
    {
        List<iOrderLine> Lines = new List<iOrderLine>();
        foreach (Objects.Database.OrderLine oOrderLine in Objects.Database.OrderLine.GetLinesWaitingFor(StaticStore.CurrentUser.UserID, int.MinValue))
        {
            Lines.Add(new iOrderLine(oOrderLine));
        }

        return Lines;
    }
    catch (Exception)
    {
        throw;
    }

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

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