简体   繁体   中英

Insert extra custom fields from DetailsView

I am using a DetailsView control (C# 4.0) to add a record with an EntityDataSource control. There are some fields that need to be added which I do not want to be visible in the DetailsView control - such as DateAdded and UserId - these fields should be added automatically.

<asp:EntityDataSource ID="edsTasks" runat="server" 
    ContextTypeName="EKIMV2_MasterModel.EKIMV2_MasterEntities" 
    EnableFlattening="False" EnableInsert="True"
    EntitySetName="tasks" ConnectionString="name=EKIMV2_MasterEntities" 
    DefaultContainerName="EKIMV2_MasterEntities">
</asp:EntityDataSource>

<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" 
    DataSourceID="edsTasks" DefaultMode="Insert" 
    AutoGenerateRows="False" DataKeyNames="task_id" 
    oniteminserted="DetailsView1_ItemInserted" 
    oniteminserting="DetailsView1_ItemInserting">
    <Fields>
        <asp:BoundField DataField="task_name" HeaderText="task_name" 
            SortExpression="task_name" />
        <asp:BoundField DataField="task_desc" HeaderText="task_desc" 
            SortExpression="task_desc" />
        <asp:TemplateField HeaderText="assigned_to" SortExpression="assigned_to">
            <InsertItemTemplate>
                <asp:DropDownList ID="ddlUsers" runat="server" DataSourceID="edsUsers" DataTextField="UserName" DataValueField="UserId"></asp:DropDownList>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Bind("assigned_to") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ButtonType="Button" ShowCancelButton="False" 
            ShowInsertButton="True" />
    </Fields>
</asp:DetailsView>

As you can see above there is no DateAdded field. I want to automatically set this value to today's date. The user doesn't need to add the value or see the field at all. There are other fields that I want to set automatically, but without getting into too much detail I think the DateAdded field gives a good example.

I figure I need to somehow add the extra fields during the ItemInserting event, but I don't know where to start.

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{

}

I've read elsewhere that maybe I need to keep the extra fields in the DetailsView but hide them? Is that right, it doesn't seem like the right thing to do.

So is it possible to add values to fields that are not actually in the DetailsView?

So the answer lies in hiding the fields in the DetailsView by converting them to TemplateFields then using a HiddenField within the template.

You then need to handle the DetailsView1_ItemInserting() event like so:

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
    e.Values["date_submitted"] = DateTime.Now.ToShortDateString();
}

The event receives DetailsViewInsertEvenrArgs ("e") which contains a dictionary object called Values. Values contains the data that is passed back to your datasource control (an EntityDataSource) in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM