代码如下:::
public partial class manage_aboutpages_Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowNoticeInfo();
}
} #region 显示所有公告信息
protected void ShowNoticeInfo()
{
dt = CommonCS.GetInfo("Notice_ShowAll");
if (dt.Rows.Count == 0)//这里正确,可以得到正常值
{
msg.Visible = true;
msg.Text = Message.RetMsg("noinfo", "");
healthist.Visible = false;
} } protected void delFile(int id)
{
int rowsCount = dt.Rows.Count;//这里就不行了。总是0.dt是全局变量啊。怎么为0??
//是否具有数据行
if (rowsCount>0)
{
for (int i = 0; i < rowsCount;i++ )
{
//根据id取得附件文件地址
if (id==Convert.ToInt32(dt.Rows[i]["id"]))
{ string filePath = Server.MapPath(dt.Rows[i]["filesaddress"].ToString()); //文件是否存在,存在则删除
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
} }
}
}
}
public partial class manage_aboutpages_Default : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ShowNoticeInfo();
}
} #region 显示所有公告信息
protected void ShowNoticeInfo()
{
dt = CommonCS.GetInfo("Notice_ShowAll");
if (dt.Rows.Count == 0)//这里正确,可以得到正常值
{
msg.Visible = true;
msg.Text = Message.RetMsg("noinfo", "");
healthist.Visible = false;
} } protected void delFile(int id)
{
int rowsCount = dt.Rows.Count;//这里就不行了。总是0.dt是全局变量啊。怎么为0??
//是否具有数据行
if (rowsCount>0)
{
for (int i = 0; i < rowsCount;i++ )
{
//根据id取得附件文件地址
if (id==Convert.ToInt32(dt.Rows[i]["id"]))
{ string filePath = Server.MapPath(dt.Rows[i]["filesaddress"].ToString()); //文件是否存在,存在则删除
if (System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);
} }
}
}
}
使用ViewSate["dt"]保存数据
Page ->Page_Load -> ShowNoticeInfo (正确处理了DataTable,Rows.Count != 0)
页面消亡!!!
点击按钮提交 Page (完全的一个新实例)->Page_Load -> (没有执行ShowNoticeInfo )
-> delFile ( 这里的Rows.Count肯定为0)
当datatable大到一定程度的时候,viewstate会截断数据;正确做法是重新提交查询
dt = CommonCS.GetInfo("Notice_ShowAll");
protected void delFile(int id)
{
int rowsCount = dt.Rows.Count;//这里就不行了。总是0.dt是全局变量啊。怎么为0??
//是否具有数据行
if (rowsCount>0)
{
protected void ShowNoticeInfo()
{
dt = CommonCS.GetInfo("Notice_ShowAll");
if(dt.Rows.Count!=0)
ViewState["mydt"]=dt; protected void delFile(int id)
{
if(ViewState["mydt"]!=null)
dt=ViewState["mydt"] as DataTable;
int rowsCount = dt.Rows.Count;//这里就不行了。总是0.dt是全局变量啊。怎么为0??
//是否具有数据行
if (rowsCount>0)
我现在已经把数据绑定到前台的repeater控件中了。控件中有一个字段,
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete" CommandArgument='<%# Eval("id") %>' OnClientClick="delone(); ">删除</asp:LinkButton>
执行的删除操作。 #region 删除当前行记录
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
int id = Convert.ToInt32(e.CommandArgument);//这个方法中能不能得到两个参数啊。要是
能够得到两个就好了。就不用上面的方法了。呵呵 if (e.CommandName == "Delete")//要和aspx名称一样
{
try
{
//删除附件文件
delFile(id); CommonCS.del(id, "Notice_Delete");
//ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('删除成功');", true);
}
catch (System.Exception ex)
{
string mess = Message.RetMsg("delfailerror", ex.ToString()); ScriptManager.RegisterStartupScript(this, this.GetType(), "", "alert('" + mess + "');", true);
}
ShowNoticeInfo();
}
}