我正在尝试将 TextBox 和 RequiredFieldValidator 添加到嵌套在 GridView 中的 DetailsView 的 InsertItemTemplate 中。我在 InsertItemTemplate 中添加了 PlaceHolder,并在 GridView 的 OnRowDataBound 事件中执行以下操作:
编辑:我意识到我需要将 ValidationGroup 添加到 Insert LinkButton (lnkInsert),但这对 TextBox 和/或 RequiredFieldValidator 的呈现没有影响。ValidationGroup 属性也不会添加到 lnkInsert。似乎我无法在父 GridView 的 RowDataBound 事件中编辑 DetailsView 内的任何控件。
if (e.Row.RowType == DataControlRowType.DataRow)
{
string valGroup = "PickListValidationGroup" + e.Row.RowIndex;
// Add the validation summary
PlaceHolder vsPlaceHolder = (PlaceHolder)e.Row.FindControl("ValidationSummaryPlaceHolder");
ValidationSummary vs = new ValidationSummary();
vs.ID = "PickListValidationSummary" + e.Row.RowIndex;
vs.ValidationGroup = valGroup;
vs.CssClass = "failurenotification";
vsPlaceHolder.Controls.Add(vs);
// Add the TextBox and RequiredFieldValidator
DetailsView dv = (DetailsView)e.Row.FindControl("AddPickListOption");
PlaceHolder displayPlaceHolder = (PlaceHolder)dv.FindControl("DisplayTextPlaceHolder");
TextBox tb = new TextBox();
tb.ID = "txtDisplayText" + e.Row.RowIndex;
tb.Text = "<%# Bind(\"DisplayText\") %>";
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.ID = "DisplayTextRequired" + e.Row.RowIndex;
rfv.ControlToValidate = tb.ID;
rfv.ErrorMessage = "Enter text to display for the list option.";
rfv.ValidationGroup = valGroup;
rfv.ToolTip = "Enter text for the option";
rfv.Text = "*";
displayPlaceHolder.Controls.Add(tb);
displayPlaceHolder.Controls.Add(rfv);
// Add the ValidationGroup to the Insert LinkButton
LinkButton lnk = (LinkButton)dv.FindControl("lnkInsert");
lnk.ValidationGroup = valGroup;
}
ValidationSummary 被添加到占位符中(它不在DetailsView 中),但TextBox 和RequiredFieldValidator 没有显示在DetailsView InsertItemTemplate 的PlaceHolder 中。
编辑:这是 DetailsView 标记,它再次嵌套在 GridView 中:
<div class="error_div">
<asp:PlaceHolder ID="ValidationSummaryPlaceHolder" runat="server"></asp:PlaceHolder>
</div>
<asp:DetailsView ID="AddPickListOption" runat="server" Height="50px"
Width="125px" AutoGenerateRows="False" DataSourceID="PickListDataSource"
DefaultMode="Insert" EnableModelValidation="True" OnItemInserted="AddPickListOption_ItemInserted"
OnItemInserting="AddPickListOption_ItemInserting" Visible="False"
OnItemCommand="AddPickListOption_ItemCommand">
<Fields>
<asp:TemplateField HeaderText="Display Text:" SortExpression="DisplayText">
<InsertItemTemplate>
<asp:PlaceHolder ID="DisplayTextPlaceHolder" runat="server"></asp:PlaceHolder>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Sort Order:" SortExpression="SortOrder">
<InsertItemTemplate>
<asp:TextBox ID="txtPickListSortOrder" runat="server" Text='<%# Bind("SortOrder") %>'></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price Change:" SortExpression="PriceChange">
<InsertItemTemplate>
<asp:TextBox ID="txtPickListPriceChange" runat="server" Text='<%#Bind("PriceChange") %>'></asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<InsertItemTemplate>
<asp:LinkButton ID="lnkInsert" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
我在这里想念什么?我认为这一定是某种我还没有深入了解的页面/控件生命周期问题。