I’m trying to find a way to make a asp:RegularExpressionValidor target a specific control when it is inside a asp:DataList
The asp:DataList is in the following code:
<asp:DataList ID="dlUserInputs" runat="server" DataKeyField="tagname" ItemStyle-CssClass="paddingBottom20" CssClass="layout">
<ItemTemplate>
<npo:UserInput ID="ctrlUserInput" runat="server" /> // user control
</ItemTemplate>
</asp:DataList>
the npo:UserInput contains the following code:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="UserInput.ascx.vb" Inherits="web_controls_publication_UserInput" %>
...
<asp:Label ID="lblUserInput" runat="server" Visible="false" />
<asp:TextBox ID="txtUserInput" runat="server" Visible="false" TextMode="MultiLine" Rows="10" Width="100%"/>
<asp:RegularExpressionValidator ID="RegularExpressionValidator" runat="server"
ErrorMessage="<%$ Resources:PageText, LETTER_GOODSANDSERVICESXML_WRONGFORMAT %>" ValidationExpression="^[Cc]lasss[0-9]{1,2}:s{1}[^n]*(?:r?n[Cc]lasss[0-9]{1,2}:s{1}[^n]*)*$"
ControlToValidate="txtUserInput"></asp:RegularExpressionValidator>
...
My problem is the RegularExpressionValidor validates the control txtUserInput but if my dataList calls,let’s say, 3 times this code, the validator will apply on the 3 txtUserInput occurences. I want it to apply only to the first
Thank you in advance for your advices
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Yes this can be done. First add a property to UserInput.ascx code behind. In this case Validate
public partial class UserInput : System.Web.UI.UserControl
{
public bool Validate { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
}
}
Then bind the new variable to the Visible property of the RegularExpressionValidator
<asp:RegularExpressionValidator ID="RegularExpressionValidator" Visible='<%# Validate %>'
Then to enable the first item in the DataList, set the new Visible property in the aspx containing the UserControl.
<asp:DataList ID="dlUserInputs" runat="server">
<ItemTemplate>
<npo:UserInput ID="ctrlUserInput" Validate='<%# Container.ItemIndex == 0 %>' />
</ItemTemplate>
</asp:DataList>
VB – using https://codeconverter.icsharpcode.net/
Public Partial Class UserInput
Inherits Web.UI.UserControl
Public Property Validate As Boolean
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
End Class
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0