我原来是xp系统,vs2008+sql2005 做了一个博客系统在pageload用ispostback来绑定数据库,运行完全没问题
但是后来换了vista,也是vs2008+sql2005,但是运行之前的博客时,点击页面按钮式(例如删除评论等)似乎不会跳到ispostback里面的那个绑定的方法。 这是一个删除评论的代码,现在删除评论之后不会重新加载了,本来xp下面可以的。。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; public partial class Admin_Default : System.Web.UI.Page
{
private string sConnectionString = ConfigurationManager.ConnectionStrings["blogConnString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
Bind();
}
} private void Bind()
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("select * from wenzhang;select * from comment", conn);
da.Fill(ds);
}
ds.Relations.Add("relationsbetweenwenzhangandcomment", ds.Tables[0].Columns["blog_id"], ds.Tables[1].Columns["commentblog_id"]);
Repeater3.DataSource = ds;
Repeater3.DataBind();
} protected void Repeater4_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delcomment")
{
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("delete from comment where comment_id=@commentid", conn))
{
cmd.Parameters.AddWithValue("@commentid", e.CommandArgument);
cmd.ExecuteNonQuery();
}
}
}
}
}
但是后来换了vista,也是vs2008+sql2005,但是运行之前的博客时,点击页面按钮式(例如删除评论等)似乎不会跳到ispostback里面的那个绑定的方法。 这是一个删除评论的代码,现在删除评论之后不会重新加载了,本来xp下面可以的。。
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient; public partial class Admin_Default : System.Web.UI.Page
{
private string sConnectionString = ConfigurationManager.ConnectionStrings["blogConnString"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack )
{
Bind();
}
} private void Bind()
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("select * from wenzhang;select * from comment", conn);
da.Fill(ds);
}
ds.Relations.Add("relationsbetweenwenzhangandcomment", ds.Tables[0].Columns["blog_id"], ds.Tables[1].Columns["commentblog_id"]);
Repeater3.DataSource = ds;
Repeater3.DataBind();
} protected void Repeater4_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delcomment")
{
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("delete from comment where comment_id=@commentid", conn))
{
cmd.Parameters.AddWithValue("@commentid", e.CommandArgument);
cmd.ExecuteNonQuery();
}
}
}
}
}
这里去掉就可以了。
BS每个服务器动作都会先进入PageLoad
所以每回都重新绑定一遍 ,
现在就是按删除的话,不会重新绑定数据库
public partial class Admin_Default : System.Web.UI.Page
{
private bool mbRebind = false;
private string sConnectionString = ConfigurationManager.ConnectionStrings["blogConnString"].ToString();
protected void Page_PreRender(object sender, EventArgs e)
{
if (!IsPostBack || mbRebind)
{
Bind();
}
} private void Bind()
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
SqlDataAdapter da = new SqlDataAdapter("select * from wenzhang;select * from comment", conn);
da.Fill(ds);
}
ds.Relations.Add("relationsbetweenwenzhangandcomment", ds.Tables[0].Columns["blog_id"], ds.Tables[1].Columns["commentblog_id"]);
Repeater3.DataSource = ds;
Repeater3.DataBind();
} protected void Repeater4_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delcomment")
{
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("delete from comment where comment_id=@commentid", conn))
{
cmd.Parameters.AddWithValue("@commentid", e.CommandArgument);
cmd.ExecuteNonQuery();
mbRebind = true;
}
}
}
}
} 好像你发了俩个贴,那贴里面吧IsCallBack改成IsPostBack.
获取一个值,该值指示该页是否正为响应客户端回发而加载,或者它是否正被首次加载和访问。
如果是为响应客户端回发而加载该页,则为 true;否则为 false。
具体的楼主可以参考MSDN.
{
if (e.CommandName == "delcomment")
{
using (SqlConnection conn = new SqlConnection(sConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand("delete from comment where comment_id=@commentid", conn))
{
cmd.Parameters.AddWithValue("@commentid", e.CommandArgument);
cmd.ExecuteNonQuery();
Bind();
}
}
}
}