in

SharePoint University

Clean slate. Nothing but SharePoint.
Go, SharePoint!

AJAX, SPGridView, UserControl

Last post 07-14-2008 3:54 PM by wagswvu. 3 replies.
Page 1 of 1 (4 items)
Sort Posts: Previous Next
  • 07-11-2008 10:41 PM

    AJAX, SPGridView, UserControl

     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?

     

    protected 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 = searchResultsInfoArrayDevil;

    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();
    }
    }
    }

     

     

  • 07-12-2008 10:30 PM In reply to

    Re: AJAX, SPGridView, UserControl

     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?

  • 07-14-2008 3:17 PM In reply to

    Re: AJAX, SPGridView, UserControl

    I would conclude from that, that for some reason the web application isn't rendering the necessary scripts for the events needed for the GridView in the update panel and by adding another server control (I assume only a vanilla textbox with autopostback  = false), it for some reason decided to render those required items on the page.

     Have you tried manually making your Grid control an async trigger?  See if that'll enabled partial rendering without the textbox.

    http://aspadvice.com/blogs/name/archive/2007/07/17/Dynamically-Register-an-Asynchronous-Postback-Control-with-a-ScriptManager.aspx

     

     

  • 07-14-2008 3:54 PM In reply to

    Re: AJAX, SPGridView, UserControl

    Victory!!!!!!, You are the man.. It works fine now. Thanks alot
Page 1 of 1 (4 items)

Need SharePoint Training? Attend a SharePoint Bootcamp!
Forum content (c) original posters. Everything else (c) 2008 SharePoint Experts, Inc.