我遇到了RequiredFieldValidator文本属性的问题。我的asp web表单有一个Panel。在重写的OnPreInit中,我创建了一个带有文本框和RequiredFieldValidator的表,并将其存储到会话变量中,在每次回发时,我将表保存到此会话中并加载它。当我运行代码时验证器没问题。通过每个回发,只要未填写文本框并且验证摘要显示错误消息,就会在表中显示Text属性。当我填写文本框时,验证器显示有效的正确;不再显示Text属性,validatorsummary不再显示错误。然后,当我删除文本框中的内容并导致回发(单击按钮)时,会发生奇怪的事情。错误消息显示在错误消息中,当我调试RequiredFieldValidator时.IsValid为false。但Text Property永远不会显示备份!?我想我可能会遗漏一些关于viewstate的东西,可能是RequiredFieldValidator上的预渲染,或者我保存/恢复会话状态的方式。
我已经尝试了所有我能想到的东西。谢谢你的帮助!!!!
aspx.cs
namespace AccessManagementRepeatingTableAsp
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Security;
using System.Transactions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class webform : System.Web.UI.Page
{
private Table testTable = new Table();
protected override void OnPreInit(EventArgs e)
{
if (!this.IsPostBack)
{
TextBox targetTextBox = new TextBox();
string targetTextBoxId = Guid.NewGuid().ToString();
targetTextBox.ID = targetTextBoxId;
RequiredFieldValidator targetRequiredValidator = new RequiredFieldValidator();
targetRequiredValidator.ID = Guid.NewGuid().ToString();
targetRequiredValidator.ForeColor = Color.Red;
targetRequiredValidator.ErrorMessage = "programatic";
targetRequiredValidator.Text = "testvalidator";
targetRequiredValidator.ControlToValidate = targetTextBoxId;
targetRequiredValidator.Display = ValidatorDisplay.Dynamic;
targetRequiredValidator.Visible = true;
TableCell textCell = new TableCell();
textCell.Text = "textCell";
textCell.Controls.Add(targetTextBox);
TableCell validatorCell = new TableCell();
validatorCell.Text = "validatorCell";
validatorCell.Controls.Add(targetRequiredValidator);
TableRow row = new TableRow();
row.Cells.Add(textCell);
row.Cells.Add(validatorCell);
this.testTable.Rows.Add(row);
Session["test"] = this.testTable;
}
else
{
this.testTable = (Table)Session["test"];
RequiredFieldValidator rfv = (RequiredFieldValidator)this.testTable.Rows[0].Cells[1].Controls[0];
rfv.Validate();
Session["test"] = this.testTable;
}
this.testPanel.Controls.Add(this.testTable);
RequiredFieldValidator rfv2 = (RequiredFieldValidator)this.testTable.Rows[0].Cells[1].Controls[0];
rfv2.Validate();
Page.Validate();
base.OnPreInit(e);
}
protected void submitButton_Click(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
this.statusLabel.ForeColor = Color.Blue;
this.statusLabel.Text = "Status: Valid";
}
else
{
this.statusLabel.ForeColor = Color.Red;
this.statusLabel.Text = "Status: Invalid";
}
}
}
}
ASPX
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AccessManagement.aspx.cs" Inherits="AccessManagementRepeatingTableAsp.webform" %>
<!DOCTYPE html>
<style type="text/css">
div {padding: 0; margin: 0; } /* generated div */
div.centeredDiv {padding: 0; margin: 0; margin-left:auto; margin-right:auto;} /* generated div */
table.tblFormat {margin-left:auto; margin-right:auto;}
td.tdFormat {text-align:left;}
td.tdHidden {visibility:hidden}
.centered { margin-left:auto; margin-right:auto;}
</style>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="Form" runat="server">
<div>
<asp:Button ID="submitButton" runat="server" Text="Submit" OnClick="submitButton_Click" />
<br />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
<br />
<asp:Label ID="statusLabel" runat="server" Text="Label"></asp:Label>
<br />
</div>
<asp:Panel ID="testPanel" runat="server">
</asp:Panel>
<br />
</form>
</body>
</html>
答案 0 :(得分:0)
我想我弄明白了这个问题。当requiredfieldvalidator设置为valid(.isvalid = true)时,它会添加一个属性键:'style'值:display:none。然后,当删除文本并且验证器为false时,该属性仍然存在。我的修复是挂钩验证器的渲染,并始终删除此属性。然后,如果验证器无效,我只会写出错误消息。这就是我的意思:
首先将验证器的呈现重定向到您自己的方法:
validator.SetRenderMethodDelegate(validator_SetRenderMethodDelegate);
在validator_SetRenderMethodDelegate方法中,接下来要删除'style'属性。
private void validator_SetRenderMethodDelegate(HtmlTextWriter output, Control container)
{
if (!this.IsPostBack)
{
RequiredFieldValidator localRFV = ((RequiredFieldValidator)container);
RequiredFieldValidator tableRFV = (RequiredFieldValidator)this.testTable.Rows[0].Cells[1].Controls[0];
string[] keys = new string[((RequiredFieldValidator)container).Attributes.Count];
localRFV.Attributes.Keys.CopyTo(keys, 0);
foreach (string key in keys)
{
string current = localRFV.Attributes[key];
if (key == "style")
{
localRFV.Attributes.Remove(key);
tableRFV.Attributes.Remove(key);
Session["test"] = this.testTable;
}
}
if (!localRFV.IsValid)
{
output.Write(localRFV.Text);
}
}
}
我认为这不是完美的,但这是朝着正确方向迈出的一步。