0

我遇到了RequiredFieldValidator 文本属性的问题。我的 asp web 表单有一个面板。在覆盖的 OnPreInit 中,我创建了一个带有文本框和 RequiredFieldValidator 的表,并将其存储到会话变量中,在每次回发时,我将表保存到此会话中并加载它。当我运行代码时,验证器很好。通过每次回发,只要未填写文本框并且验证摘要显示错误消息,表格中就会显示 Text 属性。当我填写文本框时,验证器显示有效,这是正确的;Text 属性不再显示,validatorssummary 不再显示错误。然后,当我删除文本框中的内容并导致回发(单击按钮)时,会发生奇怪的事情。错误消息显示在错误消息中,当我调试 RequiredFieldValidator 时。IsValid 为假。但是文本属性永远不会显示备份!?我认为我可能会丢失视图状态的某些内容,可能是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>
4

1 回答 1

0

我想我找到了问题所在。当 requiredfieldvalidator 设置为有效 (.isvalid = true) 时,它会添加一个属性键:'style' 值:display:none。然后当文本被删除并且验证器为假时,该属性仍然存在。我的解决方法是挂钩验证器的呈现并始终删除此属性。然后,如果验证器无效,我只会写出错误消息。这就是我的意思:

首先将验证器的呈现重定向到您自己的方法:

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);
        }
    }
}

我认为这并不完美,但这是朝着正确方向迈出的一步。

于 2012-11-05T18:33:58.630 回答