通过DataList中的DropDownList selectedValue启用RequiredFieldValidator

时间:2019-03-29 20:26:38

标签: asp.net vb.net html-select datalist requiredfieldvalidator

如果dropdownlist的选定值= 0,我正在尝试针对文本框启用必需的字段验证器。这都是在DataList内部发生的。

这是针对我们的Intranet的,带有带有ASP / VB.net网页的SQL和IIS服务器。我已经包含了我的代码,没有使RFV正常运行的任何运气。这是循环遍历DataList的vb代码:

  Dim strID As String
        Dim strComply As String
        Dim dlItem As DataListItem
        Dim strComment1 As String

        For Each dlItem In dlWeek1.Items
            Dim ddlComply1 As DropDownList = CType(dlItem.FindControl("ddlComply1"), DropDownList)
            Dim txtComment1 As TextBox = CType(dlItem.FindControl("txtComment1"), TextBox)
            Dim RequiredFieldValidator1 As RequiredFieldValidator = CType(dlItem.FindControl("RequiredFieldValidator1"), RequiredFieldValidator)
            Dim strLanID As String = lblLanID.Text
            Dim strTimestamp As Date = Now()

            strComply = ddlComply1.SelectedItem.Value
            strComment1 = txtComment1.Text
            strID = dlWeek1.DataKeys(dlItem.ItemIndex).ToString()

            If strComply = 0 Then
                RequiredFieldValidator1.Enabled = True
            Else
                RequiredFieldValidator1.Enabled = False
            End If

            With myCmd.Parameters
                .Add(New SqlParameter("@Comply", strComply))
                .Add(New SqlParameter("@comment", strComment1))
                .Add(New SqlParameter("@LanID", strLanID))
                .Add(New SqlParameter("@Timestamp", strTimestamp))
                .Add(New SqlParameter("@ID", strID))
            End With

            myCmd.ExecuteNonQuery()
            myCmd.Parameters.Clear()
        Next dlItem

这是一些相关的aspx代码:

  <asp:DataList runat="server" ID="dlWeek1" DataKeyField="wklyCompID">
                <ItemTemplate>
                    <table>
                    <tr>
                        <td>
                            <b>Contract: &nbsp;</b>
                        </td>
                        <td>
                            <%# DataBinder.Eval(Container.DataItem, "contractNumber") %>
                        </td>
                        <td>
                            <b>Compliance:</b>
                        </td>
                        <td>
                            <asp:Label ID="lblComply1" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "ComplianceDescription")%>' />
                        </td>
                        <td>
                            <asp:DropDownList runat="server" ID="ddlComply1" AutoPostBack="false">
                                <asp:ListItem Selected="true" Value="7">-- Select --</asp:ListItem>
                                <asp:ListItem Value="1">Verified Performance</asp:ListItem>
                                <asp:ListItem Value="0">Non Performance</asp:ListItem>
                                <asp:ListItem Value="2">Low Stock</asp:ListItem>
                            </asp:DropDownList>
                        </td>
                        <td rowspan="3" valign="top">
                            <asp:TextBox ID="txtComment1" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtComment1" Enabled="False" ErrorMessage="* Comments are required for &quot;Non Performance&quot;" ForeColor="Maroon"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                 </table>
             </ItemTemplate>
 </DataList>

0 个答案:

没有答案