2011-02-24 45 views
0

我喲有類似的對堆棧溢出網站的左上角的StackExchange鏈接功能。更新DIV HTML和內容與阿賈克斯

據我瞭解,在單擊堆棧交換鏈接後,下面的事情發生:

  • 隱藏的div容器中。

  • 這個div填充了它的HTML和用ajax(也許jQuery的)

我注意到,HTML和數據不會出現在頁面標記的實際數據,所以我認爲它可能是使用javascript/jquery/ajax獲取的。

一個音符 - 我使用asp.net的MVC 2和LINQ到SQL。

請給我這可怎麼來達到的,或者可能鏈接到類似的例子, 感謝例子。

+1

嗯。任何Ajax教程? – Quentin 2011-02-24 11:06:32

回答

0

您可以在後面的代碼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); 
    } 

代碼背後:

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