Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net Asp RequiredFieldValidator文本属性停止显示,但错误消息仍显示在validationsummary中_Asp.net_Webforms_Webpage_Lifecycle_Requiredfieldvalidator - Fatal编程技术网

Asp.net Asp RequiredFieldValidator文本属性停止显示,但错误消息仍显示在validationsummary中

Asp.net Asp RequiredFieldValidator文本属性停止显示,但错误消息仍显示在validationsummary中,asp.net,webforms,webpage,lifecycle,requiredfieldvalidator,Asp.net,Webforms,Webpage,Lifecycle,Requiredfieldvalidator,我对RequiredFieldValidator文本属性有问题。我的asp web表单有一个面板。在重写的OnPreInit中,我创建了一个带有文本框和RequiredFieldValidator的表,并将其存储到会话变量中,在每次回发时,我将该表保存到该会话中并将其加载。当我运行代码时,验证程序就正常了。通过每次回发,只要未填写文本框且验证摘要显示错误消息,文本属性就会显示在表中。当我填写文本框时,验证器显示正确的valid;文本属性不再显示,validatorsummary不再显示错误。然后

我对RequiredFieldValidator文本属性有问题。我的asp web表单有一个面板。在重写的OnPreInit中,我创建了一个带有文本框和RequiredFieldValidator的表,并将其存储到会话变量中,在每次回发时,我将该表保存到该会话中并将其加载。当我运行代码时,验证程序就正常了。通过每次回发,只要未填写文本框且验证摘要显示错误消息,文本属性就会显示在表中。当我填写文本框时,验证器显示正确的valid;文本属性不再显示,validatorsummary不再显示错误。然后,当我删除文本框中的内容并导致回发(单击按钮)时,奇怪的事情发生了。错误消息显示在错误消息中,当我调试RequiredFieldValidator时。IsValid为false。但是文本属性永远不会显示回!?我想我可能缺少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>

div{填充:0;边距:0;}/*生成的div*/
div.centeredDiv{padding:0;margin:0;margin left:auto;margin right:auto;}/*生成的div*/
table.tblFormat{左边距:自动;右边距:自动;}
td.tdFormat{text align:left;}
td.tdHidden{可见性:隐藏}
.centered{左边距:自动;右边距:自动;}





我想我解决了这个问题。当requiredfieldvalidator设置为valid(.isvalid=true)时,它会添加一个属性键:“style”值:display:none。然后,当文本被删除且验证程序为false时,该属性将保留。我的修复方法是挂接到验证器的呈现中,并始终删除此属性。然后,我只在验证器无效时写出错误消息。我的意思是:

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

validator.SetRenderMethodDelegate(validator_SetRenderMethodDelegate);
接下来在validator_setRenderMethodElegate方法中,始终删除“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);
        }
    }
}
我认为这并不完美,但这是朝着正确方向迈出的一步