简体   繁体   中英

how to clear the items of a listview control and datapager

I have a ListView control. I am displaying the data but once the user clicks the clear button all the data that is shown in the ListView control should go off (empty). We should clear it contents and display a message no data to be displayed.

Even the paging (I am using DataPager control to achive paging) which was shown earlier when data was there should not be shown as we have cleared the data. My code looks like this:

protected void lvEmployee_DataBound(object sender, EventArgs e)    
{    
    DropDownList ddl = DataPager1.Controls[1].FindControl("ddlPage") as DropDownList;                           
    int PageCount = (DataPager1.TotalRowCount / DataPager1.PageSize);    
    if (PageCount*DataPager1.PageSize != DataPager1.TotalRowCount)
    {
        PageCount = PageCount + 1;
    }             

    for (int i = 0; i < PageCount; i++)
    {
        ddl.Items.Add(new ListItem((i+1).ToString(),i.ToString()));
    }

    ddl.Items.FindByValue(CurrentPage.ToString()).Selected = true;
}

When I clear, all the data should go off and even the paging.

To clear all the values:

// in your .cs
lvEmployee.DataSource = null;
lvEmployee.DataBind();

To display a message when no data exists implement the EmptyDataTemplate :

// in your .aspx
<asp:ListView ID="lvEmployee" runat="server">
    <EmptyDataTemplate>
        No data available.
    </EmptyDataTemplate>
</asp:ListView>

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