Eric Bergman
Eric Bergman

Reputation: 1443

ASP.NET: RequiredFieldValidator with Multiple TextBoxes in ListView

I have a ListView

<asp:ListView ....>
    <asp:TextBox ID="txtComment" ... />
    <asp:RequiredFieldValidator ID="rfvComment" ControlToValidate="txtComment" ... />
    <act:ValidatorCalloutExtender ID="vceComment" TargetControlID="rfvComment" ... />

    <asp:Button ID="btnAddComment" ... />

</asp:ListView>

lets say this ListView creates the following:

TextBox1 Button1

TextBox2 Button2

TextBox3 Button3

If I click on Button2 the RequiredFiledValidator/ValidatorCalloutExtender are applied to TextBox1 instead of TextBox2, if I click on Button3 the RequiredFiledValidator/ValidatorCalloutExtender are applied to TextBox1 as well, I want the RequiredFiledValidator/ValidatorCalloutExtender to apply to the TextBox next to the button, so if I click Button3 I want it to apply to TextBox3.

Does anyone know how can I achieve this?

thank you.

Upvotes: 3

Views: 14098

Answers (2)

IUnknown
IUnknown

Reputation: 22478

Use ValidationGroup property and generate it value dynamically:

<asp:TextBox runat="server" ID="TextBox1" />
<asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1" Text="*"
    ValidationGroup='<%# "validationGroup_" + Container.DataItemIndex.ToString() %>' />
<asp:Button runat="server" Text="Click Me" ValidationGroup='<%# "validationGroup_" + Container.DataItemIndex.ToString() %>' />

Add script below at very bottom of form:

<script type="text/javascript">
    var originalValidatorUpdateDisplay = ValidatorUpdateDisplay;
    ValidatorUpdateDisplay = function (val) {
        originalValidatorUpdateDisplay.call(null, val);
        var isHidden = val.style.display == "none" || val.style.visibility == "hidden";

        var extender = Sys.UI.Behavior.getBehaviorsByType(val, Sys.Extended.UI.ValidatorCalloutBehavior);
        if (extender && extender.length == 1) {
            extender = extender[0];
            if (isHidden) {
                extender.hide();
            }
            else {
                extender.show(true);
            }
        }
    }
</script>

I suppose it would be better to customize toolkit source, but I'm not in the mood to do this :) So hope this script will fix your problem

Upvotes: 4

vlukham
vlukham

Reputation: 409

try smth like (example, that should work)

<asp:Panel ID="registration" defaultbutton="regButton" runat="server">

<asp:TextBox ID="name" Rows="1" CssClass="text" runat="server" ValidationGroup="Registration">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Enter your name please" Text="*" ControlToValidate="name"  EnableClientScript="False" Display="Dynamic" ValidationGroup="Registration" />


<asp:TextBox ID="address" Rows="1" CssClass="text" runat="server" ValidationGroup="Registration"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Enter your address please" Text="*" ControlToValidate="address"  EnableClientScript="False" Display="Dynamic" ValidationGroup="Registration" />


<asp:ValidationSummary DisplayMode="BulletList" EnableClientScript="false" ID="validation_sum" runat="server"  HeaderText="Errors list"  ValidationGroup="Registration"/>


<asp:Button runat="server" id="regButton" Text="Register please" ValidationGroup="Registration" OnClick="RegisterUser"/>

</asp:Panel>

Upvotes: 0

Related Questions