简体   繁体   中英

GridView not refreshing inside updatepanal

I have a GridView which loads data when a value selected from dropdown(id=drpClass). Inside this grid view i have an edit and delete button. But when i click the delete button the item deleted from the database but the grid view is not refreshing. If i refresh the page the GridView loads Successfully

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>

Cs

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();

        }
    }

So my requirement is to refresh the GridView and load new item

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

You may want to raise the PropertyChanged event of the source data. Otherwise the GridView cannot be aware of the changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM