asp.net:如何使用javascript或jquery访问转发器生成的元素?

时间:2014-09-22 04:53:27

标签: javascript jquery asp.net repeater

我正在使用母版页和转发器。我需要选择性地使用必需的字段验证器。当"是"被选中(即ListItem的值为" 0" RadioButtonList,ID为" rblMayContact"然后只有那个具有ID" rfvSupervisorName"的RequiredFieldValidator应该启用(激活)。否则应禁用所需的字段验证器。

简化代码:

<asp:Repeater ID="repEmploymentHistory" runat="server" Visible="True" OnItemCommand="repEmploymentHistory_ItemCommand"
        OnItemDataBound="repEmploymentHistory_ItemDataBound" OnDataBinding="repEmploymentHistory_DataBinding">
        <ItemTemplate>
            <table id="tblEmploymentHistory" class="table bordered">
                <tr>
<td>
                        Supervisor Name<br />
                        <asp:Label ID="lblSupervisorName" runat="server" CssClass="underlined bold"><%# DataBinder.Eval(Container.DataItem, "SupervisorName")%></asp:Label>
                        <asp:TextBox ID="txtSupervisorName" runat="server" Visible="false" MaxLength="50"></asp:TextBox>
                        <asp:RequiredFieldValidator ID="rfvSupervisorName" runat="server" ControlToValidate="txtSupervisorName"
                            EnableClientScript="true" ValidationGroup="EditEmploymentHistoryGroup" ErrorMessage="Required."
                            CssClass="field-validation-error" />
                        <asp:RegularExpressionValidator ID="revSupervisorName" runat="server" ControlToValidate="txtSupervisorName"
                            ValidationGroup="EditEmploymentHistoryGroup" ValidationExpression="^[-a-zA-Z'.\s]{1,50}$"
                            ErrorMessage="Enter a valid name." CssClass="field-validation-error" />
</td>
</tr>
<tr>
                    <td>
                        <asp:Label ID="lblContactQuestion" runat="server" Text="May we contact this employer? (If no, please explain)">May we contact this employer?</asp:Label>
                        <asp:Label ID="lblContactAnswer" runat="server" CssClass="underlined bold"><%#(String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "ContactEmployer").ToString())) ? String.Empty : (bool)DataBinder.Eval(Container.DataItem, "ContactEmployer") ? "Yes" : "No" %></asp:Label>
                        <asp:RadioButtonList ID="rblMayContact" runat="server" RepeatDirection="Horizontal"
                            RepeatLayout="Flow" AutoPostBack="False" Visible="false" >
                            <asp:ListItem Value="0" onclick="ValidateSupervisor(this);return false;"> Yes </asp:ListItem>
                            <asp:ListItem Value="1"> No </asp:ListItem>
                        </asp:RadioButtonList>
                        <asp:Label ID="lblNoContactReason" runat="server" CssClass="underlined bold"><%#(String.IsNullOrEmpty(DataBinder.Eval(Container.DataItem, "ReasonNotContacting").ToString())) ? String.Empty : DataBinder.Eval(Container.DataItem, "ReasonNotContacting") %></asp:Label>
                        <asp:TextBox ID="txtNoContactReason" runat="server" MaxLength="200" Visible="false"></asp:TextBox>
                    </td>
                </tr>
 </table>
        </ItemTemplate>

难以访问重复项的元素,因为每个元素都有索引前缀。

那么,使用javascript或jquery,只有在选择了特定的单选按钮时,如何启用与文本框相关的必填字段验证器?

1 个答案:

答案 0 :(得分:0)

在Repeater的ItemDataBound中试试这个:

protected void repEmploymentHistory_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        var rblMayContact = e.Item.FindControl("rblMayContact") as RadioButtonList;
        var rfvSupervisorName = e.Item.FindControl("rfvSupervisorName") as RequiredFieldValidator;
        var revSupervisorName = e.Item.FindControl("revSupervisorName") as RegularExpressionValidator;

        if (rfvSupervisorName != null && revSupervisorName != null)
        {
            revSupervisorName.Enabled = rfvSupervisorName.Enabled = rblMayContact != null && rblMayContact.SelectedIndex >= 0;
        }
    }
}

并添加&#34; AutoPostBack&#34; RadioButtonList的属性为true:

<asp:RadioButtonList runat="server" AutoPostBack="True">
    ...
</asp:RadioButtonList>

这样,每次更改RadioButtonList时,它都会回发到页面,并且转发器的ItemDataBound启用/禁用Validator控件。