Reputation: 369
I have a repeater that requires a validator on only some of the fields. So for instance, I have a set of TextBoxes for each field, but I would only want fields like First/Last Name as required, while Date of Birth would not be necessary. I have tried something along the lines of:
protected void repCustomerDetails_DataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
TextBox tct = e.Item.FindControl("tbColumns") as TextBox;
Label lbl = e.Item.FindControl("Label1") as Label;
RequiredFieldValidator rfv = (RequiredFieldValidator)e.Item.FindControl("rfvColumns");
if (lbl.Text != "DOB")
{
rfv.ControlToValidate = tct.ID;
}
}
}
but it's not quite working, as this validates every textbox due to the shared ID tag.
<asp:Repeater ID="repCustomerDetail" runat="server" onitemdatabound="repCustomerDetails_DataBound">
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text='<%# DataBinder.Eval(((RepeaterItem)Container).DataItem, "FieldName")%>' Width="140px"></asp:Label>
<asp:TextBox ID="tbColumns" runat="server" Width="160px" Visible='<%# (((bool)DataBinder.Eval(Container.DataItem, "IsDropDown") == false && (string)DataBinder.Eval(Container.DataItem, "Type") != "Boolean") ? true : false )%> '></asp:TextBox>
<asp:DropDownList ID="ddlField" runat="server" Width="165px" Visible='<%# ((bool)DataBinder.Eval(Container.DataItem, "IsDropDown") == true ? true : false )%> '></asp:DropDownList>
<asp:CheckBox ID="cbField" runat="server" Visible='<%# (((bool)DataBinder.Eval(Container.DataItem, "IsDropDown") == false && (string)DataBinder.Eval(Container.DataItem, "Type") == "Boolean")? true : false )%> ' />
<asp:ImageButton ID="imgbtnCalendar" runat="server" ImageUrl="~/WebResources/Images/calendar.gif" Visible='<%# ((string)DataBinder.Eval(Container.DataItem, "Type") == "DateTime" ? true : false )%> ' />
<asp:RequiredFieldValidator ID="rfvColumns" runat="server" ControlToValidate="tbColumns" ErrorMessage="* Required" ValidationGroup="Save">*</asp:RequiredFieldValidator>
<ajaxToolkit:CalendarExtender ID="CalendarExtendeeer" runat="server" Format="dd/MM/yyyy" PopupButtonID="imgbtnCalendar" TargetControlID="tbColumns" Enabled='<%# ((string)DataBinder.Eval(Container.DataItem, "Type") == "DateTime" ? true : false )%> '></ajaxToolkit:CalendarExtender>
<br id="Br1" runat="server" visible="<%# Container.ItemIndex == 3 %>" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
Upvotes: 0
Views: 149
Reputation: 4903
Try disabling the RequiredFieldValidator
for the Date of Birth field:
RequiredFieldValidator rfv = (RequiredFieldValidator)e.Item.FindControl("rfvColumns");
rfv.Enabled = lbl.Text != "DOB";
If you need to make it dynamic, you could have a "Required" property on you DataSource, then you could use it to handle the RequiredFieldValidator
enabling.
Example:
<asp:RequiredFieldValidator ID="rfvColumns" Enabled='<%# Eval("Required") %>' runat="server" ControlToValidate="tbColumns" ErrorMessage="* Required" ValidationGroup="Save" >*</asp:RequiredFieldValidator>
Upvotes: 1