3

我有一个列表视图

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

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

</asp:ListView>

假设这个 ListView 创建了以下内容:

文本框 1 按钮 1

文本框 2 按钮 2

文本框 3 按钮 3

如果我单击 Button2,RequiredFiledValidator/ValidatorCalloutExtender 将应用于 TextBox1 而不是 TextBox2,如果我单击 Button3,RequiredFiledValidator/ValidatorCalloutExtender 也将应用于 TextBox1,我希望 RequiredFiledValidator/ValidatorCalloutExtender 应用于按钮旁边的 TextBox,所以如果我单击 Button3,我希望它适用于 TextBox3。

有谁知道我怎样才能做到这一点?

谢谢你。

4

2 回答 2

4

使用 ValidationGroup 属性并动态生成它的值:

<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() %>' />

在表单的最底部添加以下脚本:

<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>

我想自定义工具包源会更好,但我没有心情这样做:) 所以希望这个脚本能解决你的问题

于 2012-11-05T20:29:19.327 回答
0

试试 smth like (例如,应该可以)

<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>
于 2012-11-05T20:27:54.027 回答