C# asp.net リストビューでこの奇妙な問題が発生しており、正確な原因と問題を特定できません。これがシナリオです
AutoCompleteExtender を使用する検索テキスト ボックスがあります。PageLoad() で、リストビューには DataTable から抽出された一連のデータが取り込まれます。誰かがテキストボックスに何かを入力すると、Web サービスから結果を取得し、結果を DataTable に入力すると、リストビューが DataTable にバインドされます。
すべて正常に動作します - listview は、DataPager が最初に正常に動作する状態で正常にバインドします。リストビューの最初のページで、ユーザーが検索を入力すると、リストビューがバインドされ、新しい結果が表示されます。
ただし、2 ページ目以降の場合、リストビューはバインドされますが、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;
}
}
}