当您打开只有一条记录的页面时会出现问题。它通过三个链接填充NavMenu; “第一”,“1”和“最后”。
出于某种原因,当您运行将返回多个页面的搜索查询时,它仍然只显示“First”,“1”和“Last”。同样,如果您从四个页面开始,后续搜索查询只返回两个记录,它仍会显示“First”,“1”,“2”,“3”,“4”和“Last”。因此,出于某种原因,无论你开始使用多少页面,你都会得到。如何重置页面计数器/显示器?
这是我的C#代码隐藏:
public void RunTheSearch()
{
//Run the Stored Procedure first
SqlConnection connection2 = new SqlConnection(strCon1);
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.CommandText = "sp_Search";
cmd2.Connection = connection2;
//--- A bunch of code that returns a dataset. Lengthy and unnecessary to my issue
connection2.Open();
SqlDataAdapter adp = new SqlDataAdapter(cmd2);
DataSet ds = new DataSet();
adp.Fill(ds, "OLDPages");
//Pagination code so only a set number of records loads at a time.
// Done to speed up the loading, since this list gets really long.
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["OLDPages"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 10;
//NavMenu.Items.Clear();
int currentPage;
if (Request.QueryString["page"] != null)
{
currentPage = Int32.Parse(Request.QueryString["page"]);
}
else
{
currentPage = 1;
}
pds.CurrentPageIndex = currentPage - 1;
//Label1.Text = "Page " + currentPage + " of " + pds.PageCount;
if (!pds.IsFirstPage)
{
MenuItem itemMessage = NavMenu.FindItem("First");
itemMessage.NavigateUrl = Request.CurrentExecutionFilePath + "?page=1";
}
AcctRepeater.DataSource = pds;
AcctRepeater.DataBind();
CreatePagingControl(pds.PageCount, pds.CurrentPageIndex);
// End of Pagination code
connection2.Close();
}
private void CreatePagingControl(int PCount, int PIndex)
{
int PIndex2 = 0;
int SCounter = PIndex + 1;
int RowCount = PCount;
//Allow the pagination menu to always start 5 less than the current page you're on
if (PIndex < 5)
{
PIndex2 = 0;
}
else
{
PIndex2 = PIndex - 5;
}
// Show 10 total page numbers. You can increase or shrink that range by changing the 10 to whatever number you want
for (int i = PIndex2; i < PIndex2 + 10 && i < PCount; i++)
{
NavMenu.Items.Add(new MenuItem
{
Text = (i + 1).ToString(),
NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (i + 1).ToString()
});
// Now determine the selected item so the proper CSS can be applied
foreach (MenuItem item in NavMenu.Items)
{
item.Selected = item.Text.Equals(SCounter.ToString());
}
}
NavMenu.Items.Add(new MenuItem
{
Text = "Last",
NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + (PCount)
});
}
在aspx页面上:
<asp:Menu ID="NavMenu" runat="server" CssClass="menu"
IncludeStyleBlock="false" Orientation="Horizontal" width="703px"
BackColor="#CC3300" EnableViewState="true">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="First" Selectable="true" />
</Items>
</asp:Menu>
我确实尝试过NavMenu.Items.Clear(),但它不喜欢它,因为它也清除了aspx端的硬编码项。
答案 0 :(得分:1)
我无法重现它。
我的直觉告诉我你没有回复,这就是你需要清除()结果的原因。
这个C#代码工作正常。
protected void Page_Load(object sender, EventArgs e)
{
RunTheSearch();
}
答案 1 :(得分:1)
您正在从名为sp_Search
的存储过程中检索数据,但所有运行中的查询都是相同的,因为您未在存储过程中指定任何参数(基于您发布的代码)。我通过修改存储过程并向其发送参数来测试您的代码,并且如您所述使用NavMenu.Items.Clear()
并且它对我来说很好:
你的SP应该是这样的:
CREATE PROCEDURE [dbo].[sp_Search]
@param1 NVARCHAR(50)
AS
SELECT * from yourTableName where SearchField = @param1
RETURN 0
在c#中:
public void RunTheSearch(string id)
{
...
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.CommandText = "sp_Search";
cmd2.Parameters.Add("@param1", SqlDbType.NVarChar, 50).Value = id;
...
...
因此,在Page_Load
中通过传递返回一条记录的参数来调用RunTheSearch
方法:
protected void Page_Load(object sender, EventArgs e)
{
RunTheSearch("p1");
}
其他地方通过传递一个返回多个记录的参数调用RunTheSearch
方法,结果将是多个页面:
protected void Button1_OnClick(object sender, EventArgs e)
{
NavMenu.Items.Clear();
RunTheSearch("p2");
}
答案 2 :(得分:0)
最终通过将菜单放在“更新”面板中解决了这个问题。所以,在aspx方面,我现在有:
<div class="clear hideSkiplink" id="NavDiv" style="margin:0 auto; display: table;">
<asp:UpdatePanel ID="NavUpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Menu ID="NavMenu" runat="server" CssClass="menu"
IncludeStyleBlock="false" Orientation="Horizontal" width="703px"
BackColor="#CC3300" EnableViewState="false">
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="First" Selectable="true" />
</Items>
</asp:Menu>
</ContentTemplate>
</asp:UpdatePanel>
</div>
谢谢大家一起去吧。事实上,在回复帖子的时候,我脑子里开了一个开关,我有了尝试的想法。