RequiredFieldValidator handling inside a user control

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)
    {
        // e-mail the form
    }
+3
source share
2 answers

ValidationProperty Text Text Text

 [ValidationProperty("Text")]
 public partial class Control
 {
    public string Text 
    {
        get { return textbox.Text;}
    }

 }
+3

:

public string TextBoxID
{
   get { return myControlTB.ClientID; }
}

, :

void Page_Load(..)
{
    this.rfv.ControlToValidate = this.uc.TextBoxID;
}

, , UniqueID , .

, TextBoxID TextboxText, myControlTB.Text [ValidationProperty ( "TextBoxText" )] ControlToValidate .

,

.

0

Source: https://habr.com/ru/post/1788512/


All Articles