数据控件三层嵌套

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值