Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a usercontrol say AddressSearchControl.ascx, applied on a page. There is a textbox named Extended Zip code. I have applied reqular expression on this in usercontrol now I want to apply required field validator on this but I want to apply this on the page only instead of applying on the user control. This textbox renders as "ctl00_DefaultContent_AddressProviderBillingAddress_TextBoxExtendedZipCode". Now plz guide how can I make this textbox mandatory. Plz guide me ASAP instead of telling me as if I have asked a terrible thing :)
Posted
Comments
Member 8942392 11-May-12 6:14am    
hi,
i have one user control with fields such as name,lastname and address.i am using this user control in aspx page 3 times,how can i apply required field validator to thos user control fields,please guide me

1 solution

Hi,
You can use this for both purpose by adding property.

user control
XML
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="uctextBox.ascx.cs" Inherits="UserControlRegulerExpressionValidator.uctextBox" %>
<table><tr><td> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1"
        runat="server" ErrorMessage="RequiredFieldValidator" ValidationGroup="ucval" Enabled="false" ></asp:RequiredFieldValidator></td><td><asp:Button ID="Button1"
    runat="server" Text="Button" ValidationGroup="ucval" /></td></tr></table>

C#



code behind of the user control

C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (MyProperty)
               RequiredFieldValidator1.Enabled = MyProperty;
       }

       public bool MyProperty { get; set; }



Your page code behind
C#
protected void Page_Load(object sender, EventArgs e)
        {
            uctextBox1.MyProperty = true;
        }

simply enable and disable user control reqiured field validator using the property.

If you need more clarifications please ask and improve this code according to your requirement and don not forget to mark as answer if this helps to you.
 
Share this answer
 
Comments
dinidusoft123 31-Jan-12 23:26pm    
thanx.Hope you have solved the issue.
touseef4pk 6-Feb-12 5:32am    
yup ur ans is gr8
dinidusoft123 6-Feb-12 12:33pm    
thnx tousee4pk..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900