I have a repeater control and textbox in that repeater. I want a required field validator in the textbox ho can i do this
Multiple validators can be associated with the same input control. For example, a RequiredFieldValidator can be used to ensure input to a control, while at the same time a RangeValidator can be used to ensure that the input is within a specified data range.
Well you can simple use the Enabled="false" property of RequiredFieldValidator . Your markup would look something like this based on your Question.
<asp:Repeater id="myRep" OnItemDataBound="rep_ItemDataBound" runat="server">
<ItemTemplate>
<asp:TextBox id="tx" runat="server" />
<asp:RequiredFieldValidator id="myVal" ControlToValidate="tx" ErrorMessage="Required" runat="server" />
</ItemTemplate>
</asp:Repeater>
UPDATE
Add this to code-behind (also modify the markup a bit to subscribe to an event, see above):
protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("myVal");
TextBox tb = (TextBox)e.Item.FindControl("tx");
val.ControlToValidate = tb.ID;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With