当页面回发时,Updatepanel使网格消失

时间:2019-01-09 09:06:09

标签: c# html asp.net grid

我添加了UpdatePanel以包括一个网格,以反映一些更改。但是,当我单击页面上的其他按钮以更新一些数据时,回发后网格将消失。

我尝试删除UpdatePanel,然后将显示网格并在网格上显示数据。

这是我的代码。

主要是,当页面首次进入时,将显示带有数据的网格。但是,当我单击页面上的按钮以更新一些数据时,更新应显示在网格上。该页面是回发的,但是,当我这样做时,将不会显示网格。

我尝试删除aspx中的UpdatePanel,然后单击页面上的按钮以更新数据时,网格将再次显示。似乎UpdatePanel已使网格消失。单击页面上的按钮并发回后,我需要保留UpdatePanel并使网格不消失。

    <asp:UpdatePanel ID="UpdatePanel3" runat="server">
   <ContentTemplate>
      <telerik:RadGrid ID="uiGrid" runat="server"
         AutoGenerateColumns="false" ItemStyle-BackColor="White" AlternatingItemStyle-BackColor="White"
         OnDataBound="uiGrid_DataBound" OnItemDataBound="uiGrid_ItemDataBound" OnNeedDataSource="uiGrid_NeedDataSource"
         ClientSettings-ClientEvents-OnKeyPress="keyPressInGrid"
         Width="100%" Height="500" EnableViewState="true">
         <ClientSettings>
            <Scrolling CountGroupSplitterColumnAsFrozen="false" AllowScroll="true" UseStaticHeaders="True" SaveScrollPosition="true" FrozenColumnsCount="3"></Scrolling>
         </ClientSettings>
         <MasterTableView>
            <GroupByExpressions>
               <telerik:GridGroupByExpression>
                  <SelectFields>
                     <telerik:GridGroupByField FieldAlias="<%$ Resources:Resource,Group %>" FieldName="IndicatorParentName"></telerik:GridGroupByField>
                  </SelectFields>
                  <GroupByFields>
                     <telerik:GridGroupByField FieldName="IndicatorParentName" SortOrder="Ascending"></telerik:GridGroupByField>
                  </GroupByFields>
               </telerik:GridGroupByExpression>
            </GroupByExpressions>
            <Columns>
               <telerik:GridBoundColumn DataField="IndicatorName" HeaderText="<%$ Resources:Resource,Indicator %>" UniqueName="IndicatorName"
                  SortExpression="IndicatorName" DataType="System.String" ItemStyle-Wrap="false">
               </telerik:GridBoundColumn>
               <telerik:GridTemplateColumn>
                  <ItemTemplate>
                     <asp:HiddenField ID="uiIndicatorID" runat="server" Value='<%#Eval("IndicatorID")%>' />
                     <asp:HiddenField ID="uiIndicatorName" runat="server" Value='<%#Eval("IndicatorName")%>' />
                     <asp:ImageButton ID="uiTipsButton" runat="server" ImageUrl="images/information-icon.png?20170703" Width="16" Height="16" />
                  </ItemTemplate>
               </telerik:GridTemplateColumn>
               <telerik:GridTemplateColumn HeaderText="<%$ Resources:Resource,Unit %>">
                  <ItemTemplate>
                     <asp:Label ID="uiUnit" runat="server" />
                  </ItemTemplate>
               </telerik:GridTemplateColumn>
            </Columns>
         </MasterTableView>
      </telerik:RadGrid>
   </ContentTemplate>
</asp:UpdatePanel>

这是背后的代码:

protected void uiImport_Click(object sender, EventArgs e)
    {
        // clear message
        uiErrorMsg.Text = "";
        uiSuccessMsg2.Text = "";

        StringBuilder message = new StringBuilder(1024);

        HttpResponseMessage result = null;

        if (uiUpload.UploadedFiles.Count > 0)
        {
            result = ImportData();

            if (result.IsSuccessStatusCode)
            {
                message.Append(Convert.ToString(HttpContext.GetGlobalResourceObject("Resource", "Message_Import_Data_Successful")));
                message.Append(Environment.NewLine);
                try
                {
                    _usageData.Dispose();
                    _usageData = null;
                }
                catch { }

                BindGrid(true); // refresh grid.

                // update task modified by and date
                bool hasError = false;
                Common.Systems.Sustainability.Constants.ApprovalStatuses status = Common.Systems.Sustainability.Constants.ApprovalStatuses.Undefined;
                bool isApprovedOrRejected = WorkflowUtil.IsApprovedOrRejected(_taskID.Value, ref status);
                if (isApprovedOrRejected)
                {
                    // re-cache data for dashboards
                    int companyID = Common.WebUtil.GetCompanyID();
                    Thread childThread = new Thread(() => Util.ReCacheForDashboards(Cache, companyID));
                    childThread.Start();
                }
                else
                {
                    UpdateTask(ref hasError);
                }
            }
            else
            {
                string msg = Convert.ToString(HttpContext.GetGlobalResourceObject("Resource", "Message_Unable_To_Import_Data"));
                message.Append(msg);
                message.Append(Environment.NewLine);

                string resultMessage = result.Content.ReadAsStringAsync().Result;
                if (resultMessage.IndexOf("ExceptionMessage", StringComparison.OrdinalIgnoreCase) > -1)
                {
                    resultMessage = resultMessage.Replace("\\r\\n", "<br />");
                    Logger.Log(string.Format("Error occurs in the '{0}.{1}' method.{2}{3}"
                        , System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.ToString()
                        , System.Reflection.MethodBase.GetCurrentMethod().Name
                        , Environment.NewLine
                        , resultMessage));
                }
                else
                {
                    message.Append(Common.Util.ReformatMessage(resultMessage));
                }
            } // end if
        } // end if

        if (result != null)
        {
            if (result.IsSuccessStatusCode)
            {
                uiSuccessMsg2.Text = message.Replace(Environment.NewLine, "<br />").ToString();
            }
            else
            {
                // show error message on web.
                //uiErrorMsg.Text = message.Replace(Environment.NewLine, "<br />").ToString();
                //uiPanel2.Alert(message.ToString());
                uiReportPanel.Alert(message.ToString());
            } // end if
        } // end if
    }


private void BindGrid(bool performDataBind = true)
    {
        DataTable locationsTable = null;
        DataTable indicatorsTable = null;
        try
        {
            // try to get delegation, location and indicators information, it contains sequence order of locations and indicators
            // order numbers will be using for sorting in code below
            try
            {
                _delegation = GetDelegationByTaskID(_taskID.Value);
                int delegationID = Convert.ToInt32(_delegation.Tables[0].Rows[0][Common.Systems.Sustainability.Constants.Delegation.ID]);
                _delegationLocationsAndIndicators = GetDelegationLocationsAndIndicatorsByDelegationID(delegationID);
            }
            catch { }

            // remove all columns
            for (int i = 3; i < uiGrid.MasterTableView.Columns.Count; i++)
            {
                GridColumn col = uiGrid.MasterTableView.Columns[i];
                uiGrid.MasterTableView.Columns.Remove(col);
                i--;
            }

            DataSet usage = GetUsageData();
            //using (DataSet usage = GetUsageData())
            //{

            {
                locationsTable = usage.Tables[0].DefaultView.ToTable(true, new string[] {
                        Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.LocationID
                        , Common.Systems.Sustainability.Constants.Location.LocationName
                    }
                );

                // As requested by user in 2016-07, sorting location and indicator based on delegation selected ordering.
                if (_delegationLocationsAndIndicators != null)
                {
                    locationsTable.Columns.Add(Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForLocation, Type.GetType("System.Int32"));
                    foreach (DataRow dr in locationsTable.Rows)
                    {
                        DataRow[] dli = _delegationLocationsAndIndicators.Tables[0].Select(string.Format("{0} = {1}"
                                    , Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.LocationID
                                    , dr[ .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.LocationID]
                                ));
                        if (dli.Length > 0)
                        {
                            int? sequence =  .Common.Util.TryToConvertToInt32(dli[0],  .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForLocation);
                            if (sequence != null)
                            {
                                dr[ .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForLocation] = sequence.Value;
                            }
                        }
                    }

                    locationsTable.DefaultView.Sort =  .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForLocation;
                    DataTable temp = locationsTable.DefaultView.ToTable();
                    locationsTable.Dispose();
                    locationsTable = temp;
                }


                // append locations to grid view
                int i = 0;
                foreach (DataRow dr in locationsTable.Rows)
                {
                    string locationID = Convert.ToString(dr["LocationID"]);
                    string locationName = Convert.ToString(dr["LocationName"]);

                    GridTemplateColumn tplColumn = new GridTemplateColumn();
                    tplColumn.ItemTemplate = new UsageColumnTemplate(locationName, i.ToString(), locationID, _currencies);
                    tplColumn.HeaderText = locationName;
                    uiGrid.MasterTableView.Columns.Add(tplColumn);

                    i++;
                }


            }


            // filter out indicators if no location associated
            {
                indicatorsTable = usage.Tables[0].DefaultView.ToTable(true, new string[] {
                         .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.IndicatorID
                        ,  .Common.Systems.Sustainability.Constants.Indicator.IndicatorName
                        , "IndicatorParentName" // Added by HC on 2016-06-08, as requested by users, use parent indicator name as group on the grid view.
                    }
                );

                // As requested by user in 2016-07, sorting location and indicator based on delegation selected ordering.
                if (_delegationLocationsAndIndicators != null)
                {
                    indicatorsTable.Columns.Add( .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForIndicator, Type.GetType("System.Int32"));
                    foreach (DataRow dr in indicatorsTable.Rows)
                    {
                        DataRow[] dli = _delegationLocationsAndIndicators.Tables[0].Select(string.Format("{0} = {1}"
                                    ,  .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.IndicatorID
                                    , dr[ .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.IndicatorID]
                                ));
                        if (dli.Length > 0)
                        {
                            int? sequence =  .Common.Util.TryToConvertToInt32(dli[0],  .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForIndicator);
                            if (sequence != null)
                            {
                                dr[ .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForIndicator] = sequence.Value;
                            }
                        }
                    }

                    indicatorsTable.DefaultView.Sort =  .Common.Systems.Sustainability.Constants.DelegationLocationAndIndicator.SequenceForIndicator;
                    DataTable temp = indicatorsTable.DefaultView.ToTable();
                    indicatorsTable.Dispose();
                    indicatorsTable = temp;
                }

                // bind indicators to grid view
                uiGrid.DataSource = indicatorsTable;
                //if (performDataBind)
                //uiGrid.Rebind();
            }

            //} // end using
        }
        finally
        {
            if (locationsTable != null)
            {
                locationsTable.Dispose();
                locationsTable = null;
            }
            if (indicatorsTable != null)
            {
                indicatorsTable.Dispose();
                indicatorsTable = null;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

尝试一下:

...
        </telerik:RadGrid>
    </ContentTemplate>
   <triggers>
       <asp:asyncpostbacktrigger controlid="TheButtonIdThatUpdateGrid" />
   </triggers>
</asp:UpdatePanel>