我在使用下拉框时遇到了一些奇怪的行为,我希望有人能解释发生了什么。我有一个构建行表的数据中继器。每行在一个单元格中都有一个下拉列表(隐藏)和一个链接按钮(未隐藏)。预期的行为是,当用户单击特定单元格的链接按钮时,它会触发服务器命令。该命令做了一些事情,但也设置了 DDL 的选定值,然后将其设置为可见并将其自身(链接按钮)设置为隐藏。问题是下拉列表的事件应该在每次所选索引更改时触发。但是,最终发生的是它只在用户第一次更改 DDL 值时触发。之后,事件将停止触发。此外,所有这些都在用户控件中,所有代码都在更新面板中。正如我在代码示例中提到的,如果我不更改 ddl 值,在链接按钮事件中,此行为会停止,但 DDL 不会设置正确的值。这是我的代码:
链接按钮事件:
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 中编写的。
谢谢杰森