ASP.NET 中嵌套 ListView 中的 ValidationGroup 问题
我的 ASP.NET 网页上有一个嵌套的 ListView,内部 ListView 有一个 InsertItem 模板,我必须在其中验证用户条目并提供警告(使用 requiredValidator 和 RegExpValidator 以及 AJAX Extenders 来添加调出)。不幸的是,由于 InsertTemplate 是为每个绑定项复制的,因此 ValidationGroup 对于每个独立的内部 ListView 保持相同,并且验证需要在页面上所有 ListView 中的所有字段上输入内容,而不仅仅是从当前的 InsertItem 模板中输入。
知道如何将 ValidationProperty 设置为动态值吗?
这是我的 ASP.NET 页面代码(经过纯化并归结为问题):
<asp:ListView ID="lstCatQualis" runat="server" ItemPlaceholderID="listQualiCats">
</LayoutTemplate>
<ItemTemplate>
</tr>
<asp:ListView ID="listQualiItems" ItemPlaceholderID="itemPlaceHolder" runat="server" DataSource='<%# Eval("Qualis") %>' InsertItemPosition="LastItem">
<InsertItemTemplate>
<tr id="quali" runat="server">
<td class="first">
</td>
<td class="qualirating">
<asp:TextBox ID="txtQualiName" runat="server" Width="250" TabIndex='1' />
<asp:RequiredFieldValidator ID="rv1" runat="server" ControlToValidate="txtQualiName"
Display="None" ErrorMessage="Bitte geben Sie Ihre Qualifikation ein."
ValidationGroup="quali"></asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="vco19" runat="server" TargetControlID="rv1">
</asp:ValidatorCalloutExtender>
</td>
<td class="qualirating">
<asp:TextBox ID="txtJahreErfahrung" runat="server" TabIndex='1' />
<asp:RequiredFieldValidator ID="rv2" runat="server" ControlToValidate="txtJahreErfahrung"
Display="None" ErrorMessage="Bitte tragen Sie die Anzhal der Jahre ein."
ValidationGroup="quali"></asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="vco2" runat="server" TargetControlID="rv2">
</asp:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="ev2" runat="server" ErrorMessage="Geben Sie bitte eine Zahl ein."
Display="None" ControlToValidate="txtJahreErfahrung" ValidationExpression="[1-9][0-9]{0,3}"
ValidationGroup="quali"></asp:RegularExpressionValidator>
<asp:ValidatorCalloutExtender ID="vcoe2" runat="server" Enabled="True" TargetControlID="ev2">
</asp:ValidatorCalloutExtender>
</td>
<td class="qualilabel">
<asp:button ID="btnAddCatQuali" OnClick="btnAddCatQuali_Clicked" runat="server" text='Hinzufügen' TabIndex='1' ValidationGroup="quali" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
I have a nested ListView on my ASP.NET Webpage, the inner ListView has an InsertItem template where I have to validate the user entries and provide warnings (using RequiredValidator and RegExpValidator and AJAX Extenders to add the call-outs). Unfortunately since the InsertTemplate is replicated for every bound item, the ValidationGroup remains the same for every independent inner ListView and the validation requires entry on all fields in all ListViews on the page, not only from the current InsertItem template.
Any idea how I can probably set the ValidationProperty to a dynamic value?
Here is (a purified and boiled-down to the problem) piece of my ASP.NET page code:
<asp:ListView ID="lstCatQualis" runat="server" ItemPlaceholderID="listQualiCats">
</LayoutTemplate>
<ItemTemplate>
</tr>
<asp:ListView ID="listQualiItems" ItemPlaceholderID="itemPlaceHolder" runat="server" DataSource='<%# Eval("Qualis") %>' InsertItemPosition="LastItem">
<InsertItemTemplate>
<tr id="quali" runat="server">
<td class="first">
</td>
<td class="qualirating">
<asp:TextBox ID="txtQualiName" runat="server" Width="250" TabIndex='1' />
<asp:RequiredFieldValidator ID="rv1" runat="server" ControlToValidate="txtQualiName"
Display="None" ErrorMessage="Bitte geben Sie Ihre Qualifikation ein."
ValidationGroup="quali"></asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="vco19" runat="server" TargetControlID="rv1">
</asp:ValidatorCalloutExtender>
</td>
<td class="qualirating">
<asp:TextBox ID="txtJahreErfahrung" runat="server" TabIndex='1' />
<asp:RequiredFieldValidator ID="rv2" runat="server" ControlToValidate="txtJahreErfahrung"
Display="None" ErrorMessage="Bitte tragen Sie die Anzhal der Jahre ein."
ValidationGroup="quali"></asp:RequiredFieldValidator>
<asp:ValidatorCalloutExtender ID="vco2" runat="server" TargetControlID="rv2">
</asp:ValidatorCalloutExtender>
<asp:RegularExpressionValidator ID="ev2" runat="server" ErrorMessage="Geben Sie bitte eine Zahl ein."
Display="None" ControlToValidate="txtJahreErfahrung" ValidationExpression="[1-9][0-9]{0,3}"
ValidationGroup="quali"></asp:RegularExpressionValidator>
<asp:ValidatorCalloutExtender ID="vcoe2" runat="server" Enabled="True" TargetControlID="ev2">
</asp:ValidatorCalloutExtender>
</td>
<td class="qualilabel">
<asp:button ID="btnAddCatQuali" OnClick="btnAddCatQuali_Clicked" runat="server" text='Hinzufügen' TabIndex='1' ValidationGroup="quali" />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
</ItemTemplate>
</asp:ListView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
添加一个
ItemDataBound
事件处理程序,并在其中搜索每个验证器并设置使用项目 id 生成的 ValidationGroup:使用递归函数,您可以扩展代码以查找 Validator 的每个子类。
Add an
ItemDataBound
event handler and inside it, search for each validator and set a ValidationGroup generated with the id of the item:With a recursive function you can extend the code to look for every subclass of Validator.