添加到嵌套在Gridview中的DetailsView中的占位符的控件未显示

时间:2013-05-20 15:58:07

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

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

编辑:我意识到我需要将ValidationGroup添加到Insert LinkBut​​ton(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>

我在这里缺少什么?我认为这必定是某种页面/控件生命周期问题,我还没有充分理解。

1 个答案:

答案 0 :(得分:0)

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

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

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

displayPlaceHolder.Controls.Add(tb1);

“tb1”可能已存在于其他地方,这就是为什么它并没有真正添加到那里。