2010-08-26 44 views
0

嗯,我有一个奇怪的问题。DropDownList当异步回发发生时触发AutoPostback

我有一个页面上有2个DropDownLists和一个自定义的Web用户控件。自定义Web用户控件在其中包含一个UpdatePanel,并且在UpdatePanel中有一个Ajax Timer控件,用于定期更新一系列内容。

当DropDownList中的一个DropDownList并将鼠标悬停在(而不是点击)某个选项上时,UpdatePanel中的Timer控件异步回发到服务器,DropDownList将“autopostbacks”传递给服务器!

我想弄清楚为什么异步回发会导致DropDownList的行为,就像我选择/点击一个选项,以便我可以找到解决此问题的方法。

现在很容易重现此问题。 创建一个名为“TimerUpdatedListing” Web用户控制......这是Web用户控件的代码ASPX标记:

<%@ Control Language="vb" AutoEventWireup="false" CodeBehind="TimerUpdatedListing.ascx.vb" Inherits="MyNamespace.TimerUpdatedListing" %> 
<div style="width: 150px; height: 150px; overflow: auto; border: solid 1px navy;"> 
    <asp:UpdatePanel ID="anUpdatePanel" runat="server"> 
     <ContentTemplate> 
      <asp:Repeater ID="aRepeater" runat="server"> 
       <ItemTemplate> 
        <div style="border-bottom: solid 1px #EEC900; margin: 3px; padding: 2px;"> 
         Id: 
         <%#Eval("Id")%> 
         <br /> 
         Time: 
         <%#Eval("Time")%> 
        </div> 
       </ItemTemplate> 
      </asp:Repeater> 
      <asp:Timer ID="aTimer" runat="server" Interval="2000"> 
      </asp:Timer> 
     </ContentTemplate> 
    </asp:UpdatePanel> 
</div> 

这是Web用户控件VB.NET服务器端代码:

Public Partial Class TimerUpdatedListing 
    Inherits System.Web.UI.UserControl 

    Private _aListOFThings As List(Of Things) 

    Private Sub aTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles aTimer.Tick 
     If Session("_aListOfThings") Is Nothing Then 
      _aListOFThings = New List(Of Things) 
     Else 
      _aListOFThings = CType(Session("_aListOfThings"), List(Of Things)) 
     End If 

     If _aListOFThings.Count > 9 Then 
      _aListOFThings = New List(Of Things) 
     End If 

     _aListOFThings.Add(New Things((_aListOFThings.Count + 1).ToString, Now.ToString("hh:mm:ss"))) 
     Session("_aListOfThings") = _aListOFThings 
     aRepeater.DataSource = _aListOFThings 
     aRepeater.DataBind() 
    End Sub 

    Private Class Things 
     Private _time As String 
     Private _id As String 
     Public Property Time() As String 
      Get 
       Return _time 
      End Get 
      Set(ByVal value As String) 
       _time = value 
      End Set 
     End Property 
     Public Property ID() As String 
      Get 
       Return _id 
      End Get 
      Set(ByVal value As String) 
       _id = value 
      End Set 
     End Property 
     Public Sub New(ByVal id As String, ByVal time As String) 
      _id = id 
      _time = time 
     End Sub 
    End Class 
End Class 

现在,在名为WebForm1.aspx的一个ASPX页面,加入2个DropDownLists和Web用户控件:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="MyNamespace.WebForm1" %> 

<%@ Register Src="TimerUpdatedListing.ascx" TagName="TimerUpdatedListing" TagPrefix="uc1" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
    <title>Test</title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true"> 
      <asp:ListItem Text="1" Value="1" /> 
      <asp:ListItem Text="2" Value="2" /> 
      <asp:ListItem Text="3" Value="3" /> 
      <asp:ListItem Text="4" Value="4" /> 
      <asp:ListItem Text="5" Value="5" /> 
     </asp:DropDownList> 
     <asp:Label ID="selectedValue1" runat="server"></asp:Label> 
     <br /> 
     <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true"> 
      <asp:ListItem Text="a" Value="a" /> 
      <asp:ListItem Text="b" Value="b" /> 
      <asp:ListItem Text="c" Value="c" /> 
      <asp:ListItem Text="d" Value="d" /> 
      <asp:ListItem Text="e" Value="e" /> 
     </asp:DropDownList> 
     <asp:Label ID="selectedValue2" runat="server"></asp:Label> 
     <br /> 
     <br /> 
     <uc1:TimerUpdatedListing ID="TimerUpdatedListing1" runat="server" /> 
    </div> 
    </form> 
</body> 
</html> 

下面是WebForm1.aspx的VB.NET的服务器端代码页:

Public Partial Class WebForm1 
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     If String.IsNullOrEmpty(Request.Params("ddl1")) = False Then 
      selectedValue1.Text = Request.Params("ddl1") 
     End If 
     If String.IsNullOrEmpty(Request.Params("ddl2")) = False Then 
      selectedValue2.Text = Request.Params("ddl2") 
     End If 
    End Sub 

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged 
     Response.Redirect(Request.Url.LocalPath + "?ddl1=" + DropDownList1.SelectedValue.ToString, True) 
    End Sub 

    Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged 
     Response.Redirect(Request.Url.LocalPath + "?ddl2=" + DropDownList2.SelectedValue.ToString, True) 
    End Sub 
End Class 

感谢,

-Frinny

+0

你能发表一些你的代码吗? – 2010-08-26 19:08:52

+0

说真的很简单。我在页面上有2个DropDownLists。我有一个UpdatePanel的用户控件...在UpdatePanel中有一个Repeater和一个Timer控件(当计时器滴答时更新的中继器)... *叹*用简单的代码更新我的原始帖子。 – Frinavale 2010-08-26 19:16:01

+0

您意识到更新面板内的所有内容都会“回传”给对方吗?如果你需要单独控制,我建议使用像[Telerik RadAjaxManager](http://demos.telerik.com/aspnet-ajax/ajax/examples/manager/firstlook/defaultcs.aspx) – 2010-08-26 19:28:29

回答

0

我尝试了两种不同的解决方案来解决这个问题。

我做的第一件事是检查Request.Params(“__ EVENTTARGET”)以查看它是否与DropDownList匹配。如果匹配,那么我会调用Response.Redirect()方法。

例如:

Public Partial Class WebForm1 
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     If String.IsNullOrEmpty(Request.Params("ddl1")) = False Then 
      selectedValue1.Text = Request.Params("ddl1") 
     End If 
     If String.IsNullOrEmpty(Request.Params("ddl2")) = False Then 
      selectedValue2.Text = Request.Params("ddl2") 
     End If 
    End Sub 

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged 
     Dim ctrlNameThatCausedPostback As String = Request.Params("__EVENTTARGET") 
     If String.IsNullOrEmpty(ctrlNameThatCausedPostback) = False AndAlso Page.FindControl(ctrlNameThatCausedPostback) Is DropDownList1 Then 
      Response.Redirect(Request.Url.LocalPath + "?ddl1=" + DropDownList1.SelectedValue.ToString, True) 
     End If 
    End Sub 

    Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged 
     Dim ctrlNameThatCausedPostback As String = Request.Params("__EVENTTARGET") 
     If String.IsNullOrEmpty(ctrlNameThatCausedPostback) = False AndAlso Page.FindControl(ctrlNameThatCausedPostback) Is DropDownList2 Then 
      Response.Redirect(Request.Url.LocalPath + "?ddl2=" + DropDownList2.SelectedValue.ToString, True) 
     End If 
    End Sub 
End Class 

我注意到,这并不总是工作。有时候我在DropDownList中选择了一个选项,并且重定向不会发生,因为计时器滴答和选定的索引更改事件同时发生。

所以我采取的第二种方法是检查页面是否在异步回发到服务器。如果是这样,那么我知道这是计时器滴答事件发生,并且重定向不应该发生。

例如:

Public Partial Class WebForm1 
    Inherits System.Web.UI.Page 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     If String.IsNullOrEmpty(Request.Params("ddl1")) = False Then 
      selectedValue1.Text = Request.Params("ddl1") 
     End If 
     If String.IsNullOrEmpty(Request.Params("ddl2")) = False Then 
      selectedValue2.Text = Request.Params("ddl2") 
     End If 
    End Sub 

    Private Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged 
     If ScriptManager.GetCurrent(Me.Page).IsInAsyncPostBack = False Then 
      Response.Redirect(Request.Url.LocalPath + "?ddl1=" + DropDownList1.SelectedValue.ToString, True) 
     End If 
    End Sub 

    Private Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged 
     If ScriptManager.GetCurrent(Me.Page).IsInAsyncPostBack = False Then 
      Response.Redirect(Request.Url.LocalPath + "?ddl2=" + DropDownList2.SelectedValue.ToString, True) 
     End If 
    End Sub 
End Class 

这帮助,并重定向将适当比前一种方法更好发生的可能性;但是,它仍然不是100%。

-Frinny

0

下面是使用触发器触发您更新面板的一个例子。把timer.tick事件作为触发,并看看会发生什么

http://www.asp.net/ajax/tutorials/understanding-asp-net-ajax-updatepanel-triggers

PS:我不认为计时器已被列入更新面板内。实际上,我在代码隐藏方面构建了我的计时器,并将它们一起排除在标记之外。

+0

感谢您的回复。 我不需要为此解决方案使用触发器。 回复PS:您可以在UpdatePanel中拥有一个计时器,您只需要注意,无论最后一次请求是否完成,计时器控件都会每隔X毫秒返回服务器。如果计时器控件在最后一个请求返回到浏览器之前进行了滴答/回传,那么您可能会遇到UpdatePanel“始终在更新”的情况。注意这一点,你会没事的。 – Frinavale 2010-08-26 21:04:12