public partial class Manager_CommentList : System.Web.UI.Page
{
Common com = new Common();
CommentDAL cDal = new CommentDAL();
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
com.BindDataToDataList(dlArticle, cDal.GetArticles());
}
Master.Page.Title = "评论列表";
}
#region 最外层的DataList绑定事件
/// <summary>
/// 最外层的DataList
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void dlArticle_ItemDataBound(object sender, DataListItemEventArgs e)
{
//获取嵌套的DataList控件
DataList dl = e.Item.FindControl("dlComment") as DataList;
//获取DataList当前绑定项目的主键
int articleId = int.Parse(dlArticle.DataKeys[e.Item.ItemIndex].ToString());
com.BindDataToDataList(dl, cDal.GetCommentByArticleId(articleId));
}
#endregion
#region 审核状态转换
public string ConvertToStr(int state)
{
if (state == 1)
{
return "<img src=/"../images/admin/true.jpg/" style=/"float:left;/"><p style=/"color:green;/">已审核</p>";
}
else
{
return "<img src=/"../images/admin/false.jpg/" style=/"float:left;/"><p style=/"color:red;/">未审核</p>";
}
}
#endregion
#region 修改审核状态(嵌套三层的控件的控制)
protected void cboxUpState_CheckedChanged(object sender, EventArgs e)
{
CheckBox cboxUpState=sender as CheckBox;
int commentId=int.Parse(cboxUpState.Attributes["commentId"].ToString());
if (cboxUpState.Checked)
{
cDal.UpdateCommentStateById(commentId,1);
}
else
{
cDal.UpdateCommentStateById(commentId,0);
}
com.BindDataToDataList(dlArticle, cDal.GetArticles());
}
#endregion
#region 绑定CheckBox的审核状态(嵌套两层的控件控制)
protected void dlComment_ItemDataBound(object sender, DataListItemEventArgs e)
{
CheckBox cbox = e.Item.FindControl("cboxUpState") as CheckBox;
DataList dl = cbox.Parent.Parent as DataList;
//给CheckBox添加一个属性用来保存当前行的Id
cbox.Attributes.Add("commentId", dl.DataKeys[e.Item.ItemIndex].ToString());
int commentId = int.Parse(dl.DataKeys[e.Item.ItemIndex].ToString());
int state = int.Parse(cDal.GetCommentById(commentId).Rows[0][3].ToString());
if (state == 1)
{
cbox.Checked = true;
}
else
{
cbox.Checked = false;
}
}
#endregion