I have a page for user to select word documents and do documents merge. There is a ListView containing a label, a dropdownlist and a delete button. The ListView contain one row when the page load, users can add / delete rows during run-time. When submit button click, it will do documents merge and stream to download the output file. My problem is, file is downloaded after clicking the submit button but the dropdownlist in ListView are not removed. How to reset the ListView to the original state after clicking submit button?

In Default.aspx

<asp:ListView ID="lvDynamicControls" runat="server" ItemPlaceholderId="itemPlaceholder" 
    OnItemDataBound="lvDynamicControls_ItemDataBound" OnItemCommand="lyDynamicControls_ItemCommand">
    <LayoutTemplate>
        <table>
            <asp:PlaceHolder ID="itemPlaceholder" runat="server" ></asp:PlaceHolder>
        </table>
    </LayoutTemplate>
    <ItemTemplate>
        <tr>
            <td><asp:Label ID="lblService" runat="server" Text="Service"></asp:Label></td>
            <td><asp:DropDownList ID="ddlService" runat="server" ></asp:DropDownList></td>
            <td><asp:LinkButton ID="lnkDelete" runat="server">Delete</asp:LinkButton></td>
        </tr>
    </ItemTemplate>
</asp:ListView>
<asp:Button ID="btnAddControls" runat="server" Text="Add more" OnClick="btnAddControls_click" />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" 
    onclick="btnSubmit_Click" />
<asp:Button ID="btnClear" runat="server" Text="Clear" 
    onclick="btnClear_Click" />

Part of the code in Default.aspx.cs

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.BindListView();  
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        List<string> selectedFiles = this.GetSelectedFiles();
        this.MergeDocuments(selectedFiles);
        this.ClearListView();
    }
    private void MergeDocuments(List<string> filenames)
    {
         // code for merging documents here...
         // stream file to download
         Response.Clear();
         Response.ContentType = "application/force-download";
         Response.AddHeader("content-disposition", "attachment;    filename=output.docx");
         mem.Position = 0;
         mem.CopyTo(Response.OutputStream);
         Response.End();
    }
    protected void ClearListView()
    {
        // create a new dataTable with 1 row and update datasource
        this.SetDataSource(this.CreateDataTable());
        this.lvDynamicControls.Items.Clear();
        this.lvDynamicControls.DataSource = null;
        this.lvDynamicControls.DataBind();
        this.BindListView();
    }
    protected void BindListView()
    {
        DataTable dataSource = this.GetDataSource();
        this.lvDynamicControls.DataSource = dataSource;
        this.lvDynamicControls.DataBind();
    }

Related posts

Recent Viewed