单击 gridview 内的按钮时,更新面板内的 Gridview 不刷新
Gridview inside update panel not refreshing when click on button inside gridview
我有一个 gridview,它在下拉列表 OnSelectedIndexChanged 更改时加载数据。网格视图位于更新面板内。数据加载成功。但是当我点击更新面板内的删除 link 按钮(Id="btnDelete")时,数据从数据库中删除但是网格视图不是 refreshing.I 发现错误在控制台为,
ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:940 Uncaught Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
at Function.Error$create [as create] (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:847)
at PageRequestManager$_createPageRequestManagerParserError [as _createPageRequestManagerParserError] (ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:776)
at PageRequestManager$_parseDelta [as _parseDelta] (ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:1554)
at PageRequestManager$_onFormSubmitCompleted [as _onFormSubmitCompleted] (ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:1414)
at Array.<anonymous> (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:628)
at ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:4338
at raise (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:7429)
at WebRequest$completed [as completed] (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:7433)
at XMLHttpRequest.XMLHttpExecutor._onReadyStateChange (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:6857)
Aspx 页面
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDiv" />
</Triggers>
<ContentTemplate>
<p style="margin: 0 0 5px 0;"><b>Details</b></p>
<asp:GridView ID="GridView1" DataKeyNames="OP_AS_ID" class="table table-striped table-bordered "
AllowPaging="True" CellPadding="4" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnPageIndexChanging="GridView1_PageIndexChanging1" PageSize="15"
ShowFooter="True" EmptyDataText="No Data Found" runat="server" ViewStateMode="Inherit">
<PagerStyle CssClass="numericpageul" />
<Columns>
<asp:TemplateField HeaderText="Slno" HeaderStyle-Width="10px">
<ItemTemplate>
<asp:Label ID="lblSl" Text='<%# Container.DisplayIndex+1%>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" HeaderStyle-Width="400px">
<ItemTemplate>
<asp:Label ID="lblCoAssTitlempanyName" Text='<%# Eval("OP_AS_Title") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Date" Text='<%# Eval("OP_AS_Date","{0:dd/MM/yyyy}") %>'
runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Submission Date" HeaderStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="DateofSub" Text='<%# Eval("OP_AS_LastDate","{0:dd/MM/yyyy}") %>'
runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Attachment" HeaderStyle-Width="150px">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.OP_ASF_FilePath")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" CommandName="Edit" runat="server"> <span aria-hidden="true" class="glyphicon glyphicon-edit"></span></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandName="Delete" OnClientClick="if (!confirm('Are you sure you want delete?')) return false;" runat="server"> <span aria-hidden="true" class="glyphicon
glyphicon-trash"></span></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Cs 页面
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
decimal id = (decimal)GridView1.DataKeys[e.RowIndex].Value;
DataTable dtas = Assignment.AssignmentDelete(id);//OP_del_AssignmentDelete
if (dtas.Rows.Count > 0)
{
string path = Server.MapPath(dtas.Rows[0][0].ToString());
FileInfo file = new FileInfo(path);
if (file.Exists)//check file exsit or not
{
file.Delete();
}
Response.Write("<script>alert('Deleted Sucessfully')</script>");
}
GridReset();
LoadGridView();
}
public void GridReset()
{
GridView1.Visible = true;GridView1.DataSource = null;
}
public void LoadGridView()
{
decimal GsId = 0;
if (drpDiv.SelectedIndex == -1)
{
GsId = 0;
}
else
{
GsId = Convert.ToDecimal(drpDiv.Text);
}
SqlDataAdapter sda = new SqlDataAdapter("select * from OP_Assignments inner join OP_AssignmentFiles on OP_Assignments.OP_AS_ID = OP_AssignmentFiles.OP_AS_ID where OP_AS_Staffid='" + StaffId + "' and OP_GS_IdNo='" + GsId + "' and OP_AS_SubjId='" + Convert.ToInt32(drpSubject.Text) + "' and OP_AS_TypeId='" + Convert.ToInt32(drpAssignmentType.Text) + "' order by OP_Assignments.OP_AS_ID desc,OP_AS_Type asc", con);
DataTable datatable = new DataTable();
sda.Fill(datatable);
for (int index = 0; index < datatable.Rows.Count; index++)
{
string fpath = datatable.Rows[index]["OP_ASF_FilePath"].ToString();
if ((fpath == "") || (fpath == "nofile"))
{
datatable.Rows[index]["OP_ASF_FilePath"] = "";
}
else
datatable.Rows[index]["OP_ASF_FilePath"] = "<a style='padding-left:50px' target=_blank href = '" + datatable.Rows[index]["OP_ASF_FilePath"].ToString() + "'><span class='glyphicon glyphicon-paperclip'></span></a>";
}
GridView1.AutoGenerateColumns = false;
GridView1.DataSource = datatable;
GridView1.DataBind();
}
您不能在异步回发期间使用 Response.Write
。 GridView1_RowDeleting 包含 Response.Write
。它抛出异常。
我有一个 gridview,它在下拉列表 OnSelectedIndexChanged 更改时加载数据。网格视图位于更新面板内。数据加载成功。但是当我点击更新面板内的删除 link 按钮(Id="btnDelete")时,数据从数据库中删除但是网格视图不是 refreshing.I 发现错误在控制台为,
ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:940 Uncaught Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.
at Function.Error$create [as create] (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:847)
at PageRequestManager$_createPageRequestManagerParserError [as _createPageRequestManagerParserError] (ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:776)
at PageRequestManager$_parseDelta [as _parseDelta] (ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:1554)
at PageRequestManager$_onFormSubmitCompleted [as _onFormSubmitCompleted] (ScriptResource.axd?d=DT3YJR8QaqV61-teuz0hgjs5-RCOey2-uDAyhszGAHbLjEcc31icIOPnrYipVEyQcXFMxnve0uP6xEvgOIC53vEC-feFnRyAEu_AKMP4G6e6S-L7bP5B3AYVhopxS7MPDYDXflFfsfy3B0lvfqyFKQ2&t=60b7ac5d:1414)
at Array.<anonymous> (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:628)
at ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:4338
at raise (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:7429)
at WebRequest$completed [as completed] (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:7433)
at XMLHttpRequest.XMLHttpExecutor._onReadyStateChange (ScriptResource.axd?d=yeSNAJTOPtPimTGCAo3LlQGMjf69gb9mP9Cg5L8cFQrpj29YLswnMWucwR38cSJVgaRydbsSurxoNqaEDGskNdXYd0Esj7Ur7el2ofn5TPuvlHvoElCZjnStXN2vl-sz0&t=60b7ac5d:6857)
Aspx 页面
<asp:UpdatePanel ID="UpdatePanel4" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="drpDiv" />
</Triggers>
<ContentTemplate>
<p style="margin: 0 0 5px 0;"><b>Details</b></p>
<asp:GridView ID="GridView1" DataKeyNames="OP_AS_ID" class="table table-striped table-bordered "
AllowPaging="True" CellPadding="4" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnPageIndexChanging="GridView1_PageIndexChanging1" PageSize="15"
ShowFooter="True" EmptyDataText="No Data Found" runat="server" ViewStateMode="Inherit">
<PagerStyle CssClass="numericpageul" />
<Columns>
<asp:TemplateField HeaderText="Slno" HeaderStyle-Width="10px">
<ItemTemplate>
<asp:Label ID="lblSl" Text='<%# Container.DisplayIndex+1%>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title" HeaderStyle-Width="400px">
<ItemTemplate>
<asp:Label ID="lblCoAssTitlempanyName" Text='<%# Eval("OP_AS_Title") %>' runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date" HeaderStyle-Width="100px">
<ItemTemplate>
<asp:Label ID="Date" Text='<%# Eval("OP_AS_Date","{0:dd/MM/yyyy}") %>'
runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Submission Date" HeaderStyle-Width="150px">
<ItemTemplate>
<asp:Label ID="DateofSub" Text='<%# Eval("OP_AS_LastDate","{0:dd/MM/yyyy}") %>'
runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="View Attachment" HeaderStyle-Width="150px">
<ItemTemplate>
<%# DataBinder.Eval(Container, "DataItem.OP_ASF_FilePath")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" CommandName="Edit" runat="server"> <span aria-hidden="true" class="glyphicon glyphicon-edit"></span></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandName="Delete" OnClientClick="if (!confirm('Are you sure you want delete?')) return false;" runat="server"> <span aria-hidden="true" class="glyphicon
glyphicon-trash"></span></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Cs 页面
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
decimal id = (decimal)GridView1.DataKeys[e.RowIndex].Value;
DataTable dtas = Assignment.AssignmentDelete(id);//OP_del_AssignmentDelete
if (dtas.Rows.Count > 0)
{
string path = Server.MapPath(dtas.Rows[0][0].ToString());
FileInfo file = new FileInfo(path);
if (file.Exists)//check file exsit or not
{
file.Delete();
}
Response.Write("<script>alert('Deleted Sucessfully')</script>");
}
GridReset();
LoadGridView();
}
public void GridReset()
{
GridView1.Visible = true;GridView1.DataSource = null;
}
public void LoadGridView()
{
decimal GsId = 0;
if (drpDiv.SelectedIndex == -1)
{
GsId = 0;
}
else
{
GsId = Convert.ToDecimal(drpDiv.Text);
}
SqlDataAdapter sda = new SqlDataAdapter("select * from OP_Assignments inner join OP_AssignmentFiles on OP_Assignments.OP_AS_ID = OP_AssignmentFiles.OP_AS_ID where OP_AS_Staffid='" + StaffId + "' and OP_GS_IdNo='" + GsId + "' and OP_AS_SubjId='" + Convert.ToInt32(drpSubject.Text) + "' and OP_AS_TypeId='" + Convert.ToInt32(drpAssignmentType.Text) + "' order by OP_Assignments.OP_AS_ID desc,OP_AS_Type asc", con);
DataTable datatable = new DataTable();
sda.Fill(datatable);
for (int index = 0; index < datatable.Rows.Count; index++)
{
string fpath = datatable.Rows[index]["OP_ASF_FilePath"].ToString();
if ((fpath == "") || (fpath == "nofile"))
{
datatable.Rows[index]["OP_ASF_FilePath"] = "";
}
else
datatable.Rows[index]["OP_ASF_FilePath"] = "<a style='padding-left:50px' target=_blank href = '" + datatable.Rows[index]["OP_ASF_FilePath"].ToString() + "'><span class='glyphicon glyphicon-paperclip'></span></a>";
}
GridView1.AutoGenerateColumns = false;
GridView1.DataSource = datatable;
GridView1.DataBind();
}
您不能在异步回发期间使用 Response.Write
。 GridView1_RowDeleting 包含 Response.Write
。它抛出异常。