0

RequiredFieldValidator テキスト プロパティに問題があります。私の ASP Web フォームには Panel があります。オーバーライドされた OnPreInit で、テキスト ボックスと RequiredFieldValidator を含むテーブルを作成し、それをセッション変数に格納します。ポストバックごとに、テーブルをこのセッションに保存してロードします。コードを実行すると、バリデータは問題ありません。テキスト ボックスが入力されておらず、検証の概要にエラー メッセージが表示されている限り、ポストバックごとに Text プロパティがテーブルに表示されます。テキストボックスに入力すると、バリデーターは正しい有効なものを表示します。Text プロパティが表示されなくなり、validatorssummary にエラーが表示されなくなりました。次に、テキストボックスの内容を削除してポストバック (ボタンをクリック) すると、奇妙なことが起こります。エラーメッセージはエラーメッセージに表示され、 RequiredFieldValidator をデバッグすると表示されます。IsValid は false です。しかし、Text プロパティが表示されない!? ビューステート、おそらく 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 が追加されます。次に、テキストが削除され、バリデーターが 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);
        }
    }
}

完璧ではないと思いますが、正しい方向への一歩です。

于 2012-11-05T18:33:58.630 に答える