gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:46:54 24 4
gpt4 key购买 nike

我正在使用母版页和中继器。我需要选择性地使用所需的字段验证器。当选择“Yes”时(即 ID 为“rblMayContact”的 RadioButtonList 的值为“0”的 ListItem,则且只有 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,如何仅在选择特定单选按钮时启用与文本框相关的必填字段验证器?

最佳答案

在 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;
}
}
}

并将 RadioButtonList 的“AutoPostBack”属性添加为 true:

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

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

关于javascript - asp.net:如何使用 javascript 或 jquery 访问转发器生成的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25966899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com