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>