我想驗證一個使用RequiredFieldValidator的文本框,並通過後面的代碼在ValidationSummary中顯示錯誤消息,但我無法做到這一點。 我的代碼放在這裏通過後面的代碼使用asp.net驗證控件
if (txtUsrName.Text.Length == 0 || txtUsrAge.Text.Length == 0)
{
RequiredFieldValidator req = new RequiredFieldValidator();
req.ID = "Required";
req.ControlToValidate = txtUsrName.ID;
req.IsValid = false;
req.Visible = true;
req.Enabled = true;
req.ValidationGroup = "ValidationGroup";
req.ErrorMessage = "Thease are required fields";
req.InitialValue = "";
req.Text = "*";
ValidationSummary valsum = new ValidationSummary();
ValidationSummaryDisplayMode mode = new ValidationSummaryDisplayMode();
valsum.ID = "validatesummury";
valsum.HeaderText = "please correct the following errors";
valsum.DisplayMode = mode;
valsum.ShowSummary = true;
valsum.ValidationGroup = "ValidationGroup";
valsum.Visible = true;
}
請幫我這個
您需要重新創建pre-init上的元素。 –