【问题标题】:Controls added to placeholder in DetailsView nested in Gridview are not showing up添加到嵌套在 Gridview 中的 DetailsView 占位符的控件未显示
【发布时间】:2013-05-20 15:58:07
【问题描述】:

我正在尝试将 TextBox 和 RequiredFieldValidator 添加到嵌套在 GridView 中的 DetailsView 的 InsertItemTemplate。我在 InsertItemTemplate 中添加了 PlaceHolder,我在 GridView 的 OnRowDataBound 事件中执行以下操作:

编辑:我意识到我需要将 ValidationGroup 添加到插入链接按钮 (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>&nbsp;
                <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" 
                    CommandName="Cancel" Text="Cancel"></asp:LinkButton>
            </InsertItemTemplate>
        </asp:TemplateField>
    </Fields>
</asp:DetailsView>

我在这里缺少什么?我想这一定是某种我还没有深入理解的页面/控件生命周期问题。

【问题讨论】:

  • 您没有收到任何错误消息?
  • 不,除了细节视图中的文本框外,所有渲染都很好。我可以在标记中找到验证摘要,但在文本框中找不到。

标签: asp.net gridview nested detailsview dynamic-controls


【解决方案1】:

看来你只是有一个错字。您在这里创建一个名为“tb”的 TextBox 变量,并设置一些属性:

TextBox tb = new TextBox();
tb.ID = "txtDisplayText" + e.Row.RowIndex;
tb.Text = "<%# Bind(\"DisplayText\") %>";

然后将一个名为“tb1”的变量添加到控件集合中:

displayPlaceHolder.Controls.Add(tb1);

“tb1”可能已经存在于其他地方,这就是它没有真正添加到那里的原因。

【讨论】:

  • 我很兴奋,但不幸的是我只是在这里打错了字。仍然是相同的行为。我会修改错别字。
  • @user1405290 “插入”是您的默认模式吗?如果没有,我想知道在更改为插入模式时添加 TextBox 和 Validator OnModeChanged 是否有帮助。
  • 感谢您添加额外的上下文,@user1405290。您可以将此逻辑移至 DetailsView 的 DataBound 事件吗?我担心它会以某种方式覆盖您正在执行的全部或部分操作,因为它发生在 GridView 的行已被数据绑定之后。​​
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-05
  • 2017-08-10
相关资源
最近更新 更多