ASP.Net Details List 'Edit' function

808 Views Asked by At

Afternoon All,

I am used to using gridviews but i am using a a details view for the first time. I am using Visual Studio 2010 with VB code.

I have a grid view that displays items from a database table and uses the 'ShowSelectButton="true"' function to enable users to select these individual items and display the full information within a details view.

The above works fine. The only issue that i have is my data is bound to the datasource and the associated database. On one of these columns i want to enable the users to select an item (Action Status and update - Outstaning, In-progress & Completed) in the form of a drop down list and then update it.

Im not too sue how to complete this? Here is my code for the details view....

        <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="400px" 
            AutoGenerateRows="False" 
            DataKeyNames="ActionID" DataSourceID="dsDetailsView" 
            AutoGenerateEditButton="True" CssClass="mGrid" PagerStyle-CssClass="pgr" 
            AlternatingRowStyle-CssClass="alt" HorizontalAlign="Center" 
            CellPadding="5" >
       <AlternatingRowStyle CssClass="alt"></AlternatingRowStyle>
         <Fields>

           <asp:BoundField DataField="AgendaID" HeaderText="Agenda Ref:" 
            SortExpression="AgendaID"  ReadOnly="true">
             <HeaderStyle Font-Bold="True" Font-Names="Ariel" />
           </asp:BoundField>

           <asp:BoundField DataField="ActionID" HeaderText="Action ID:" 
            InsertVisible="False" ReadOnly="True" SortExpression="ActionID" 
            ItemStyle-Width="500px" >
             <HeaderStyle Font-Bold="True" />
             <ItemStyle Width="500px" Font-Bold="True"></ItemStyle>
           </asp:BoundField>

           <asp:BoundField DataField="Action" HeaderText="Action:" 
            SortExpression="Action" ReadOnly="true">
            <HeaderStyle Font-Bold="True" />
           <ItemStyle Wrap="True" />
           </asp:BoundField>

           <asp:BoundField DataField="Owner" HeaderText="Owner:" 
            SortExpression="Owner"  ReadOnly="true">
            <HeaderStyle Font-Bold="True" Wrap="False" />
            <ItemStyle Wrap="False" />
           </asp:BoundField>

           <asp:BoundField DataField="TargetDate" HeaderText="Target Date:" 
            SortExpression="TargetDate"  ReadOnly="true" 
            DataFormatString="{0:dd-MM-yyyy} " >
            <HeaderStyle Font-Bold="True" Wrap="True" />
            <ItemStyle Font-Bold="False" />
           </asp:BoundField>

           <asp:BoundField DataField="DateCreated" HeaderText="Date Created:" 
            SortExpression="DateCreated"  ReadOnly="true" 
            DataFormatString="{0:dd-MM-yyyy} " >
            <HeaderStyle Font-Bold="True" Wrap="False" />
           </asp:BoundField>

          <asp:BoundField DataField="ActionUpdate" HeaderText="Action Update:" 
            SortExpression="ActionUpdate" 
            NullDisplayText="Select 'Edit' to update Action..."  >
           <HeaderStyle Font-Bold="True" Wrap="False" />
          </asp:BoundField>

          <asp:BoundField DataField="ActionStatus" HeaderText="Action Status:" 
            SortExpression="ActionStatus"  ReadOnly="true" >
          <HeaderStyle Font-Bold="True" Wrap="False" />
          </asp:BoundField>

          <asp:BoundField DataField="ActionStatusID" HeaderText="Action Status ID:" 
            SortExpression="ActionStatusID"  ReadOnly="true" Visible="true">
            <HeaderStyle Font-Bold="True" Wrap="False" />
          </asp:BoundField>

     </Fields>
       <PagerStyle CssClass="pgr" />
</asp:DetailsView>

Any help in advance is much appriechiated.

Regards Betty

2

There are 2 best solutions below

1
On BEST ANSWER

You want to use a Templatefield, rather than a BoundField for "ActionStatus" to accomplish this.

You can use the designer to convert the field to a TemplateField as follows:

  • Expand the grey arrow in the corner of your DetailsView (I can't remember what it's called), then
  • Click on "Edit Fields."
  • In the "Selected Fields" box, click on the field you want to change ("ActionStatus", in your case).
  • In the lower right-hand portion of the window, click the link that says "Convert this field into a TemplateField"

Then you need to go to your markup (the source view) and change the <EditItemTemplate> section so it has a DropDownList inside it. Like this:

<asp:TemplateField HeaderText="ActionStatus" 
    SortExpression="ActionStatus">
    <EditItemTemplate>
        <asp:DropdownList ID="actionStatusDDL" runat="server">
            <asp:ListItem Text="Outstanding" Value="Outstanding"></asp:ListItem>
            <asp:ListItem Text="In Progress" Value="In Progress"></asp:ListItem>
            <asp:ListItem Text="Completed" Value="Completed"></asp:ListItem>
        </asp:DropdownList>
    </EditItemTemplate>

You will also need to make sure that your DataSource has a "UpdateCommand" configured in order for the update to work properly.

0
On

Add the ddl in TemplateField and then access it in code behind:

            <asp:TemplateField HeaderText="Action Status list:">
            <ItemTemplate>
                <asp:DropDownList runat="server" ID="ddl_ActionList" DataSource="ActionListDataSource" DataValueField="ActionStatusID"
                    DataTextField="ActionStatus">
                </asp:DropDownList>
            </ItemTemplate>
        </asp:TemplateField>

in code behind use the find method to get and set your ddl selected value:

    Protected Sub DetailsView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DetailsView1.DataBound
    Dim ActionLst As DropDownList = DetailsView1.FindControl("ddl_ActionList")
    'ActionLst.SelectedValue =  set the selected value here
End Sub

Protected Sub DetailsView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewUpdateEventArgs) Handles DetailsView1.ItemUpdating
    Dim ActionLst As DropDownList = DetailsView1.FindControl("ddl_ActionList")
    'ActionLst.SelectedValue =  get the selected value here
End Sub