GridView 不刷新内部 updatepanal
GridView not refreshing inside updatepanal
我有一个 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>
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();
}
}
所以我的要求是刷新 GridView 并加载新项目
尝试清除网格然后重新加载它看看是否有效。
您可能想要引发源数据的 PropertyChanged
事件。否则 GridView 无法感知变化。
我有一个 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>
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();
}
}
所以我的要求是刷新 GridView 并加载新项目
尝试清除网格然后重新加载它看看是否有效。
您可能想要引发源数据的 PropertyChanged
事件。否则 GridView 无法感知变化。