4

我对 asp.net 有一个奇怪的问题GridView。我有GridView那个绑定到一个LinqDataSource. 在GridView的行删除事件中,我正在从数据库中删除行,但网格没有刷新(即使在绑定到数据源之后)。

当我放置断点时,我可以看到OnRowDeleting事件在事件之后被触发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();
}
4

3 回答 3

1

I can't point out error in your code but I am giving you my code which i did to delete the data.

<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 file

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");
    }
于 2013-01-14T10:34:59.837 回答
1

如果您的记录实际上被正确删除并且网格只是没有显示更改,我会尝试将 GridView 放在 UpdatePanel 中,看看这是否有帮助。

<asp:UpdatePanel runat="server" UpdateMode="Always">
    <ContentTemplate>
        // GridView
    </ContentTemplate>
</asp:UpdatePanel>
于 2012-11-16T11:06:09.287 回答
0

尝试EditIndex在 GridView 的方法上将 GridView 设置为 -1 ItemDeleting

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

并将删除逻辑移至RowCommandGridView 的方法:

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" />
于 2012-11-16T10:58:55.067 回答