点击删除按钮之后,用 window.close();关闭之后,重新打开这个页面才可以刷新页面。但是用window.location.reload(); 不能刷新页面。这是为什么,哪位大侠看看怎么处理?谢谢
(注意:上面的窗体是window.showModalDialog弹出来的页面)
主要代码:
JS:
<script type="text/javascript" language="javascript">
function deleteFile(strKeyId) {//我们就是通过这个函数来异步获取信息的
//alert(document.getElementById("HiddenField3").value);
//if (document.getElementById("HiddenField3").value == "0") {
// alert("你没有删除权限!");
// return false;
//}
debugger
var xmlHttpReq = null;
if (window.ActiveXObject) {
xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttpReq = new XMLHttpRequest();
}
else {
xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
}
//alert(xmlHttpReq);
if (xmlHttpReq != null) {//如果对象实例化成功 我们就可以干活啦
xmlHttpReq.open("POST", "DeleteFile.aspx?id=" + strKeyId, true);
//调用open()方法并采用异步方式
xmlHttpReq.send(null); //因为使用get方式提交,所以可以使用null参调用
//document.execCommand('Refresh');
window.close();
//window.location.reload();
//location.reload();
//alert("2");
//alert("3");
//window.location.reload(location.href);
//location.replace(location.href);
//window.navigate(location)
}
}
</script>
<asp:DataList ID="DataList1" runat="server"
BorderWidth="0px" Width="100%" onitemdatabound="DataList1_ItemDataBound"
BorderStyle="None">
<%-- OnDeleteCommand="DataList1_DeleteCommand" OnSelectedIndexChanged="DataList1_SelectedIndexChanged"
OnItemDataBound="DataList1_ItemDataBound" --%>
<ItemTemplate>
<table style="height: 40px;border-bottom:1px solid #c4ddff; line-height:40px;" width="100%">
<tr>
<td style="font-size: 12px;padding-left:10px;" width="67%"> <%# Eval("Title")%></td>
<td style="font-size: 12px;" width="18%">
<%# Convert.ToDateTime(Eval("CreatedOn").ToString()).ToShortDateString().ToString()%>
</td>
<td style="font-size: 12px;" width="15%" align="left" id="td2" runat="server">
<input id='<%# Eval("New_kbarticlecommentId")%>' type="button" onclick='javascript: deleteFile(this.id);' value="" class="buttondel"/>//删除按钮
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
DeleteFile.aspx:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.IO;
namespace Lovol_FileDownUpload
{
public partial class DeleteFile : System.Web.UI.Page
{
CrmUtil cu = new CrmUtil();
protected void Page_Load(object sender, EventArgs e)
{
string id = Request["id"].ToString();
StringBuilder sbSQLText = new StringBuilder();
sbSQLText.AppendLine("update LOVOL_MSCRM.dbo.New_kbarticlecommentBase set DeletionStateCode=2 where New_kbarticlecommentId='" + id + "'");
//cu.WriteLog("更新主记录" + sbSQLText.ToString());
SqlCommand cmd = new SqlCommand(sbSQLText.ToString());
cu.ExecuteBySQL(cmd);
StringBuilder sbSql = new StringBuilder();
sbSql.AppendLine("select New_name from LOVOL_MSCRM.dbo.New_kbarticlecomment");
sbSql.AppendLine("where New_kbarticlecommentId=@New_kbarticlecommentId");
SqlCommand cmd1 = new SqlCommand(sbSql.ToString());
cmd1.Parameters.Add("@New_kbarticlecommentId", SqlDbType.UniqueIdentifier).Value = new Guid(id);
DataTable dt = cu.QueryBySql(cmd1);
if (dt.Rows.Count > 0)
{
string filepath = MapPath("upload/" + dt.Rows[0][0].ToString());
if (File.Exists(filepath))
{
File.Delete(filepath);
} }
}
}
}
解决思路 :
步骤如下:
1 前台放一个按钮。设置为隐藏。注意:不能直接设置Visible=false的方式,这样的话在ASP.Net 2.0编译后的代码里是找不到这个按钮的。需要设置风格:style="display:none;"
2 双击按钮,写入C#方法,或者写一个调用后台其他方法的代码段
3 前台JS调用的时候,照如下写,这样可以模拟按钮的点击事件,触发后台方法:document.getElementById("Button2").click()
JS:document.getElementById("btnR").click();
css: .uplodadbtn{ display:none;}
<asp:Button ID="btnR" runat="server" onclick="btnR_Click" CssClass="uplodadbtn"/>
C#: protected void btnR_Click(object sender, EventArgs e)
{
bind();
}