2013-09-26 156 views
0

我有一個gridview內updatepanel刪除記錄後不刷新。gridview不刷新後updatepanel中刪除

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

回答

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

我正在做其他邏輯,我需要在我的網頁...我已經刪除了,並更新我的問題,請看看。 –

+0

@ patel.milanb查看我更新的答案 –

+0

重定向到指定的URL不是我的項目的選項。 –

1

我已經完成了類似的工作,但通過Edit模板完成了。由此可以非常容易地控制不同的綁定操作。 例如我的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"/> 

有兩個部分項目和編輯網格項目模板examlpe:

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

我已經更新了我的問題請看看... –

+0

你已經使用了它的實體。請檢查您的頁面加載情況是否正確! –

+0

@amit你很快回應:) –

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的數據綁定已發生之後更新面板。