6

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 8

8
<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; } 
Sign up to request clarification or add additional context in comments.

7 Comments

Could you elaborate? What is exactly not working? It works fine for me (just tested). Don't you get an error messages when you try to submit and empty field?
Don't ask me why, but this only works for me if I use tb.ID as opposed to tb.ClientID.
@ngm I think you're right because I'd assume that the validator will try to find the control within the current INamingContainer (which is in this case Repeater). I've updated my post.
error: Unable to find control id 'tx' referenced by the 'ControlToValidate' property of 'myVal' .. What can I do solve this?
I have tested this myself and from what I can tell it looks like the only validation that is running is the client-side validation script registered by the 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?
|
5

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

2

You can set ControlToValidate value on repeater itemdatabound.

1 Comment

onitemdatabound get textbox.clientid and than set this your validators control attribute...
1

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

Can u explain me how u implement this solution on your side.Its working fine on my side(TESTED).If u click on submit button it display error msg alongside textbox control.Can u write your code in question.
0
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

0
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

0

Maybe you want have a validation per row... Set the validation group to a group per row like this

ValidationGroup='<%# "gropname" + Eval("Id") %>' 

do this in the validator, textbox and the button

HTH G.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.