C# – DataSource populated, listview binds but show EmptyDataTemplate

asp.netcdata-bindinglistview

I am having this weird issue with a C# asp.net listview that I am unable to pin point the exact cause and problem. Here's the scenario

I have a search textbox that uses AutoCompleteExtender. At PageLoad(), the listview will be populated with a bunch of data extracted from a DataTable. When someone enters something in the textbox, get result from webservice, populate the result in the DataTable and the listview will bind to the DataTable.

Everything works ok – listview binds ok with DataPager working properly initially. At the 1st page of the listview, if user enters a search, listview will bind and show the new result.

However, when I'm in the 2nd page or more, listview binds but shows EmptyDataTemplate. I have checked the DataTable and determine that it is populated with the new data before listview.DataBind. The problem only happens when I move away from Page 1 of the listview.

ASPX

<asp:ListView ID="productList" runat="server" onitemcommand="productList_ItemCommand" DataKeyNames="PrimaryID">

             <LayoutTemplate>
          <table>
            <tr runat="server">
              <th runat="server">Actions</th>
              <th runat="server">PrimaryID</th>
               <th runat="server">Product</th>
            <th runat="server">Description</th>
            </tr>
            <tr runat="server" id="itemPlaceholder" />
          </table>
        <asp:DataPager runat="server" ID="productDataPager" PageSize="20" PagedControlID="productList" QueryStringField="pageNumber">
            <Fields>
               <asp:NextPreviousPagerField ButtonType="Image" ShowFirstPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" FirstPageText="|&lt;&lt; " />
                <asp:NumericPagerField ButtonCount="10" />
                <asp:NextPreviousPagerField ButtonType="Image" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" LastPageText=" &gt;&gt;|" />

            </Fields>
          </asp:DataPager>     
       </LayoutTemplate>

         <ItemTemplate>
          <tr id="Tr1" class="even" runat="server">
            <td>
              <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit_product"/>
            </td>

            <td">
              <asp:Label ID="primarylbl" runat="Server" Text='<%#Bind("PrimaryID") %>' />
            </td>
            <td>
              <asp:Label ID="productlbl" runat="Server" Text='<%#Bind("Product") %>' />
            </td>
             <td>
              <asp:Label ID="descriptionlbl" runat="Server" Text='<%#Bind("Description") %>' />            </td>
            </tr>
        </ItemTemplate>
       <AlternatingItemTemplate>
        <tr id="Tr1" class="odd" runat="server">
            <td>
              <asp:LinkButton ID="EditButton" runat="Server" Text="Edit" CommandName="Edit_product"/>
            </td>

            <td>
              <asp:Label ID="primarylbl" runat="Server" Text='<%#Bind("PrimaryID") %>' />
            </td>
            <td>
              <asp:Label ID="productlbl" runat="Server" Text='<%#Bind("Product") %>' />
            </td>
             <td>
              <asp:Label ID="descriptionlbl" runat="Server" Text='<%#Bind("Description")  %>' />
            </td>
            </tr>

       </AlternatingItemTemplate>

         <EmptyDataTemplate>
       No Records Found
       </EmptyDataTemplate>
           </asp:ListView>

CodeBehind

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string productkey = "0";
                getWeb(productkey); //call WebService to get all Products

             }

        }

   private void createTable(Products[] product)
        {


            DataTable productTable = new DataTable();
            productTable.Columns.Add(new DataColumn("PrimaryID", typeof(string)));
            prouctTable.Columns.Add(new DataColumn("Product", typeof(string)));
            productTable.Columns.Add(new DataColumn("Description", typeof(string)));

            for (int i = 0; i < product.Length; i++)
            {

                DataRow dr = productTable.NewRow();
                dr["PrimaryID"] = product[i].PrimaryID.ToString();
                dr["Product"] = product[i].Product.ToString();
                dr["Description"] product[i].Description.ToString();


                productTable.Rows.Add(dr);
                productTable.AcceptChanges();
            }


            bindtoList(productTable);

 protected void bindtoList(DataTable prodTab)
        {
            if (productList.DataSource == null)
            {
                productList.DataSource = prodTab;
                productList.DataBind();

                Updatepanel1.Update();
            }
            else
            {
                productList.DataSource = null;
                productList.DataSource = proTab;
                productList.DataBind();
            }


            if (prodTab.Rows.Count > 20)
            {
                ((DataPager)productList.FindControl("productDataPager")).Visible = true;
            }
            else
            {

                if (((DataPager)productList.FindControl("productDataPager")) != null && ((DataPager)productList.FindControl("productDataPager")).Visible == true)
                {
                    ((DataPager)productList.FindControl("productDataPager")).Visible = false;
                }
            }

        }

Best Answer

I think that the getWeb(productkey); should be called outside the "if(!PostBack)" because changing a page creates a postback (not a fresh Get), so your table will be null (or at least this is how it works in grids).

Related Topic