Connecting Tech Pros Worldwide Forums | Help | Site Map

ASP.NET,AJAX, SPGridView

Newbie
 
Join Date: Mar 2008
Posts: 7
#1: Jul 12 '08
I am trying to enclose SPGridView inside an UpdatePanel control in a user control page (.ascx), however whenever I click on the paging links for the grid, it still postbacks instead of partial page rendering. Also, this is not the first time I have used ajax in my project. I have taken the same code that has worked on other controls and applied them to the SPGridvew with no success. Below is a sample of the user control page in question with my override childcontrols. Any thoughts?

Expand|Select|Wrap|Line Numbers
  1. protected override void CreateChildControls()
  2.         {
  3.             // Register the AJAX ScriptManager
  4.             ScriptManager ajaxScriptManager = ScriptManager.GetCurrent(this.Page);
  5.  
  6.             if (ajaxScriptManager == null)
  7.             {
  8.                 ajaxScriptManager = new ScriptManager();
  9.                 ajaxScriptManager.ID = "AJAXScriptManager";
  10.                 ajaxScriptManager.EnablePartialRendering = true;
  11.                 this.Controls.Add(ajaxScriptManager);
  12.             }
  13.  
  14.             EnsureAJAXUpdatePanelCompatibility();
  15.  
  16.             ajaxUpdatePanel = new UpdatePanel();
  17.             ajaxUpdatePanel.ID = "AJAXUpdatePanel";
  18.             ajaxUpdatePanel.ChildrenAsTriggers = true;
  19.             ajaxUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
  20.             this.Controls.Add(ajaxUpdatePanel);
  21.  
  22.             currentSite = SPContext.Current.Web;
  23.  
  24.             using (ListCaml listCaml = new ListCaml(currentSite))
  25.             {
  26.                 searchID = ((SearchLibraryResultsPage)this.Page).searchID;                
  27.                 searchResultInfo = SearchProcessController.GetSearchResultInfo(searchID);
  28.  
  29.                 using (Fields fields = new Fields())
  30.                 {
  31.                     searchResultsInfoArray = fields.CreateStringValueArray(searchResultInfo);
  32.                 }
  33.  
  34.                 elapsedTime = Convert.ToDouble(searchResultsInfoArray[0]);
  35.                 setNumber = Convert.ToInt16(searchResultsInfoArray[1]);
  36.                 rowStart = Convert.ToInt16(searchResultsInfoArray[2]);
  37.                 rowCount = Convert.ToInt16(searchResultsInfoArray[3]);
  38.                 isNextSet = Convert.ToBoolean(searchResultsInfoArray[4]);
  39.                 sortManagedProperty = searchResultsInfoArray[5];
  40.                 sortDirection = searchResultsInfoArray[6];
  41.  
  42.                 searchResultsDT = (DataTable)Cache["SearchResults_" + searchID.ToString()];
  43.                 searchCriteriaDT = (DataTable)Cache["SearchCriteria_" + searchID.ToString()];               
  44.  
  45.                 gridView = new SPGridView();
  46.                 searchResultsDV = new DataView(searchResultsDT);
  47.                 gridView.DataSource = searchResultsDV;             
  48.  
  49.                 //CreateViewColumnMenu();
  50.  
  51.                 searchCriteria = new StringBuilder();
  52.  
  53.                 foreach (DataRow libraryFieldsDR in searchCriteriaDT.Rows)
  54.                 {
  55.                     if (libraryFieldsDR["Value"].ToString() != string.Empty)
  56.                     {
  57.                         CreateSearchCriteriaList(libraryFieldsDR["Column_Name"].ToString(), libraryFieldsDR["Literal_Operator"].ToString(),
  58.                                                  libraryFieldsDR["Data_Type"].ToString(),libraryFieldsDR["Value"].ToString());                       
  59.                     }
  60.  
  61.                     if (libraryFieldsDR["Column_Name"].ToString() != "Path")
  62.                     {
  63.                         SPBoundField boundField = new SPBoundField();
  64.                         boundField.HeaderText = libraryFieldsDR["Column_Name"].ToString();
  65.                         boundField.DataField = libraryFieldsDR["Managed_Property"].ToString();                    
  66.                         boundField.SortExpression = libraryFieldsDR["Managed_Property"].ToString();
  67.                         gridView.Columns.Add(boundField);
  68.                     }
  69.                 }
  70.                 gridView.AutoGenerateColumns = false;
  71.                 gridView.AllowSorting = true;
  72.                 gridView.AllowPaging = true;
  73.                 gridView.PagerSettings.Position = PagerPosition.Top;
  74.                 gridView.PagerSettings.Mode = PagerButtons.NumericFirstLast;
  75.                 gridView.PagerSettings.PageButtonCount = 5;               
  76.                 gridView.PageSize = listCaml.GetSearchResultLimits(Convert.ToInt16(Session["Current_LibraryID"]), SharePointSearch.resultsPerPage);
  77.                 gridView.Sorting += new GridViewSortEventHandler(gridView_Sorting);
  78.                 gridView.PageIndexChanging += new GridViewPageEventHandler(gridView_PageIndexChanging);
  79.  
  80.  
  81.                 ajaxUpdatePanel.ContentTemplateContainer.Controls.Add(gridView);
  82.                 //PagerTemplate must be added after you added the control, but before you bind your data
  83.                 gridView.PagerTemplate = null;
  84.                 gridView.DataBind();
  85.  
  86.                 //AddResultsInformation();
  87.  
  88.                 //Preserves the sorting when page is reload
  89.                 if ((!Page.IsPostBack) && (sortManagedProperty != string.Empty) && (sortDirection != string.Empty))
  90.                 {
  91.                     ViewState["SortExpression"] = sortManagedProperty;
  92.                     ViewState["SortDirection"] = sortDirection;
  93.                     AddSortColumnArrows(gridView.HeaderRow,sortManagedProperty, sortDirection);
  94.                     gridView.DataBind();
  95.                 }
  96.             }
  97.         }
  98.  

Newbie
 
Join Date: Mar 2008
Posts: 7
#2: Jul 13 '08

re: ASP.NET,AJAX, SPGridView


Well after many of hours messing with this, I decided to just double check that the UpdatePanel was even working. So I added a TextBox and a Button with an EventHandler that would clear the text out of the TextBox. To my amazement, when I added those two controls the SPGridView started working... I did a few more experiments and soon I discovered that by just having a TextBox control visible on the screen would allow the SPGridView to work with the UpdatePanel. Take it off the page and SPGridView is back to full postbacks. I am now completely mystified on what’s going on, anybody have an idea?
Reply