I have a repeater control and textbox in that repeater. I want a required field validator in the textbox ho can i do this
8 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; } 7 Comments
tb.ID as opposed to tb.ClientID.RequiredFieldValidator. I do not believe that the server-side validation is actually running. I know that this is an old post, but can you confirm that this is the case?If you want all textboxes in a repeater to be validated by a single button click then thats simple like this
<asp:Repeater ID="rpt" runat="server"> <ItemTemplate> <asp:TextBox ID="txt" runat="server"> </asp:TextBox> <asp:RequiredFieldValidator ID="rqf" ControlToValidate="txt" ErrorMessage="*" ValidationGroup = "TestValidationGroup" runat = "server"> </asp:RequiredFieldValidator> </ItemTemplate> </asp:Repeater> <asp:Button ID = "btnSubmit" runat = "server" ValidationGroup = "TestValidationGroup" /> No need to check for any event for databound or anything.
Comments
You can set ControlToValidate value on repeater itemdatabound.
1 Comment
Try this one
<asp:Repeater ID="rptSample" runat="server"> <ItemTemplate> Name:<br /> <asp:TextBox ID="txtName" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvName" ControlToValidate="txtName" runat="server" Display="Static" ErrorMessage="Name field cannot be left blank"></asp:RequiredFieldValidator> </ItemTemplate> </asp:Repeater> <br /> <asp:Button ID="btnSubmit" Text="Submit" runat="server" /> 1 Comment
protected void myRep_ItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { ((RequiredFieldValidator)e.Item.FindControl("myVal")).ValidationGroup = ((TextBox)e.Item.FindControl("tx")).UniqueID; } } Comments
protected void Repeater_OnItemDataBound(object sender, RepeaterItemEventArgs e) { tblData tbldata = e.Item.DataItem as tblData; RequiredFieldValidator val = (RequiredFieldValidator)e.Item.FindControl("rfv"); if (tbldata.FieldName.ToUpper().Contains("NOT NULL")) { TextBox tb = (TextBox)e.Item.FindControl("txtDATFieldName"); val.ControlToValidate = tb.ID; } else { val.Enabled = false; // disable when you dont need a validator } } Comments
I kept getting a duplicate key exception in RegisterExpandoAttribute trying to do this. I was using a UserControl inside a repeater, when "OnDataBinding" instead of "ItemDataBinding" This worked for me:
/// <summary> /// Raises the <see cref="E:System.Web.UI.Control.DataBinding" /> event. /// </summary> /// <param name="e">An <see cref="T:System.EventArgs" /> object that contains the event data.</param> protected override void OnDataBinding(EventArgs e) { base.OnDataBinding(e); foreach (Control ct in this.Controls) { BaseValidator bv = ct as BaseValidator; if (null == bv) { continue; } bv.ControlToValidate = this.FindControl(bv.ControlToValidate).ID; bv.ValidationGroup = this.ValidationGroup; } } And yes, I don't think it makes any sense either