DetailsView将文本更改为ItemInserted事件上的标签

时间:2010-11-23 09:32:18

标签: c# .net asp.net detailsview

我在DetailsView中有标签。 在触发事件ItemInserted后,我需要在DetailsView中更改此Label的Text属性。

脚本不会抛出任何错误,但Label不会更改其文本属性。 我有什么想法吗?

谢谢你的时间!

标记:

<asp:DetailsView ID="uxInsertAuthorInput" runat="server" AutoGenerateRows="False"
        DataKeyNames="AuthorId" DataSourceID="EntityDataSourceCreateAuthor" DefaultMode="Insert"
        OnItemInserting="uxInsertAuthor_ItemInserting" OnItemInserted="uxInsertAuthorInput_ItemInserted">
    <Fields>
        <asp:TemplateField HeaderText="First Name" SortExpression="FirstName">
            <ItemTemplate>
                <asp:Label ID="uxFirstNameDisplayer" runat="server" Text='<%# Bind("FirstName") %>' />
            </ItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="uxFirstNameInput" runat="server" Text='<%# Bind("FirstName") %>' />
                <asp:RequiredFieldValidator ID="uxRequiredFieldValidatorFirstName" runat="server"
                    ControlToValidate="uxFirstNameInput" ErrorMessage="First Name Field is required">*</asp:RequiredFieldValidator>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Last Name" SortExpression="LastName">
            <ItemTemplate>
                <asp:Label ID="uxLastNameDisplayer" runat="server" Text='<%# Bind("LastName") %>' />
            </ItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="uxLastNameInput" runat="server" Text='<%# Bind("LastName") %>' />
                <asp:RequiredFieldValidator ID="uxRequiredFieldValidatorLastName" runat="server"
                    ControlToValidate="uxLastNameInput" ErrorMessage="Last Name Field is required">*</asp:RequiredFieldValidator>
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="UserId" SortExpression="UserId">
            <InsertItemTemplate>
                <asp:DropDownList ID="uxUserListSelector" runat="server" />
                <asp:Label ID="uxAllert" runat="server" Text="Label" />
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Profile" SortExpression="ContentAuthor">
            <ItemTemplate>
                <asp:Label ID="uxContentAuthorDisplayer" runat="server" Text='<%# Bind("ContentAuthor") %>' />
            </ItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="uxContentAuthorInput" runat="server" Text='<%# Bind("ContentAuthor") %>' />
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="NoteInternal" SortExpression="NoteInternal">
            <ItemTemplate>
                <asp:Label ID="uxNoteInternalDisplayer" runat="server" Text='<%# Bind("NoteInternal") %>' />
            </ItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="uxNoteInternalInput" runat="server" Text='<%# Bind("NoteInternal") %>' />
            </InsertItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>

代码隐藏:

protected void uxInsertAuthorInput_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
    String FirstName = e.Values["FirstName"].ToString();
    String LastName = e.Values["LastName"].ToString();
    uxMessageDisplayer.Text = string.Format("Author: {0} {1} has been added.", FirstName, LastName);

    // problem is here:
    Label myAllert = (Label)uxInsertAuthorInput.FindControl("uxAllert");
    myAllert.Text = "TEST";
}

1 个答案:

答案 0 :(得分:1)

您需要使用ItemUpdated事件详情视图,而不是ItemInserted事件

ItemUpdated:在单击DetailsView控件中的“更新”按钮但在更新操作之后发生。来自here

我相信这将解决问题100%