简体   繁体   中英

Dynamically added RequiredFieldValidator is not working

I have linkbutton on its click event which adds one Textbox and RequiredValidator Dynamically.

protected void lnkAdd_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
RequiredFieldValidator ReqAuthor = new RequiredFieldValidator();

txt.ID = "txtAuthorName";
txt.CssClass = "form_3";

ReqAuthor.ID = "ReqAuthor"
ReqAuthor.SetFocusOnError = true;
ReqAuthor.EnableClientScript = true;
ReqAuthor.Enabled = true;
ReqAuthor.ControlToValidate = txt.ID;
ReqAuthor.Display = ValidatorDisplay.Dynamic;
ReqAuthor.ErrorMessage = "Please Enter Author Name.";
ReqAuthor.Font.Size = 12;

pnlAuthorTextBox.Controls.Add(txt);

pnlAuthorTextBox.Controls.Add(ReqAuthor);
}

In this code No Error i get at runtime. Code runs Smoothly.

Textbox also get created. But RequiredFieldValidator does not seems as working.

Thanks in advance.

Please help

This is where you have the problem : CausesValidation="true"

<asp:Button ID="btnSubmit" runat="server" CausesValidation="true" OnClick="btnSubmit_Click" Text="Submit" />

and your validator will start working and let me know if that solved your issue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM