【问题标题】:gridview does not refresh after delete in updatepanel在updatepanel中删除后gridview不刷新
【发布时间】:2013-09-26 13:00:16
【问题描述】:

我在更新面板中有一个网格视图,删除记录后不会刷新。

<ajax:UpdatePanel ID="SearchResultAjax" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:GridView runat="server" ID="mwOffersGrid" AutoGenerateColumns="False" Width="100%" DataKeyNames="OfferId" OnRowCommand="mwOffersGrid_RowCommand"   
    <Columns>
        <asp:TemplateField HeaderText="Offer #">
        <ItemTemplate>
            <asp:HyperLink runat="server" ID="offerIdLink" NavigateUrl='<%# String.Format("/Admin/Marketing/MWOffers/EditOffer.aspx?OfferId={0}",Eval("OfferId")) %>'
            Text='<%# Eval("OfferId") %>' />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemStyle HorizontalAlign="Right" Wrap="false" />
            <ItemTemplate>
            <asp:ImageButton ID="EditButton" runat="server" CommandName="EditOffer" CommandArgument='<%# Eval("OfferId") %>' ToolTip="Edit" SkinID="EditIcon" />
            <asp:ImageButton ID="DeleteButton" runat="server" CommandName="DeleteOffer" CommandArgument='<%# Eval("OfferId") %>'  ToolTip="Delete"
            SkinID="DeleteIcon" OnClientClick='<%# Eval("OfferId", "return confirm(\"Are you sure you want to delete Offer # {0}?\")") %>' />
            </ItemTemplate>
    </asp:TemplateField>
    </Columns>
</asp:GridView>
</ContentTemplate>
</ajax:UpdatePanel>

代码隐藏

protected void Page_Load(object sender, EventArgs e)
  {
    MWOffersCollection OffersCollection = MWOffersDataSource.LoadForCriteria(SqlCriteria, "OfferPageType ASC, OrderBy ASC");
    if (!Page.IsPostBack)
    {
      if (OffersCollection != null)
      {
            BindGrid();
      }
    }
  }

  public void BindGrid()
  {
    mwOffersGrid.DataSource = OffersCollection;
    mwOffersGrid.DataBind();
  }

protected void mwOffersGrid_RowCommand(object sender, GridViewCommandEventArgs e)
  {
    switch (e.CommandName)
    {

    case "EditOffer":
        var offerId = AlwaysConvert.ToInt(e.CommandArgument);
        Response.Redirect(string.Format("~/Admin/Marketing/MWOffers/EditOffer.aspx?OfferId={0}", offerId));
        break;

    case "DeleteOffer":
          var id = AlwaysConvert.ToInt(e.CommandArgument);
                    OffersCollection.Cast<MWOffers>().Single(o => o.OfferId == id).IsValid = false;
                    OffersCollection.Save();
                    BindGrid();
                    break;
        break;
    }
  }

【问题讨论】:

    标签: asp.net gridview updatepanel


    【解决方案1】:

    这是我更新的答案。问题在于页面加载时加载集合项。

    MWOffersCollection OffersCollection;
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!IsPostBack)
       {
          BindGrid();          
       }
    }
    
    private void BindGrid()
    {
     OffersCollection = MWOffersDataSource.LoadForCriteria(SqlCriteria, "OfferPageType ASC, OrderBy 
    ASC");
      mwOffersGrid.DataSource = OffersCollection;
        mwOffersGrid.DataBind();
    }
    
    protected void mwOffersGrid_RowCommand(object sender, GridViewCommandEventArgs e)
    {
       switch (e.CommandName)
       {
         case "EditOffer":
           var offerId = AlwaysConvert.ToInt(e.CommandArgument);
           Response.Redirect(string.Format("~/Admin/Marketing/MWOffers/EditOffer.aspx?OfferId={0}", offerId));
           break;
    
         case "DeleteOffer":
            var id = AlwaysConvert.ToInt(e.CommandArgument);
            DeleteRow(id);
            BindGrid(); //bind the grid again
           break;
       }
    }
    private void DeleteRow(int id)
    {
      var id = AlwaysConvert.ToInt(e.CommandArgument);
      OffersCollection.Cast<MWOffers>().Single(o => o.OfferId == id).IsValid = false;
      OffersCollection.Save();
    }
    

    【讨论】:

    • 我正在做我页面中需要的其他逻辑...我已经删除了它并更新了我的问题,请看一下。
    • 重定向到指定的 URL 不是我的项目的选项。
    【解决方案2】:

    我做过类似的工作,但是是通过编辑模板完成的。 这样就可以很容易地控制不同的绑定操作。 例如我的gridview标签代码是

     <asp:GridView ID="gvTrip" runat="server" AutoGenerateColumns="False" OnRowDataBound="gvTrip_RowDataBound"
                                                CellPadding="4" DataKeyNames="trip_code" ForeColor="#333333" GridLines="None"
                                                OnRowCancelingEdit="gvTrip_RowCancelingEdit" OnRowDeleting="gvTrip_RowDeleting"
                                                OnRowEditing="gvTrip_RowEditing" OnRowUpdating="gvTrip_RowUpdating" RowStyle-Height="10px">
    

    之后我给出了两个命令字段:

     <asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" ItemStyle-Width = "100px"/>
        <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ItemStyle-Width = "100px"/>
    

    将网格项目模板分为两部分项目和编辑例如:

    <asp:TemplateField HeaderText="Trip End Time">
    
    <ItemTemplate>
                                                            <asp:Label ID="lblgTripEndTime" runat="server" Text='<%# Bind("TRIP_END_TIME") %>' Width="100px"></asp:Label>
                                                        </ItemTemplate>
    
                                                        <EditItemTemplate>
                                                            <asp:TextBox ID="txtgTripEndTime" Visible="true" runat="server" 
                                                               Text='<%# Bind("TRIP_END_TIME") %>' MaxLength="5" CssClass="phoneNumber1" Width="100px"> </asp:TextBox>
    
    
                                                        </EditItemTemplate>           
    </EditItemTemplate>  
    

    现在我的删除代码会很简单

    protected void gvTrip_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
    
            string strTripCode = Convert.ToString(((Label)gvTrip.Rows[e.RowIndex].FindControl("lblgTripCode")).Text.Trim());
            objMasterTransport.deleteTripDetail(strTripCode);
    
            BindData();
            DropdownhelperENT.PopulateDistinctDropDown(drpDateSelection, "TRANS_M_TRIP", "TRIP_DATE");
        }
    

    【讨论】:

    • 你已经使用实体了。请检查您的页面加载条件是否正确!
    • @amit 你反应很快 :)
    【解决方案3】:

    试试这个

    var id = AlwaysConvert.ToInt(e.CommandArgument);
             OffersCollection.Cast<MWOffers>().Single(o => o.OfferId == id).IsValid = false;
                    OffersCollection.Save();
    BindGrid();
    SearchResultAjax.Update();
    break;
    

    这个SearchResultAjax.Update(); 应该在gridview 数据绑定发生后更新面板。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-16
      • 2012-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多