2012-11-08 137 views
4

我遇到一個奇怪的問題,與asp.net GridView。我有GridView,它綁定到LinqDataSource。在GridView的行刪除事件中,我正在刪除數據庫中的行,但網格未刷新(即使在綁定到數據源之後)。刪除事件後GridView不刷新

當我放置一個斷點時,我可以看到在LinqDS_Selecting事件之後觸發了​​事件。但是在刪除事件後它沒有再次被解僱!這可能是原因嗎?我究竟做錯了什麼?

有人請幫忙。提前謝謝了。

.aspx文件:

<asp:LinqDataSource 
    ID="LinqDS" 
    runat="server" 
    OnSelecting="LinqDS_Selecting"> 
</asp:LinqDataSource> 

<asp:GridView 
    DataSourceID = "LinqDS" 
    ID = "gv1" 
    runat = "server" 
    DataKeyNames = "InstructionId" 
    EnableViewState = "false" 
    OnRowDataBound = "gv1_RowDataBound" 
    OnRowDeleting = "gv1_RowDeleting" 
    OnRowCommand = "gv1_RowCommand" 
    PageSize = "30" > 
    <Columns> 
     <!-- My colums ---> 
    </Columns> 
</asp:GridView> 

.aspx.cs

protected void Page_Load(object sender, EventArgs e) 
{ 

} 

protected void LinqDS_Selecting(object sender, LinqDataSourceSelectEventArgs e) 
{ 
    // my linq to sql query 
    var query = from .... .... ; 
    e.Result = query; 
} 

protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
    int instructionId = (int)gv1.DataKeys[e.RowIndex].Value; 
    /// my delete logic 
    CTX.SubmitChanges(); 
    gv1.DataBind(); 
} 
+0

難道'gv1_RowDeleting'解僱?如果是的話,也許你還需要製作'LinqDS.DataBind()'? – Aristos

+0

gv1_RowDeleting事件是否觸發並且是您試圖從數據庫中實際刪除的值? –

回答

0

嘗試設置在GridView的EditIndex爲-1的ItemDeleting方法在GridView的:

protected void gv1_RowDeleting(object sender, GridViewDeleteEventArgs e) { 
    gv1.EditIndex = -1; 
    gv1.DataBind(); 
} 

並將您的刪除邏輯移至G的RowCommand方法ridView:

protected void gv1_RowCommand(object sender, GridViewDeleteEventArgs e) { 
    If (e.CommandName == "Delete") { 
     int instructionId = (int) gv1.DataKeys[e.RowIndex].Value; 
     /// my delete logic 
     CTX.SubmitChanges(); 
    } 
} 

您的刪除動作控制應該是這樣的:

<asp:LinkButton ID="lbDel" runat="server" Text="Delete" CommandName="Delete" /> 
1

如果記錄實際上是正確刪除,網格是單純只是沒有表現出改變我會嘗試,並把在UpdatePanel內的GridView,看看這是否有幫助。

<asp:UpdatePanel runat="server" UpdateMode="Always"> 
    <ContentTemplate> 
     // GridView 
    </ContentTemplate> 
</asp:UpdatePanel> 
1

我不能指出你的代碼錯誤,但我給你我的代碼,我沒有刪除數據。

<asp:GridView ID="GridView1" runat="server" OnRowDeleting="Del" DataKeyNames="Clg_ID" BackColor="#DEBA84" 
       BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" 
       CellSpacing="2"> 
       <Columns> 
        <asp:CommandField ShowDeleteButton="True" /> 
       </Columns> 
       <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" /> 
       <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" /> 
       <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" /> 
       <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" /> 
       <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" /> 
       <SortedAscendingCellStyle BackColor="#FFF1D4" /> 
       <SortedAscendingHeaderStyle BackColor="#B95C30" /> 
       <SortedDescendingCellStyle BackColor="#F1E5CE" /> 
       <SortedDescendingHeaderStyle BackColor="#93451F" /> 
      </asp:GridView> 

.cs文件

protected void Del(object sender, GridViewDeleteEventArgs e) 
    { 
     string i = GridView1.DataKeys[0].Value.ToString(); 
     SqlConnection cn = new SqlConnection(); 
     cn.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString.ToString(); 
     cn.Open(); 
     SqlCommand cmd = new SqlCommand("Delete from Feedback where Clg_ID='" + i + "'", cn); 
     cmd.ExecuteNonQuery(); 
     cmd.Dispose(); 
     cn.Close(); 
     Response.Redirect("MFeedback.aspx"); 
    }