我有一个包含许多链接按钮的列表视图。此时,当您单击一个按钮时,它会触发一个完整的回发,我想交换它,以便它只是部分回发。似乎它应该足够简单,但无论我做什么,我似乎无法让它发挥作用。似乎我错过了一些明显的东西,但此时此刻,我很难过。
列表视图:
<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(没有区别)
- 将触发器设置为链接按钮ID(未找到控件)
- 在页面声明中设置页面为异步(没有区别)
- 在listview控件上将ClientIDMode设置为AutoID(没有区别)
- 使用scriptmanager(下面的代码隐藏代码)注册一个linkbutton
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 :(得分: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更容易支持,更不用说了,我已经为你完成了很多工作。