GridView内部不刷新updatepanal

时间:2017-11-01 07:27:21

标签: c# gridview updatepanel

我有一个GridView,当从下拉列表中选择一个值(id = drpClass)时,它会加载数据。在这个网格视图中,我有一个编辑和删除按钮。但是当我单击删除按钮时,该项目从数据库中删除但网格视图不刷新。如果我刷新页面,GridView会成功加载

ASPX

<asp:UpdatePanel ID="UpdatePanel7" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpClass" />
</Triggers>
<ContentTemplate>
<p style="margin: 0 0 5px 0;"><b>Details</b></p>
<asp:GridView ID="GridView1" DataKeyNames="DCU_IdNo" class="table table-striped table-bordered "
ShowFooter="True" EmptyDataText="No Data Found" runat="server" ShowHeaderWhenEmpty="True">
<Columns>
    <asp:TemplateField HeaderText="Sl" HeaderStyle-Width="10px">
        <ItemTemplate>
            <asp:Label ID="lblCode" Text='<%# Container.DisplayIndex+1%>' runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Title" HeaderStyle-Width="1500px">
        <ItemTemplate>
            <asp:Label ID="title"
                Text='<%# Eval("DCU_Title")%>' runat="server"></asp:Label>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Attachment">
        <ItemTemplate>
            <%# DataBinder.Eval(Container, "DataItem.DCU_FilePath")%>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Edit" HeaderStyle-Width="1px">
        <ItemTemplate>
            <asp:LinkButton ID="btnEdit" OnClick="btnEdit_Click" runat="server"> 
<span aria-hidden="true" class="glyphicon glyphicon-edit"></span></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Delete" HeaderStyle-Width="100px">
        <ItemTemplate>
            <asp:LinkButton ID="btnDelete" OnClick="btnDelete_Click"
                OnClientClick="if (!confirm('Are you sure you want delete?')) return false;"
                runat="server"> <span class="glyphicon
glyphicon-trash"></span></asp:LinkButton>
        </ItemTemplate>
    </asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

protected void btnDelete_Click(object sender, EventArgs e)
    {
        int rowIndex = ((sender as LinkButton).NamingContainer as GridViewRow).RowIndex;
        int id = Convert.ToInt32(grdUploads.DataKeys[rowIndex].Values[0]);
        SqlDataAdapter sda = new SqlDataAdapter("select DCU_FilePath from OP_DownloadsUploads where DCU_IdNo='" + id + "'", con);
        DataTable datatable = new DataTable();
        sda.Fill(datatable);
        if (datatable.Rows.Count > 0)
        {
            string path = Server.MapPath(datatable.Rows[0][0].ToString());
            FileInfo file = new FileInfo(path);
            if (file.Exists)//check file exsit or not
            {
                file.Delete();
            }
            SqlDataAdapter sda1 = new SqlDataAdapter("delete from OP_DownloadsUploads where DCU_IdNo='" + id + "'", con);
            DataTable datatable1 = new DataTable();
            sda1.Fill(datatable1);
            Response.Write("<script>alert('Deleted Sucessfully')</script>");
            LoadGrid();

        }
    }

所以我的要求是刷新GridView并加载新项目

2 个答案:

答案 0 :(得分:1)

尝试清除网格,然后重新加载它,看看是否有效。

答案 1 :(得分:0)

您可能希望引发源数据的PIVOT事件。否则,GridView无法识别更改。