我有這個奇怪的問題與C#asp.net listview,我無法指出確切的原因和問題。這裏是場景數據源填充,列表視圖綁定,但顯示EmptyDataTemplate
我有一個使用AutoCompleteExtender的搜索文本框。在PageLoad()中,listview將填充從DataTable中提取的一堆數據。當某人在文本框中輸入內容時,從webservice獲取結果,將結果填充到DataTable中,並且listview將綁定到DataTable。
一切工作正常 - listview綁定與DataPager最初正常工作。在列表視圖的第一頁,如果用戶輸入搜索,listview將綁定並顯示新的結果。
但是,當我在第2頁或更多時,listview綁定但顯示EmptyDataTemplate。我檢查了DataTable,並確定它在listview.DataBind之前填充了新數據。這個問題只發生在我離開列表視圖的第1頁時。
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="|<< " />
<asp:NumericPagerField ButtonCount="10" />
<asp:NextPreviousPagerField ButtonType="Image" ShowLastPageButton="true" ShowNextPageButton="false" ShowPreviousPageButton="false" LastPageText=" >>|" />
</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>
代碼隱藏
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;
}
}
}
你可以發佈你的HTML標記和代碼? –
添加了代碼。在問題出在哪裏,你最初的想法是什麼? – Dee
當這個'createTable'被調用? –