Answered by:
AJAX, SPGridView, UserControl

Question
-
Hi All,
I am trying to enclose SPGridView inside an UpdatePanel control in a user control page (.ascx) Whenever I do sorting or paging, it still postbacks instead of partial page rendering.
How can I enable partial page rendering in a user control that is being loaded by a web part? Below is my code:
Code Snippet<
asp:UpdatePanel ID="UpdatePanel1" runat="server"><
ContentTemplate> <SharePoint:SPGridView runat="server" ID="listsView" AutoGenerateColumns="false" RowStyle-BackColor="#DDDDDD" AlternatingRowStyle-BackColor="#EEEEEE" OnRowEditing="listsView_RowEditing" OnSorting="listsView_Sorting" AllowSorting="True" AllowPaging="True" PageSize="6" onpageindexchanging="listsView_PageIndexChanging" onpageindexchanged="listsView_PageIndexChanged"/> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label></
ContentTemplate></
asp:UpdatePanel>Sunday, June 15, 2008 9:24 PM
Answers
-
Somebody in another forum had the answer to this issue. It turns out that for some reason the web application isn't rendering the necessary scripts for the events needed for just the GridView in the update panel and by adding another server control (a textbox), it would then render those required items on the page allow the the GridView to take advantage of the AJAX. To fix this issue, I had to add two lines of code one is ajaxScriptManager.RegisterAsyncPostBackControl(GridView) and then ajaxUpdatePanel.Update() in the eventhandler that would be called.
Below is the url to the article:
After that it works greats witout extra controls.
I hope this helps out anybody else that runs into this issue.
- Proposed as answer by wagswvu Monday, August 18, 2008 3:35 PM
- Marked as answer by Michael Washam - MSFT Monday, December 8, 2008 9:23 PM
Tuesday, July 15, 2008 5:45 PM
All replies
-
Hi,
Couple of things you can check are :
1) Try adding mode='Conditional' for the update panel
2) Make sure that you have a ScriptManager control loaded on page before the update panel is added :
<aspcriptManager runat="server" ID="ScriptManager1" EnablePartialRendering="true"></asp
criptManager>
Monday, June 16, 2008 6:08 AM -
Hi Laxmikant - I tried them both but they did not help. Any other suggestions?Monday, June 16, 2008 4:40 PM
-
diffident wrote: Hi Laxmikant - I tried them both but they did not help. Any other suggestions? I was wondering if you ever found a solution, because I seem to be having the same issue. Also, this is not the first time I have used ajax in my project. I have taking the same code that has worked on other controls and applied them to the SPGridvew however, it still does full postback on the paging links (which is mainly what I wanted it for). Any thoughts?
Code Snippetprotected override void CreateChildControls()
{
// Register the AJAX ScriptManager
ScriptManager ajaxScriptManager = ScriptManager.GetCurrent(this.Page);if (ajaxScriptManager == null)
{
ajaxScriptManager = new ScriptManager();
ajaxScriptManager.ID = "AJAXScriptManager";
ajaxScriptManager.EnablePartialRendering = true;
this.Controls.Add(ajaxScriptManager);
}EnsureAJAXUpdatePanelCompatibility();
ajaxUpdatePanel = new UpdatePanel();
ajaxUpdatePanel.ID = "AJAXUpdatePanel";
ajaxUpdatePanel.ChildrenAsTriggers = true;
ajaxUpdatePanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
this.Controls.Add(ajaxUpdatePanel);currentSite = SPContext.Current.Web;
using (ListCaml listCaml = new ListCaml(currentSite))
{
searchID = ((SearchLibraryResultsPage)this.Page).searchID;
searchResultInfo = SearchProcessController.GetSearchResultInfo(searchID);using (Fields fields = new Fields())
{
searchResultsInfoArray = fields.CreateStringValueArray(searchResultInfo);
}elapsedTime = Convert.ToDouble(searchResultsInfoArray[0]);
setNumber = Convert.ToInt16(searchResultsInfoArray[1]);
rowStart = Convert.ToInt16(searchResultsInfoArray[2]);
rowCount = Convert.ToInt16(searchResultsInfoArray[3]);
isNextSet = Convert.ToBoolean(searchResultsInfoArray[4]);
sortManagedProperty = searchResultsInfoArray[5];
sortDirection = searchResultsInfoArray[6];
searchResultsDT = (DataTable)Cache["SearchResults_" + searchID.ToString()];
searchCriteriaDT = (DataTable)Cache["SearchCriteria_" + searchID.ToString()];gridView = new SPGridView();
searchResultsDV = new DataView(searchResultsDT);
gridView.DataSource = searchResultsDV;//CreateViewColumnMenu();
searchCriteria = new StringBuilder();
foreach (DataRow libraryFieldsDR in searchCriteriaDT.Rows)
{
if (libraryFieldsDR["Value"].ToString() != string.Empty)
{
CreateSearchCriteriaList(libraryFieldsDR["Column_Name"].ToString(), libraryFieldsDR["Literal_Operator"].ToString(),
libraryFieldsDR["Data_Type"].ToString(),libraryFieldsDR["Value"].ToString());
}if (libraryFieldsDR["Column_Name"].ToString() != "Path")
{
SPBoundField boundField = new SPBoundField();
boundField.HeaderText = libraryFieldsDR["Column_Name"].ToString();
boundField.DataField = libraryFieldsDR["Managed_Property"].ToString();
boundField.SortExpression = libraryFieldsDR["Managed_Property"].ToString();
gridView.Columns.Add(boundField);
}
}
gridView.AutoGenerateColumns = false;
gridView.AllowSorting = true;
gridView.AllowPaging = true;
gridView.PagerSettings.Position = PagerPosition.Top;
gridView.PagerSettings.Mode = PagerButtons.NumericFirstLast;
gridView.PagerSettings.PageButtonCount = 5;
gridView.PageSize = listCaml.GetSearchResultLimits(Convert.ToInt16(Session["Current_LibraryID"]), SharePointSearch.resultsPerPage);
gridView.Sorting += new GridViewSortEventHandler(gridView_Sorting);
gridView.PageIndexChanging += new GridViewPageEventHandler(gridView_PageIndexChanging);
ajaxUpdatePanel.ContentTemplateContainer.Controls.Add(gridView);
//PagerTemplate must be added after you added the control, but before you bind your data
gridView.PagerTemplate = null;
gridView.DataBind();//AddResultsInformation();
//Preserves the sorting when page is reload
if ((!Page.IsPostBack) && (sortManagedProperty != string.Empty) && (sortDirection != string.Empty))
{
ViewState["SortExpression"] = sortManagedProperty;
ViewState["SortDirection"] = sortDirection;
AddSortColumnArrows(gridView.HeaderRow,sortManagedProperty, sortDirection);
gridView.DataBind();
}
}
}Friday, July 11, 2008 9:25 PM -
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?Sunday, July 13, 2008 3:30 AM
-
Somebody in another forum had the answer to this issue. It turns out that for some reason the web application isn't rendering the necessary scripts for the events needed for just the GridView in the update panel and by adding another server control (a textbox), it would then render those required items on the page allow the the GridView to take advantage of the AJAX. To fix this issue, I had to add two lines of code one is ajaxScriptManager.RegisterAsyncPostBackControl(GridView) and then ajaxUpdatePanel.Update() in the eventhandler that would be called.
Below is the url to the article:
After that it works greats witout extra controls.
I hope this helps out anybody else that runs into this issue.
- Proposed as answer by wagswvu Monday, August 18, 2008 3:35 PM
- Marked as answer by Michael Washam - MSFT Monday, December 8, 2008 9:23 PM
Tuesday, July 15, 2008 5:45 PM -
Hi All:
Did any one found the solution to it. I am also having the same issue.
Please let me know.
Thanks!
Ravi
RaviThursday, July 16, 2009 2:55 PM