ASP.net下拉列表意外事件触发行为

时间:2013-01-31 12:20:04

标签: asp.net vb.net ajaxcontroltoolkit

我在下拉框中遇到了一些奇怪的行为,我希望有人可以解释发生了什么。我有一个datarepeater,可以构建一个行表。每行在单元格中都有一个下拉列表(隐藏)和一个链接按钮(未隐藏)。预期的行为是,当用户单击特定单元格的链接按钮时,它会触发服务器命令。此命令执行一些操作,但也设置DDL的选定值,然后将其设置为可见并将其自身(链接按钮)隐藏。问题是每次选定的索引发生变化时都会触发下拉列表的事件。但是,最终发生的事情是它只在用户第一次更改DDL值时触发。之后,该事件将停止射击。此外,所有这些都在用户控件中,并且所有代码都在更新面板内。正如我在代码示例中提到的,如果我不更改ddl值,则在linkbutton事件中,此行为会停止,但DDL将无法设置正确的值。这是我的代码:

Linkbutton事件:

Protected Sub edit_click(ByVal sender As Object, ByVal e As EventArgs)
    Dim btn As LinkButton = CType(sender, LinkButton)
    Dim userId As String = btn.CommandArgument
    Dim ds As New List(Of myObject)
    Dim currentChoice As New myObject
    Dim parent As RepeaterItem = CType(btn.Parent, RepeaterItem)

    Dim lbl As Label = CType(parent.FindControl("userId"), Label)
    Dim ma As DropDownList = CType(parent.FindControl("ddlMustApprove"), DropDownList)
    Dim maLbl As Label = CType(parent.FindControl("mustApprove"), Label)

     'just get a list of values to chose from
    ds = Session("userNotificationDS")
    currentChoice = ds.Find(Function(p As myObject) p.User.NetworkID = userId)

    With maLbl
        .Width = 100
        .BorderColor = Drawing.Color.DarkBlue

    End With

    'if I disable this next line, everything works fine (but then the correct selection wont be chosen)
    ma.Text = currentChoice.MustApprove
    ma.CssClass = ""
    maLbl.Visible = False
End Sub

DDL更改事件:

Protected Sub selection_Change(ByVal sender As Object, ByVal e As EventArgs) 
    Dim cnt As DropDownList = CType(sender, DropDownList)
    Dim parent As RepeaterItem = CType(cnt.Parent, RepeaterItem)
    Dim maLbl As Label = CType(parent.FindControl("mustApprove"), Label)
    Dim userId As Label = CType(parent.FindControl("userId"), Label)
    Dim ds As New List(Of myObject)

    'just gets the value to set the DDL to
    ds = Session("userNotificationDS")

    For Each i As myObjectIn ds
        If (i.User.NetworkID = userId.Text) Then
            i.MustApprove = cnt.SelectedValue
        End If
    Next

    maLbl.Visible = True
    cnt.CssClass = "hidden"
    Session("userNotificationDS") = ds
    bindData(ds)
End Sub

这是前端代码中的Datarepeater:

<asp:Repeater ID="dataRepeateUsers" runat="server"  EnableViewState="false" OnItemCreated="RepeaterItemCreated" >
<HeaderTemplate> 
          .... column headers
</HeaderTemplate>  
     <ItemTemplate>  
        <tr  class="listcolor">
          <td style="border:0px; border-right:1px solid  #808080  ; border-bottom:0px solid #808080;">
            <asp:DropDownList runat="server" ID="ddlMustApprove" CssClass="hidden"  OnTextChanged="selection_Change" EnableViewState="true" AutoPostBack="true" >
                <asp:ListItem Text="True" Value="True"></asp:ListItem>
                <asp:ListItem Text="False" Value="False"></asp:ListItem>
           </asp:DropDownList>
          <asp:label ID="mustApprove" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "MustApprove") %>'></asp:label>
         </td>
       </tr>
     </ItemTemplate>
     <AlternatingItemTemplate>
        <tr  class="listcolor">
          <td style="border:0px; border-right:1px solid  #808080  ; border-bottom:0px solid #808080;">
            <asp:DropDownList runat="server" ID="ddlMustApprove" CssClass="hidden"  OnTextChanged="selection_Change" EnableViewState="true" AutoPostBack="true" >
                <asp:ListItem Text="True" Value="True"></asp:ListItem>
                <asp:ListItem Text="False" Value="False"></asp:ListItem>
           </asp:DropDownList>
          <asp:label ID="mustApprove" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "MustApprove") %>'></asp:label>
         </td>
       </tr>             
     </AlternatingItemTemplate> 
 <FooterTemplate>  
</FooterTemplate>  
    </asp:Repeater>  

我的代码是使用.Net 4.0和AjaxControlToolkit 4.1.60919在VS 2010中编写的。

由于 杰森

1 个答案:

答案 0 :(得分:0)

ma.Text = currentChoice.MustApprove

将此行替换为以下行。

if(ma.items.FindByText(currentChoice.MustApprove) != null)
{
ma.SelectedIndex = -1;
ma.items.FindByText(currentChoice.MustApprove).selected = true;
 }

只需将下拉列表的selectedindex设置为-1,如上所述。