前几天在做一个BBS网页的时候遇到这样一个问题。我用GridView绑定数据,在删除某一个帖子前要判断用户是否有删除的权限,我用RowDeleting事件来判断,该事件是发生在帖子删除前的,可运行的时候RowDeleting事件是在帖子删除后发生的,请大家帮忙解决以下:
public partial class TopicList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
InitData();
if (!CheckUser())
Response.Redirect("Login.aspx");
}
private bool CheckUser()
{
if (Session["login_name"] == null)
{
Response.Write("<script language=javascript>alert('请登录!');</script>");
return false;
}
return true;
}
private void InitData()
{
DataSet ds = Topic.QueryTopic();
GV.DataSource = ds;
GV.DataBind();
LabelPages.Text = "查询结果(第" + (GV.PageIndex + 1).ToString() + "页 共" + GV.PageCount.ToString() + "页)";
}
protected void GV_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GV.PageIndex = e.NewPageIndex;
InitData();
}
protected void GV_RowCommamd(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
int topicId = -1;
switch (e.CommandName)
{
case "Update":
topicId = Convert.ToInt32(GV.Rows[index].Cells[0].Text);
Response.Redirect("TopicUpdate.aspx?topic_id=" + topicId);
break;
case "Delete":
topicId = Convert.ToInt32(GV.Rows[index].Cells[0].ToString());
Topic topic = new Topic();
topic.LoadData(topicId);
topic.Delete();
break;
default:
break;
}
}
protected void GV_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string userLoginNameOfTopic = GV.Rows[e.RowIndex].Cells[1].Text.ToString();
if (userLoginNameOfTopic == "guest" || userLoginNameOfTopic != Session["login_name"].ToString())
{
Response.Write("<Script language=JavaScript>alert('您无权删除!');</Script>");
e.Cancel = true;
}
}
protected void GV_Updating(object sender, GridViewUpdateEventArgs e)
{
string userLoginNameOfTopic = GV.Rows[e.RowIndex].Cells[1].Text.ToString();
if (userLoginNameOfTopic == "guest" || userLoginNameOfTopic != Session["login_name"].ToString())
{
Response.Write("<Script language=JavaScript>alert('您无权修改!');</Script>");
e.Cancel = true;
}
}
}