C# 在基于Ajax搜索的Webcontrol上实现分页功能

C# 在基于Ajax搜索的Webcontrol上实现分页功能,c#,asp.net,ajax,paging,C#,Asp.net,Ajax,Paging,我目前正在使用带有updatemode条件的UpdatePanel构建搜索功能。此updatepanel的触发器是我的搜索短语文本框。当TextChanged触发时,我的面板将更新,并显示正确的搜索结果。 然而,我对如何实现与搜索结果一起工作的分页功能一无所知。 我希望分页链接显示在部分页面更新上,我希望分页器也更新我的搜索结果(只希望在一个页面上显示3个结果)。 如何正确设置此项 这是我的代码(ascx) 帕吉纳标志处: 函数ClientCallbackFunction(args) {

我目前正在使用带有updatemode条件的UpdatePanel构建搜索功能。此updatepanel的触发器是我的搜索短语文本框。当TextChanged触发时,我的面板将更新,并显示正确的搜索结果。 然而,我对如何实现与搜索结果一起工作的分页功能一无所知。 我希望分页链接显示在部分页面更新上,我希望分页器也更新我的搜索结果(只希望在一个页面上显示3个结果)。 如何正确设置此项

这是我的代码(ascx)




帕吉纳标志处: 函数ClientCallbackFunction(args) { } 选择1 选择2 选择3 选择4
代码隐藏:

// Needed to store the searchResults
     List<Item> validSearchResults = new List<Item>();

     private List<string> listOneSelectedItems = new List<string>();
     private List<string> listTwoSelectedItems = new List<string>();

     private string currentCategoryPath = string.Empty;

     private string _callbackArgs;


     /// <summary>
     /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
     /// </summary>
     /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
     protected override void OnInit(EventArgs e)
     {
         base.OnInit(e);

         this.UpdateSelectedItemLists();
         this.currentCategoryPath = Sitecore.Context.Item.Paths.FullPath;

         this.searchPhrase.TextChanged += new EventHandler(searchPhrase_TextChanged);

         this.filterOneCbl.SelectedIndexChanged += new EventHandler(filterOneCbl_SelectedIndexChanged);
         this.filterTwoCbl.SelectedIndexChanged += new EventHandler(filterTwoCbl_SelectedIndexChanged);
         this.ProductRepeater.ItemDataBound += new RepeaterItemEventHandler(ProductRepeater_ItemDataBound);
     }


     private void UpdateScreen()
     {
         // update the list of selectedItems for use with the selection of new Items from the index
         this.UpdateSelectedItemLists();
         this.GetSearchResults();

         ProductRepeater.DataSource = validSearchResults;
         ProductRepeater.DataBind();

         ProductenUpdate.Update();
     }

     void btnSearch_Click(object sender, EventArgs e)
     {
         // update the list of selectedItems for use with the selection of new Items from the index
         this.UpdateSelectedItemLists();
         this.GetSearchResults();

         ProductRepeater.DataSource = validSearchResults;
         ProductRepeater.DataBind();

         ProductenUpdate.Update();
     }

     private void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
     {
         Item dataItem = (Item)e.Item.DataItem;

         // if there is a dataItem
         if (dataItem != null)
         {
             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.Item)
             {
                 Sitecore.Web.UI.WebControls.Text Titel = (Sitecore.Web.UI.WebControls.Text)e.Item.FindControl("Titel");
                 Sitecore.Web.UI.WebControls.Text Auteur =  (Sitecore.Web.UI.WebControls.Text)e.Item.FindControl("Auteur");
                 Sitecore.Web.UI.WebControls.Text Intro = (Sitecore.Web.UI.WebControls.Text)e.Item.FindControl("Intro");
                 Sitecore.Web.UI.WebControls.Image Thumb = (Sitecore.Web.UI.WebControls.Image)e.Item.FindControl("Thumb");

                 if (Thumb != null)
                 {
                     Thumb.Item = dataItem;
                 }

                 if (Titel != null)
                 {
                     Titel.Item = dataItem;
                 }

                 if (Auteur != null)
                 {
                     Auteur.Item = dataItem;
                 }

                 if (Intro != null)
                 {
                     Intro.Item = dataItem;
                 }
             }
         }            
     }

     /// <summary>
     /// Handles the TextChanged event of the searchPhrase control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     private void searchPhrase_TextChanged(object sender, EventArgs e)
     {
         this.UpdateScreen();
     }

     /// <summary>
     /// Handles the SelectedIndexChanged event of the filterTwoCbl control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     private void filterTwoCbl_SelectedIndexChanged(object sender, EventArgs e)
     {
         this.UpdateScreen();
     }

     /// <summary>
     /// Handles the SelectedIndexChanged event of the filterOneCbl control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     private void filterOneCbl_SelectedIndexChanged(object sender, EventArgs e)
     {
         this.UpdateScreen();
     }

     /// <summary>
     /// Updates the selected item lists.
     /// </summary>
     private void UpdateSelectedItemLists()
     {
         foreach (ListItem thisItem in filterOneCbl.Items)
         {
             if (thisItem.Selected)
             {
                 if (!listOneSelectedItems.Contains(thisItem.Value.ToUpper()))
                 {
                     listOneSelectedItems.Add(thisItem.Value.ToUpper());
                 }
             }
         }

         foreach (ListItem thisItem in filterTwoCbl.Items)
         {
             if (thisItem.Selected)
             {
                 if (!listTwoSelectedItems.Contains(thisItem.Value.ToUpper()))
                 {
                     listTwoSelectedItems.Add(thisItem.Value.ToUpper());
                 }
             }
         }
     }

     /// <summary>
     /// Gets the search results.
     /// </summary>
     private void GetSearchResults()
     {
         SearchResultRetreiver retriever = new SearchResultRetreiver("productenSearch");
         IndexSearcher searcher = null;

         Hits mySearchResults = retriever.GetSearchResults(searchPhrase.Text.Trim(), ref searcher);

         if (mySearchResults != null)
         {
             for (int i = 0; i < mySearchResults.Length(); i++)
             {
                 Item newItem = Sitecore.Data.Indexing.Index.GetItem(mySearchResults.Doc(i),  Sitecore.Context.Database);
                 string itemTitel = SitecoreHelper.ShowItemTitel(newItem);
                 bool addItem = true;
                 if (itemTitel.StartsWith("$")) addItem = false;
                 if (itemTitel.StartsWith("_")) addItem = false;

                 if (addItem)
                 {
                     this.validSearchResults.Add(newItem);
                 }
             }
         }
     }

     private void FilterSearchResults()
     {

     }

     /// <summary>
     /// Handles the Load event of the Page control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     protected void Page_Load(object sender, EventArgs e)
     {
         Item contextItem = Sitecore.Context.Item;
         Item subwebsiteItem = contextItem.Axes.GetAncestors().Where(c => c.Axes.Level == 4).FirstOrDefault<Item>();

         Sitecore.Data.Fields.CheckboxField filterOneSelected = subwebsiteItem.Fields["Filter1Actief"];
         Sitecore.Data.Fields.CheckboxField filterTwoSelected = subwebsiteItem.Fields["Filter2Actief"];

         if (filterOneSelected.Checked)
         {
             TitelTwo.Item = subwebsiteItem;
         }

         if (filterTwoSelected.Checked)
         {
             TitelOne.Item = subwebsiteItem;
         }

         if (!Page.IsPostBack)
         {
             string callbackRef = Page.ClientScript.GetCallbackEventReference(this, "args", "ClientCallbackFunction", "");

             string callbackScript = "function MyServerCall(args)" +
                 "{" + callbackRef + "}";

             Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyServerCall", callbackScript, true);

             filterOnePlaceholder.Visible = false;
             filterTwoPlaceholder.Visible = false;

             if (subwebsiteItem != null)
             {                   
                 if (filterOneSelected.Checked)
                 {
                     filterOnePlaceholder.Visible = true;  

                     Item filterOneLocation =  Sitecore.Context.Database.GetItem(subwebsiteItem.Paths.FullPath + "/Filters/Filter1");
                     List<Item> filterOneList = new List<Item>();

                     filterOneList = filterOneLocation.GetChildren().Where(c => c.TemplateName.ToLower() == "filter").ToList<Item>();

                     foreach (Item filterItem in filterOneList)
                     {
                         ListItem thisItem = new ListItem(filterItem["Naam"], filterItem.ID.ToGuid().ToString());
                         this.filterOneCbl.Items.Add(thisItem);
                     }
                 }

                 if (filterTwoSelected.Checked)
                 {
                     filterTwoPlaceholder.Visible = true;

                     Item filterTwoLocation = Sitecore.Context.Database.GetItem(subwebsiteItem.Paths.FullPath + "/Filters/Filter2");
                     List<Item> filterTwoList = new List<Item>();

                     filterTwoList = filterTwoLocation.GetChildren().Where(c => c.TemplateName.ToLower() == "filter").ToList<Item>();

                     foreach (Item filterItem in filterTwoList)
                     {
                         ListItem thisItem = new ListItem(filterItem["Naam"], filterItem.ID.ToGuid().ToString());
                         this.filterTwoCbl.Items.Add(thisItem);
                     }
                 }
             }
         }           
     }

     #region ICallbackEventHandler Members

     /// <summary>
     /// Returns the results of a callback event that targets a control.
     /// </summary>
     /// <returns>The result of the callback.</returns>
     public string GetCallbackResult()
     {
         return this._callbackArgs;
     }

     /// <summary>
     /// Processes a callback event that targets a control.
     /// </summary>
     /// <param name="eventArgument">A string that represents an event argument to pass to the event handler.</param>
     public void RaiseCallbackEvent(string eventArgument)
     {
         this._callbackArgs = eventArgument;
     }
//需要存储搜索结果
List validSearchResults=新列表();
私有列表listOneSelectedItems=新列表();
私有列表listTwoSelectedItems=新列表();
私有字符串currentCategoryPath=string.Empty;
私有字符串_callbackArgs;
/// 
///引发事件。
/// 
///包含事件数据的对象。
受保护的覆盖无效OnInit(事件参数e)
{
碱基.奥尼特(e);
this.UpdateSelectedItemLists();
this.currentCategoryPath=Sitecore.Context.Item.Paths.FullPath;
this.searchPhrase.TextChanged+=新事件处理程序(searchPhrase\u TextChanged);
this.filterOneCbl.SelectedIndexChanged+=新事件处理程序(filterOneCbl\u SelectedIndexChanged);
this.filterTwoCbl.SelectedIndexChanged+=新事件处理程序(filterTwoCbl\u SelectedIndexChanged);
this.ProductRepeater.ItemDataBound+=新的RepeaterItemEventHandler(ProductRepeater\u ItemDataBound);
}
私有void UpdateScreen()
{
//更新selectedItems列表,以便与从索引中选择的新项一起使用
this.UpdateSelectedItemLists();
这是.GetSearchResults();
ProductRepeater.DataSource=有效搜索结果;
ProductRepeater.DataBind();
ProductenUpdate.Update();
}
无效btnSearch_单击(对象发送者,事件参数e)
{
//更新selectedItems列表,以便与从索引中选择的新项一起使用
this.UpdateSelectedItemLists();
这是.GetSearchResults();
ProductRepeater.DataSource=有效搜索结果;
ProductRepeater.DataBind();
ProductenUpdate.Update();
}
私有void ProductRepeater_ItemDataBound(对象发送方,RepeaterItemEventArgs e)
{
Item dataItem=(Item)e.Item.dataItem;
//如果有数据项
if(dataItem!=null)
{
如果(e.Item.ItemType==ListItemType.Item | | e.Item.ItemType==ListItemType.Item)
{
Sitecore.Web.UI.WebControls.Text Titel=(Sitecore.Web.UI.WebControls.Text)e.Item.FindControl(“Titel”);
Sitecore.Web.UI.WebControls.Text Auteur=(Sitecore.Web.UI.WebControls.Text)e.Item.FindControl(“Auteur”);
Sitecore.Web.UI.WebControls.Text Intro=(Sitecore.Web.UI.WebControls.Text)e.Item.FindControl(“Intro”);
Sitecore.Web.UI.WebControls.Image Thumb=(Sitecore.Web.UI.WebControls.Image)e.Item.FindControl(“Thumb”);
if(Thumb!=null)
{
Thumb.Item=dataItem;
}
如果(滴度!=null)
{
Titel.Item=数据项;
}
如果(AUTUR!=null)
{
Auteur.Item=数据项;
}
如果(简介!=null)
{
简介项=数据项;
}
}
}            
}
/// 
///处理SearchPhase控件的TextChanged事件。
/// 
///事件的来源。
///包含事件数据的实例。
private void SearchPhase_TextChanged(对象发送方,事件参数e)
{
this.UpdateScreen();
}
/// 
///处理FilterWOCBL控件的SelectedIndexChanged事件。
/// 
///事件的来源。
///包含事件数据的实例。
私有无效筛选器WOCBL\u SelectedIndexChanged(对象发送方,事件参数e)
{
this.UpdateScreen();
}
/// 
///处理filterOneCbl控件的SelectedIndexChanged事件。
/// 
///事件的来源。
///包含事件数据的实例。
私有无效筛选器已选择索引已更改(对象发件人,事件参数e)
{
this.UpdateScreen();
}
/// 
///更新所选项目列表。
/// 
私有void UpdateSelectedItemLists()
{
foreach(filterOneCbl.Items中的ListItem thisItem)
{
如果(已选择此项)
{
如果(!listOneSelectedItems.Contains(thisItem.Value.ToUpper()))
{
listOneSelectedItems.Add(thisItem.Value.ToUpper());
}
}
}
foreach(过滤器WOCBL.Items中的ListItem thisItem)
{
如果(已选择此项)
{
如果(!listTwoSelectedItems.Contains(thisItem.Value.ToUpper()))
{
listTwoSelectedItems.Add(thisItem.Value.ToUpper());
}
}
}
}
/// 
///获取搜索结果
// Needed to store the searchResults
     List<Item> validSearchResults = new List<Item>();

     private List<string> listOneSelectedItems = new List<string>();
     private List<string> listTwoSelectedItems = new List<string>();

     private string currentCategoryPath = string.Empty;

     private string _callbackArgs;


     /// <summary>
     /// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
     /// </summary>
     /// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
     protected override void OnInit(EventArgs e)
     {
         base.OnInit(e);

         this.UpdateSelectedItemLists();
         this.currentCategoryPath = Sitecore.Context.Item.Paths.FullPath;

         this.searchPhrase.TextChanged += new EventHandler(searchPhrase_TextChanged);

         this.filterOneCbl.SelectedIndexChanged += new EventHandler(filterOneCbl_SelectedIndexChanged);
         this.filterTwoCbl.SelectedIndexChanged += new EventHandler(filterTwoCbl_SelectedIndexChanged);
         this.ProductRepeater.ItemDataBound += new RepeaterItemEventHandler(ProductRepeater_ItemDataBound);
     }


     private void UpdateScreen()
     {
         // update the list of selectedItems for use with the selection of new Items from the index
         this.UpdateSelectedItemLists();
         this.GetSearchResults();

         ProductRepeater.DataSource = validSearchResults;
         ProductRepeater.DataBind();

         ProductenUpdate.Update();
     }

     void btnSearch_Click(object sender, EventArgs e)
     {
         // update the list of selectedItems for use with the selection of new Items from the index
         this.UpdateSelectedItemLists();
         this.GetSearchResults();

         ProductRepeater.DataSource = validSearchResults;
         ProductRepeater.DataBind();

         ProductenUpdate.Update();
     }

     private void ProductRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
     {
         Item dataItem = (Item)e.Item.DataItem;

         // if there is a dataItem
         if (dataItem != null)
         {
             if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.Item)
             {
                 Sitecore.Web.UI.WebControls.Text Titel = (Sitecore.Web.UI.WebControls.Text)e.Item.FindControl("Titel");
                 Sitecore.Web.UI.WebControls.Text Auteur =  (Sitecore.Web.UI.WebControls.Text)e.Item.FindControl("Auteur");
                 Sitecore.Web.UI.WebControls.Text Intro = (Sitecore.Web.UI.WebControls.Text)e.Item.FindControl("Intro");
                 Sitecore.Web.UI.WebControls.Image Thumb = (Sitecore.Web.UI.WebControls.Image)e.Item.FindControl("Thumb");

                 if (Thumb != null)
                 {
                     Thumb.Item = dataItem;
                 }

                 if (Titel != null)
                 {
                     Titel.Item = dataItem;
                 }

                 if (Auteur != null)
                 {
                     Auteur.Item = dataItem;
                 }

                 if (Intro != null)
                 {
                     Intro.Item = dataItem;
                 }
             }
         }            
     }

     /// <summary>
     /// Handles the TextChanged event of the searchPhrase control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     private void searchPhrase_TextChanged(object sender, EventArgs e)
     {
         this.UpdateScreen();
     }

     /// <summary>
     /// Handles the SelectedIndexChanged event of the filterTwoCbl control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     private void filterTwoCbl_SelectedIndexChanged(object sender, EventArgs e)
     {
         this.UpdateScreen();
     }

     /// <summary>
     /// Handles the SelectedIndexChanged event of the filterOneCbl control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     private void filterOneCbl_SelectedIndexChanged(object sender, EventArgs e)
     {
         this.UpdateScreen();
     }

     /// <summary>
     /// Updates the selected item lists.
     /// </summary>
     private void UpdateSelectedItemLists()
     {
         foreach (ListItem thisItem in filterOneCbl.Items)
         {
             if (thisItem.Selected)
             {
                 if (!listOneSelectedItems.Contains(thisItem.Value.ToUpper()))
                 {
                     listOneSelectedItems.Add(thisItem.Value.ToUpper());
                 }
             }
         }

         foreach (ListItem thisItem in filterTwoCbl.Items)
         {
             if (thisItem.Selected)
             {
                 if (!listTwoSelectedItems.Contains(thisItem.Value.ToUpper()))
                 {
                     listTwoSelectedItems.Add(thisItem.Value.ToUpper());
                 }
             }
         }
     }

     /// <summary>
     /// Gets the search results.
     /// </summary>
     private void GetSearchResults()
     {
         SearchResultRetreiver retriever = new SearchResultRetreiver("productenSearch");
         IndexSearcher searcher = null;

         Hits mySearchResults = retriever.GetSearchResults(searchPhrase.Text.Trim(), ref searcher);

         if (mySearchResults != null)
         {
             for (int i = 0; i < mySearchResults.Length(); i++)
             {
                 Item newItem = Sitecore.Data.Indexing.Index.GetItem(mySearchResults.Doc(i),  Sitecore.Context.Database);
                 string itemTitel = SitecoreHelper.ShowItemTitel(newItem);
                 bool addItem = true;
                 if (itemTitel.StartsWith("$")) addItem = false;
                 if (itemTitel.StartsWith("_")) addItem = false;

                 if (addItem)
                 {
                     this.validSearchResults.Add(newItem);
                 }
             }
         }
     }

     private void FilterSearchResults()
     {

     }

     /// <summary>
     /// Handles the Load event of the Page control.
     /// </summary>
     /// <param name="sender">The source of the event.</param>
     /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
     protected void Page_Load(object sender, EventArgs e)
     {
         Item contextItem = Sitecore.Context.Item;
         Item subwebsiteItem = contextItem.Axes.GetAncestors().Where(c => c.Axes.Level == 4).FirstOrDefault<Item>();

         Sitecore.Data.Fields.CheckboxField filterOneSelected = subwebsiteItem.Fields["Filter1Actief"];
         Sitecore.Data.Fields.CheckboxField filterTwoSelected = subwebsiteItem.Fields["Filter2Actief"];

         if (filterOneSelected.Checked)
         {
             TitelTwo.Item = subwebsiteItem;
         }

         if (filterTwoSelected.Checked)
         {
             TitelOne.Item = subwebsiteItem;
         }

         if (!Page.IsPostBack)
         {
             string callbackRef = Page.ClientScript.GetCallbackEventReference(this, "args", "ClientCallbackFunction", "");

             string callbackScript = "function MyServerCall(args)" +
                 "{" + callbackRef + "}";

             Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "MyServerCall", callbackScript, true);

             filterOnePlaceholder.Visible = false;
             filterTwoPlaceholder.Visible = false;

             if (subwebsiteItem != null)
             {                   
                 if (filterOneSelected.Checked)
                 {
                     filterOnePlaceholder.Visible = true;  

                     Item filterOneLocation =  Sitecore.Context.Database.GetItem(subwebsiteItem.Paths.FullPath + "/Filters/Filter1");
                     List<Item> filterOneList = new List<Item>();

                     filterOneList = filterOneLocation.GetChildren().Where(c => c.TemplateName.ToLower() == "filter").ToList<Item>();

                     foreach (Item filterItem in filterOneList)
                     {
                         ListItem thisItem = new ListItem(filterItem["Naam"], filterItem.ID.ToGuid().ToString());
                         this.filterOneCbl.Items.Add(thisItem);
                     }
                 }

                 if (filterTwoSelected.Checked)
                 {
                     filterTwoPlaceholder.Visible = true;

                     Item filterTwoLocation = Sitecore.Context.Database.GetItem(subwebsiteItem.Paths.FullPath + "/Filters/Filter2");
                     List<Item> filterTwoList = new List<Item>();

                     filterTwoList = filterTwoLocation.GetChildren().Where(c => c.TemplateName.ToLower() == "filter").ToList<Item>();

                     foreach (Item filterItem in filterTwoList)
                     {
                         ListItem thisItem = new ListItem(filterItem["Naam"], filterItem.ID.ToGuid().ToString());
                         this.filterTwoCbl.Items.Add(thisItem);
                     }
                 }
             }
         }           
     }

     #region ICallbackEventHandler Members

     /// <summary>
     /// Returns the results of a callback event that targets a control.
     /// </summary>
     /// <returns>The result of the callback.</returns>
     public string GetCallbackResult()
     {
         return this._callbackArgs;
     }

     /// <summary>
     /// Processes a callback event that targets a control.
     /// </summary>
     /// <param name="eventArgument">A string that represents an event argument to pass to the event handler.</param>
     public void RaiseCallbackEvent(string eventArgument)
     {
         this._callbackArgs = eventArgument;
     }