DetailsView inserting/updating with templates

I was having a problem with template fields in a details view, where it doesn't automatically get the value from a template field.  The problem is the template shows up as a control through FindControl and a child control of the cell, whereas boundfields, etc. can be retrieved through the collection of values that are stored in the event argument.  The initial bind is OK, but like with the insert, binding isn't provided for both directions.  The problem is that the properties for those templates aren't in the collection, and aren't automatically updated, at least that is what I was having problems with.  I was getting SqlExceptions for the insert statements, stating that the value was null.  So how do you ensure that it is there, without custom inserting/updating?

The way that I did it, whether right or wrong, is to attach to the ItemInserting event.  Then, I retrieve the value from the control, and append it to the e.Values collection, which is an IOrderedDictionary.  For ItemUpdating, it is e.NewValues.  So my ItemInserting would look like:

FormView view = (FormView)sender;
DropDownList box = view.FindControl("DDL") as DropDownList;
e.Values.Add("State", box.SelectedValue);

I haven't tried this with the updating yet, but I did this with insertion, and it worked great.  I don't know if this is a good idea or not, but I don't see any other day.
Published Wednesday, August 16, 2006 4:56 PM by bmains
Filed under:

Comments

# re: DetailsView inserting/updating with templates

Thursday, November 08, 2007 8:21 AM by Paul

I have been battling this behavior for several days now and just wanted to say thanks for sharing. It's reassuring to know that someone else dealt with this issue in a similar fashion.

# re: DetailsView inserting/updating with templates

Thursday, November 08, 2007 7:01 PM by Paul

Brian:

I am trying to implement this strategy and I can not access my drop down list from my ItemInserting event handler.  FindControl is returning a null reference.  Can you tell me why the list would be gone?

# re: DetailsView inserting/updating with templates

Monday, June 23, 2008 10:39 AM by MyPlace311

this is a Details view i made for adding locations to an order for a moving company.

youll see that is you set the TEXT value to text="<%BIND("FeildNAme")%> for each of the item templates that the data will be updated. ferthermore you will also see that when in ReadOnly that it will display the text for the current records value(eg for stop type "PickUp" "DropOff" ect)

hope this helps

          <asp:DetailsView ID="dvLoactions" runat="server" AutoGenerateRows="False"

               DefaultMode="Insert" DataSourceID="AddLocations" Height="50px"

               Width="125px">

               <Fields>

                   <asp:BoundField DataField="LocationID" HeaderText="LocationID"

                       InsertVisible="False" ReadOnly="True" SortExpression="LocationID"

                       Visible="False" />

                   <asp:BoundField DataField="Contact" HeaderText="Contact"

                       SortExpression="Contact" />

                   <asp:BoundField DataField="Area" HeaderText="Area" SortExpression="Area" />

                   <asp:BoundField DataField="Telephone" HeaderText="Telephone"

                       SortExpression="Telephone" />

                           <asp:TemplateField  ItemStyle-Width="50px"  HeaderText="Building:"

                       SortExpression="Notes">

                       <ItemTemplate>

                       <asp:DropDownList OnSelectedIndexChanged="setBuildingNotes" ID="ddlBuildingInfoDV" runat="server" AutoPostBack="True"

                               DataSourceID="sqlBuildingINfo" DataTextField="BuildingName"

                               DataValueField="ID" Enabled="false"  Text='<%# Bind("BuildingID") %>'/>

                       </ItemTemplate>

                       <EditItemTemplate>

                           <asp:DropDownList OnSelectedIndexChanged="setBuildingNotes" ID="ddlBuildingInfoEdit" runat="server" AutoPostBack="True"

                               DataSourceID="sqlBuildingINfo" DataTextField="BuildingName"

                               DataValueField="ID" text='<%# Bind("BuildingID") %>'/>

                       </EditItemTemplate>

                       <InsertItemTemplate>

                       <asp:DropDownList OnSelectedIndexChanged="setBuildingNotes" ID="ddlBuildingInfoInsert" runat="server" AutoPostBack="True"

                               DataSourceID="sqlBuildingINfo" DataTextField="BuildingName"

                               DataValueField="ID" Text='<%# Bind("BuildingID") %>'>

                       </asp:DropDownList>

                       <asp:Button ID="btnAddNewBuilding" Text="Add New Building" runat="server" OnClick="AddNewBuilding" />

                       </InsertItemTemplate>

                       <ControlStyle></ControlStyle>

<ItemStyle Width="50px"></ItemStyle>

               </asp:TemplateField>

                   <asp:BoundField DataField="AptNumber" HeaderText="AptNumber"

                       SortExpression="AptNumber" />

                   <asp:BoundField DataField="StreetNumber" HeaderText="StreetNumber"

                       SortExpression="StreetNumber" />

                   <asp:BoundField DataField="StreetName" HeaderText="StreetName"

                       SortExpression="StreetName" />

                   <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />

                   <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />

                   <asp:BoundField DataField="zip" HeaderText="zip" SortExpression="zip" />

                   <asp:CheckBoxField DataField="COI" HeaderText="COI" SortExpression="COI" />

                   <asp:BoundField DataField="CarryDist" HeaderText="CarryDist"

                       SortExpression="CarryDist" />

                   <asp:TemplateField  ItemStyle-Width="50px"  HeaderText="Stop Type"

                       SortExpression="Notes">

                       <ItemTemplate>

                       <asp:DropDownList ID="DropDownListItem" runat="server"

                           Enabled="false"  EnableViewState="true" Text='<%# Bind("LocationType") %>'/>

                       </ItemTemplate>

                       <EditItemTemplate>

                           <asp:DropDownList ID="DropDownListEdit" runat="server"

                           Enabled="true" Text='<%# Bind("LocationType") %>'>

                               <asp:ListItem>Pick Up</asp:ListItem>

                               <asp:ListItem>Extra Stop</asp:ListItem>

                               <asp:ListItem>Drop Off</asp:ListItem>

                           </asp:DropDownList>

                       </EditItemTemplate>

                       <InsertItemTemplate>

                       <asp:DropDownList ID="DropDownListInsert" runat="server"  

                           Enabled="true" Text='<%# Bind("LocationType") %>'>

                                       <asp:ListItem>Pick Up</asp:ListItem>

                                       <asp:ListItem>Extra Stop</asp:ListItem>

                                       <asp:ListItem>Drop Off</asp:ListItem>

                       </asp:DropDownList>

                       </InsertItemTemplate>

                       <ControlStyle></ControlStyle>

<ItemStyle Width="50px"></ItemStyle>

               </asp:TemplateField>

                   <asp:CommandField ShowInsertButton="True" />

               </Fields>

           </asp:DetailsView>

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.