在updatepanel中删除后,gridview不会刷新

时间:2013-09-26 13:00:16

标签: asp.net gridview updatepanel

我在updatepanel里面有一个gridview,在删除记录后没有刷新。

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

3 个答案:

答案 0 :(得分: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();
}

答案 1 :(得分:1)

我做过类似的工作但是通过编辑模板完成了。 通过它能够非常容易地控制不同的绑定操作。 例如我的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"/>

将网格项目模板分为两部分,并编辑testlpe:

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

答案 2 :(得分:1)

试试这个

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数据绑定后更新面板。