Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required field validater in repeater

Tags:

asp.net

I have a repeater control and textbox in that repeater. I want a required field validator in the textbox ho can i do this

like image 264
sumit Avatar asked Jan 13 '11 07:01

sumit


People also ask

What is the use of required field validator?

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.

How do I turn off required field validator?

Well you can simple use the Enabled="false" property of RequiredFieldValidator . Your markup would look something like this based on your Question.


1 Answers

<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;
}
like image 103
volpav Avatar answered Oct 03 '22 23:10

volpav