Question

So, here is the scenarion I have a webpart that loads some data. I have in another place of the page a button, that opens a popup, when the user does something in that page (create action), the popup is closed and the calling page is reloaded.

When this happen, I can see in the browser that the page is reloaded, but my new data is not shown on the webpart.

I am binding the data in the create child controls, I tried to do it in the page load but then it doesnt work. If I put my cursor on the address bar then it shows me the new data, but if I press F5 it doesnt.

 public class LastCreatedJobs : WebPart
    {
        private GridView _lastCreatedJobsGrid;

        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            CreateGridControl();
        }

        private void CreateGridControl()
        {
            try
            {
                _lastCreatedJobsGrid = new GridView();
                _lastCreatedJobsGrid.RowDataBound += _lastCreatedJobsGrid_RowDataBound;

                var bJobCode = new BoundField { DataField = "JobCode", HeaderText = "Job Code" };
                bJobCode.ItemStyle.CssClass = "ms-cellstyle ms-vb2";
                bJobCode.HeaderStyle.CssClass = "ms-vh2";
                _lastCreatedJobsGrid.Columns.Add(bJobCode);

                var jobName = new HyperLinkField
                {
                    DataNavigateUrlFields = new[] { "JobWebsite" },
                    HeaderText = "Job Name",
                    DataTextField = "JobName"
                };
                jobName.ItemStyle.CssClass = "la";
                jobName.HeaderStyle.CssClass = "ms-vh2";
                jobName.ControlStyle.CssClass = "ms-listlink";
                _lastCreatedJobsGrid.Columns.Add(jobName);

                var biPowerLink = new HyperLinkField
                {
                    Target = "_blank",
                    DataNavigateUrlFields = new[] { "IPowerLink" },
                    HeaderText = "iP Link",
                    Text = @"<img src='" + ResolveUrl("/_layouts/15/PWC/DMS/Images/iclient.gif") + "' /> "
                };
                biPowerLink.ItemStyle.CssClass = "ms-cellstyle ms-vb-icon";
                biPowerLink.HeaderStyle.CssClass = "ms-vh2";
                _lastCreatedJobsGrid.Columns.Add(biPowerLink);

                _lastCreatedJobsGrid.CssClass = "ms-listviewtable"; //Table tag?
                _lastCreatedJobsGrid.HeaderStyle.CssClass = "ms-viewheadertr ms-vhlt";
                _lastCreatedJobsGrid.RowStyle.CssClass = "ms-itmHoverEnabled ms-itmhover";

                _lastCreatedJobsGrid.AutoGenerateColumns = false;

                _lastCreatedJobsGrid.EmptyDataText = Constants.Messages.NoJobsFound;

                Controls.Add(_lastCreatedJobsGrid);
                LoadGridData();
            }
            catch (Exception ex)
            {
                LoggingService.LogError(LoggingCategory.Job, ex);
            }
        }

        private void _lastCreatedJobsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    JobInfo jobInfo = (JobInfo)e.Row.DataItem;

                    if (jobInfo.IsConfidential)
                    {
                        var newHyperLink = new HyperLink
                        {
                            Text = @"<img src='" + ResolveUrl("/_layouts/15/PWC/DMS/Images/spy-icon.gif") + "' /> ",
                            NavigateUrl = jobInfo.xlink
                        };
                        e.Row.Cells[2].Controls.RemoveAt(0);
                        e.Row.Cells[2].Controls.Add(newHyperLink);
                    }
                }
            }
            catch (Exception ex)
            {
                LoggingService.LogError(LoggingCategory.Job, ex);
            }
        }

        #region Private methods

        private void LoadGridData()
        {
            try
            {
                String currentUrl = SPContext.Current.Site.Url;

                var jobInfoList = new List<JobInfo>();

                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (var clientSiteCollection = new SPSite(currentUrl))
                    {
                        foreach (
                            SPWeb web in
                                clientSiteCollection.AllWebs.Where(
                                    c =>
                                    c.AllProperties[Constants.WebProperties.General.WebTemplate] != null &&
                                    c.AllProperties[Constants.WebProperties.General.WebTemplate].ToString() ==
                                    Constants.WebTemplates.JobWebPropertyName).OrderByDescending(d => d.Created).Take(5)
                            )
                        {
                            SPList jobInfoListSp = web.Lists.TryGetList(Constants.Lists.JobInfoName);
                            if (jobInfoListSp != null)
                            {
                                if (jobInfoListSp.Items.Count > 0)
                                {
                                    var value =
                                        new SPFieldUrlValue(
                                            jobInfoListSp.Items[0][Constants.FieldNames.Job.iPowerLink].ToString());

                                    jobInfoList.Add(new JobInfo
                                    {
                                        JobName = jobInfoListSp.Items[0][Constants.FieldNames.Job.JobName].ToString(),
                                        JobCode = jobInfoListSp.Items[0][Constants.FieldNames.Job.JobCode].ToString(),
                                        xlink= value.Url,
                                        JobWebsite = web.Url,
                                        IsConfidential = HelperFunctions.ConvertToBoolean(jobInfoListSp.Items[0][Constants.FieldNames.Job.Confidential].ToString())
                                    });
                                }
                            }

                            web.Dispose();
                        }
                    }
                });

                _lastCreatedJobsGrid.DataSource = jobInfoList;
                _lastCreatedJobsGrid.DataBind();
            }
            catch (Exception ex)
            {
                LoggingService.LogError(LoggingCategory.Job, ex);
            }
        }

        #endregion
    }

and the page that opens the popup:

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <SharePoint:ScriptLink ID="ScriptLink1" runat="server" Name="sp.js" OnDemand="false" Localizable="false" LoadAfterUI="true"/>
    <script type="text/javascript">
        //Just an override from the ClientField.js - Nothing Special to do here
        function DisableClientValidateButton() {
        }

        function waitMessage() {
            window.parent.eval("window.waitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose('Creating Job','Working on the Job site.  Please wait.',90,450);");
        }

    </script>
</asp:Content>

and the code behind of that page, in a button click, just the important part, after the button is clicked.

  ClientScript.RegisterStartupScript(GetType(), "CloseWaitDialog",
                                               @"<script language='javascript'>
                        if (window.frameElement != null) {
                            if (window.parent.waitDialog != null) {
window.parent.waitDialog.close();                                
window.frameElement.commonModalDialogClose(1, 'New call was successfully logged.');

                            }
                        }
                        </script>");
Was it helpful?

Solution

The trick here is that the loadgrid data has to be executed in the OnPreRender.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top