2015-03-13 15 views
0

我有一個帶有一些linkbuttons的listview。目前,當你點擊一個按鈕時,它會觸發一個完整的回發,我想交換它以便它只是一個部分回傳。似乎它應該很簡單,但我似乎無法讓它工作,無論我做什麼。似乎我錯過了一些明顯的東西,但在這一點上,我很難過。帶Listview的異步回傳不起作用

列表視圖:

<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="False" runat="server"> 
<ContentTemplate> 
    <section id="basketbox"> 
    <hgroup class="mainhead"> 
     <h2>Your basket</h2> 
    </hgroup> 
    <asp:ListView runat="server" ID="ListView1" OnItemCommand="ListView1_ItemCommand" DataSourceID="SqlDataSource1" DataKeyNames="PartCode"> 
     <LayoutTemplate> 
      <div runat="server" id="itemPlaceholder" ></div> 
     </LayoutTemplate> 
     <ItemTemplate> 
      <h4><%#Eval("Name") %></h4> 
      <div class="quantitybox"> 
       <div class="qtylbl">Qty</div> 
       <asp:LinkButton id="QtyDown" CommandArgument='<%#Databinder.Eval(Container.DataItem,"PartCode")%>' CssClass="qtybutton" CommandName="QtyDown" runat="server"><img src="/images/minus.png"></asp:LinkButton> 
       <div class="qtybox"><%#Eval("Quantity") %></div> 
       <asp:LinkButton id="QtyUp" CommandArgument='<%#Databinder.Eval(Container.DataItem,"PartCode")%>' CssClass="qtybutton" CommandName="QtyUp" runat="server"><img src="/images/plus.png"></asp:LinkButton> 
      </div> 
     </ItemTemplate> 
    </asp:ListView> 
    </section> 
</ContentTemplate> 
<Triggers> 
    <asp:AsyncPostBackTrigger ControlID="ListView1" /> 
</Triggers> 
</asp:UpdatePanel> 

事情我已經嘗試: -ChildrenAsTriggers設置爲true(沒有區別)

- 設置觸發器是LinkBut​​ton的標識(沒有找到控制)

- 設置頁面在頁面聲明中爲異步(沒有區別)

- 將ClientIDMode設置爲AutoID ListView控件(沒有區別)

- 登記一個LinkBut​​ton使用ScriptManager(以下背後功能代碼)

Private Sub RegisterPostBackControl() 
    For Each item As ListviewItem In ListView1.Items 
     Dim lnkFull As LinkButton = TryCast(item.FindControl("QtyUp"), LinkButton) 
     ScriptManager.GetCurrent(Me).RegisterAsyncPostBackControl(lnkFull) 
    Next 
End Sub 

還是那句話:沒有什麼區別

我也試過的大多數人在某種組合彼此之間;其中沒有任何差別。

我還應該嘗試什麼?

+0

'在頁面聲明中設置頁面是異步的(沒有區別)'這根本不涉及到UpdatePanel,這是爲了異步/等待支持。 ' - 在listview控件上將ClientIDMode設置爲AutoID(沒有區別)'客戶端上的ID無關緊要。你有沒有嘗試在自己的UpdatePanel中包裝每個LinkBut​​ton?順便說一下,我建議你完全放棄UpdatePanel(因爲它很糟糕)(http://www.mikesmithdev.com/blog/asp-net-web-forms-the-drunken-lovechild-of-desktop-and-web /)),並使用AJAX和[Web API](http://www.asp.net/web-api)。 – mason 2015-03-13 15:54:21

+0

@mason我知道UpdatePanels是惡魔的衍生物 - 不幸的是,我正在開發的項目有一個截止日期太近了,我無法離開並掌握Web API。我還沒有嘗試嵌套更新面板 - 我會給它一個去。 (並感謝解釋 - 他們都是我試過的「它可能有助於」的基礎)撕掉我的頭髮後 – FrustratedWDotNet 2015-03-13 16:11:01

+0

您不*有*使用Web API,尤其是因爲它只適用於。 NET 4.5(但它是理想的)。我也不推薦ASMX,因爲該服務基本上已被棄用。如果我是你,我會做什麼,建立一些通用處理程序(.ashx)。讓他們在Web API中履行控制器的職責。你不會得到好的模型綁定的東西,你會手動需要從查詢字符串/表單值中提取值。但它應該都是直接和快速的實施。我知道使用UpdatePanel來「完成它」的誘惑,但是你必須支持這個代碼,對嗎? – mason 2015-03-13 16:45:04

回答

0

你的評論聽起來像你需要學習很多能夠使用通用處理程序來響應AJAX請求。這是不是這樣的,你可能有必要的知識

QtyDown.ashx

<%@ WebHandler Language="C#" Class="Handler" %> 

using System; 
using System.Web; 

public class QtyChangeResponse 
{ 
    public bool Success {get; set;} 
    public string Message {get; set;} 
} 

public class Handler : IHttpHandler 
{ 
    public void ProcessRequest (HttpContext context) 
    { 
     //pull values from query string 
     //update database based on values 
     //create response 
     var responseObj = new QtyChangeResponse(){Success=true, Message="Qty Updated"}; 
     context.Response.ContentType="application/json"; 
     context.Response.Write(JsonConvert.SerializeObject(responseObj));  
    } 

    public bool IsReusable 
    { get { return false; } } 
} 

VB版本(自動由Telerik的翻譯)

Imports System.Web 

Public Class QtyChangeResponse 
    Public Property Success() As Boolean 
     Get 
      Return m_Success 
     End Get 
     Set 
      m_Success = Value 
     End Set 
    End Property 
    Private m_Success As Boolean 
    Public Property Message() As String 
     Get 
      Return m_Message 
     End Get 
     Set 
      m_Message = Value 
     End Set 
    End Property 
    Private m_Message As String 
End Class 

Public Class Handler 
    Implements IHttpHandler 
    Public Sub ProcessRequest(context As HttpContext) 
     'pull values from query string 
     'update database based on values 
     'create response 
     Dim responseObj = New QtyChangeResponse() With { _ 
      Key .Success = True, _ 
      Key .Message = "Qty Updated" _ 
     } 
     context.Response.ContentType = "application/json" 
     context.Response.Write(JsonConvert.SerializeObject(responseObj)) 
    End Sub 

    Public ReadOnly Property IsReusable() As Boolean 
     Get 
      Return False 
     End Get 
    End Property 
End Class 

所以這是做同樣的事情作爲Web API控制器會大致。

<asp:LinkButton id="QtyDown" CssClass="qtybutton" runat="server" OnClientClick='QtyDown(<%#Databinder.Eval(Container.DataItem,"PartCode")%>); return false;'><img src="/images/minus.png"></asp:LinkButton> 

<script type="text/javascript"> 
function QtyDown(partCode){ 
    //use your favorite JavaScript library to gather the quantity and PartCode and make a POST call to QtyDown.ashx?qty={quantity}&partcode={partcode) 
} 
</script> 

更容易支持,比UpdatePanel少得多的bug,我已經爲你做了很多工作。

+0

1)說實話,我可能有很多東西需要了解這些 - 我對.net的知識是不完整的,很大程度上是自學的(因此是不可避免的愚蠢問題)2)不幸的是,我使用vb.net,所以ac#解決方案真的不能幫助我 – FrustratedWDotNet 2015-03-13 17:07:27

+0

@FrustratedWDotNet C#和VB是如此接近,代碼轉換器存在,做一個合理的工作。我已經使用了其中的一個,並使用相應的VB代碼更新了我的答案。不,如果您可以處理Web窗體頁面的回發並更新數據庫,則可以從通用處理程序執行相同操作。您需要的唯一附加知識就是JavaScript。 – mason 2015-03-13 17:54:04