I have a User Control that consists of a TextBox with a few extra features, but for the purposes of this example, it is just assumed that a simple TextBox is enough. I am invoking this user control from a web form and want to be able to use the RequiredFieldValidator, which will basically work just as if I used it in a text box in a web form. How to configure my user control for this?
EDIT:
Datepicker.ascx
<asp:TextBox runat="server" ID="myControlTB">
DatePicker.ascx.cs
[ValidationProperty("Text")]
public partial class DatePicker : System.Web.UI.UserControl
{
public String Text { get { return myControlTB.Text; } set { myControlTB.Text = value; } }
protected void Page_Load(object sender, EventArgs e)
{
}
}
WebForm.aspx
<cu:UserControl runat="server" ID="myControl">
<asp:RequiredFieldValidator runat="server" errormessage="This is a required field." ControlToValidate="myControl">
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
WebForm.aspx.cs
protected void btnSubmit_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
}
source
share